views:

168

answers:

1

I am testing out a Socket Server application in c and I am getting an error on the bind function with code 10038. I looked this up and MSDN says it means:

An operation was attempted on something that is not a socket. Either the socket handle parameter did not reference a valid socket, or for select, a member of an fd_set was not valid.

Here is the code:

// I have the correct include files such as include , but stackoverflow displays it weird when i put #include winsock2.h

int main()
{
WSADATA         wsaData;
SOCKET          ListeningSocket;
SOCKET          NewConnection;
SOCKADDR_IN     ServerAddr;
int             Port = 5150;


if(WSAStartup(MAKEWORD(2,2),&wsaData) != 0)
{
    printf("Server: WSAStartup failed with error %ld\n",WSAGetLastError());
    return -1;
}
else
{
        printf("Server: The Winsock dll found!\n");
        printf("Server: The current status is: %s.\n",wsaData.szSystemStatus);
}

if(LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
    printf("Server: The dll do not support Winsock version 
           %u.%u!\n",LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion));
    WSACleanup();
    return -1;
}
else
{
    printf("Server: The dll supports the Winsock version %u.%u!\n",LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion));
    printf("Server: The highest version this dll can support: %u.%u\n",LOBYTE(wsaData.wHighVersion),HIBYTE(wsaData.wHighVersion));
}

ListeningSocket == socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

if(ListeningSocket == INVALID_SOCKET)
{
    printf("Server: Error at socket(), error code: %ld\n",WSAGetLastError());
    WSACleanup();
    return -1;
}
else
    printf("Server: socket() is OK!\n");

ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(Port);
ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);

if(bind(ListeningSocket, (SOCKADDR *)&ServerAddr,sizeof(ServerAddr)) == SOCKET_ERROR)
{

    printf("Server: bind() failed! Error code: %ld.\n",WSAGetLastError());
    closesocket(ListeningSocket);
    WSACleanup();
    return -1;
}
else
    printf("Server: bind() is OK!\n");

if(listen(ListeningSocket,5) == SOCKET_ERROR)
{
      printf("Server: listen(): Error listening on socket %ld.\n", WSAGetLastError());
      closesocket(ListeningSocket);
      WSACleanup();
      return -1;
 }
 else
      printf("Server: listen() is OK, I'm waiting for connections...\n");

 printf("Server: accept() is ready...\n");


 while(1)
 {
      NewConnection = SOCKET_ERROR;

      while(NewConnection == SOCKET_ERROR)
      {
          NewConnection = accept(ListeningSocket, NULL, NULL);
      }

      printf("Server: accept() is OK...\n");
      printf("Server: Client connected, ready for receiving and sending data...\n");

      ListeningSocket = NewConnection;

      break;
 }

 if(closesocket(NewConnection) != 0)
     printf("Server: Cannot close \"NewConnection\" socket. Error code: %ld\n", 
            WSAGetLastError());
 else
      printf("Server: Closing \"NewConnection\" socket...\n");

 if(WSACleanup() != 0)
      printf("Server: WSACleanup() failed! Error code: %ld\n", WSAGetLastError());
 else
      printf("Server: WSACleanup() is OK...\n");


 return 0;

}

A: 

I just got it, I put == for

ListeningSocket == socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

instea of =

ListeningSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
Xaisoft
I feel like an idiot.
Xaisoft
since you made the mistake again in your answer ...
bmargulies
yep thanks for pointing that out buddy.
Xaisoft