title: Error on server/client c program: "Connect: socket operation on non-socket"
body: <p>I'm working on a socket program and everything seems fine when compiling. First I compile and run the server and then I compile and run the client. The server will run fine, but when the client starts up, I receive an error message in the <code>Connect()</code> function. Although the socket appears to be okay, the client will not connect and the server does not see the connection attempt.</p>
  
  <p>The error message is:</p>
  
  <blockquote>
    <p>Connect: Socket operation on non-socket</p>
  </blockquote>
  
  <p>Here is the server-side code:</p>
  
  <pre><code>if ((ListeningSocket = socket(AF_INET, SOCK_STREAM,0 )) == -1){
          printf("socket failed!\n");
          exit(1);
      }
  
       else
            printf("Server: socket() is OK!\n");
  
  
       ServerAddr.sin_family = AF_INET;
       ServerAddr.sin_port = htons(5000);
       ServerAddr.sin_addr.s_addr = INADDR_ANY; // any one for any network can connect
       memset(&amp;(ServerAddr.sin_zero), '\0', 8); //
  
  
       if (bind (ListeningSocket, (struct sockaddr *)&amp;ServerAddr, sizeof(struct sockaddr))==-1)
       {
            printf("Server: bind() failed!\n");
            exit (1);
       }
       else
            printf("Server: bind() is OK!\n");
  
       if (listen(ListeningSocket,5)== -1){
            printf("Server: Error listening on socket\n");
            exit (1);
       }
       else{
       printf("Server: listen() is OK, I'm waiting for connections...\n");
       printf("Server: accept() is ready...\n");}
  
  
  
           sin_size = sizeof(struct sockaddr_in);
       NewConnection = accept(ListeningSocket, (struct sockaddr *)&amp;ClientAddr,(socklen_t *)&amp;sin_size);
           printf("Server: accept() is OK...\n");
           printf("Server: Client connected, ready for receiving and sending data...\n");
  
  .........
  ....
  
   //}
  </code></pre>
  
  <p>And here is the client-side code:</p>
  
  <pre><code>if(SendingSocket = socket(AF_INET, SOCK_STREAM, 0) == -1)
       {
            printf("Client: socket() failed!");
            exit (1);
       }
       else
            printf("Client: socket() is OK!\n");
  
  
       ClientAddr.sin_family = AF_INET;
       ClientAddr.sin_port = htons(5000);  
       ClientAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
       memset(&amp;(ClientAddr.sin_zero), '\0', 8); 
  
  
  
  
  if  ( connect (SendingSocket, (struct sockaddr *)&amp;ClientAddr,
                      sizeof(struct sockaddr)) == -1) 
          {
              perror("Connect");
              exit(1);
          }
  
  
       else
       {
            printf("Client: connect() is OK, got connected...\n");
            printf("Client: Ready for sending and/or receiving data...\n");
       }
  
  ....
  </code></pre>