This snippet is taken from server.c, which is supposed to be receiving data from a client machine:
int main() {
int sock, client_socket;
char buffer[1024];
char response[18384];
struct sockaddr_in server_address, client_address;
int i = 0;
int optval = 1;
socklen_t client_length;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char *) &optval, sizeof(optval)) < 0) {
printf("Error Setting TCP Socket Options!\n");
return 1;
}
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr("10.0.0.188");
server_address.sin_port = htons(12345);
bind(sock, (struct sockaddr *) &server_address, sizeof(server_address));
listen(sock, 5);
client_length = sizeof(client_address);
client_socket = accept(sock, (struct sockaddr *) &client_address, &client_length);
When I compile and run this on Kali Linux, it sets the TCP socket fine and works, but when I compile and run this on Windows with gcc gcc server.c -o server.exe -lws2_32, I get the error message from setsockopt():
Error Setting TCP Socket Options!
Below this snippet is the bind() and listen() function, along with if/else block deciding what to do with the data received from the client.
On Windows, you must call
WSAStartup()before using any other socket functions (and callWSACleanup()when you are done using the Winsock library before exiting). Other platforms don't have similar functions to initialize/cleanup their socket libraries.And, you should always check the return values of socket functions (or any system call) for failure, and use
WSAGetLastError()(errnoon other platforms) to retrieve error codes to tel, you why the functions failed.You are not checking if
socket()fails (which would report aWSANOTINITIALISEDerror), so you are then passing an invalid socket tosetsockopt()(which would report aWSAENOTSOCKerror).Try this: