0

I'm writing a local client-server application in C, running on a 64bit Windows 7 machine.

My problem is that when I call bind() for the server socket, it always returns -1 and I can't seem to figure out why. I tried using setsockopt with SO_REUSEADDR before calling bind(), but that ended up doing nothing so I removed it. I also tried changing INADDR_ANY to inet_address("127.0.0.1") and inet_address(ipv4 of my pc) and that didn't work either.

Here's the code for the server socket setup:

SOCKET serverSocket;

//AF_UNIX for process communication
serverSocket = socket(AF_UNIX, SOCK_STREAM, 0);

struct sockaddr_in serverAddress;

serverAddress.sin_family = AF_UNIX;
serverAddress.sin_port = htons(10509);
serverAddress.sin_addr.s_addr = INADDR_ANY;

// always returns -1, no matter what I do.
if (bind(serverSocket, (LPSOCKADDR) &serverAddress, sizeof(serverAddress)) == -1)
{
    int result = MessageBoxA(NULL, "Failed to set up server. Continue?", "Server Error", MB_YESNO | MB_SYSTEMMODAL);

    closesocket(serverSocket);

    if (result == IDNO)
        exit(1);
    else
        goto cleanup;
}

...

Why is this happening? What can I do to fix it?

1 Answers1

0

AF_UNIX sockets are not available on Windows 7. Simply change to AF_INET sockets on 127.0.0.1 if you want to communicate between processes.