tags:

views:

1639

answers:

1

I'm working on a socket program and everything seems fine.However after i compiled and run the server, i compile and run the client. But there is a problem in connect() function and even socket is ok client does not connect or server does not accept connection...

While compiling, i got no errors. but after client has ran : error is

Connect: Socket operation on non-socket

here is my code:

server side:

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(&(ServerAddr.sin_zero), '\0', 8); //


     if (bind (ListeningSocket, (struct sockaddr *)&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 *)&ClientAddr,(socklen_t *)&sin_size);
         printf("Server: accept() is OK...\n");
         printf("Server: Client connected, ready for receiving and sending data...\n");

.........
....

 //}

client side :

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(&(ClientAddr.sin_zero), '\0', 8); 




if  ( connect (SendingSocket, (struct sockaddr *)&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");
     }

....

as i said before server runs without and error and waits for the connections as soon as the client sends connect function i got the message:

Connect: Socket operation on non-socket

+3  A: 

You are missing a parenthesis. The code

if(SendingSocket = socket(AF_INET, SOCK_STREAM, 0) == -1)

should read

if((SendingSocket = socket(AF_INET, SOCK_STREAM, 0)) == -1)

The way you wrote it, it means

if(SendingSocket = (socket(AF_INET, SOCK_STREAM, 0) == -1))

which means: compare the socket() result to -1, giving 0 or 1, and assign that to SendingSocket; then test whether it is true or false. So SendingSocket is likely 0, assuming the socket() call succeeded.

Martin v. Löwis
ohh okey it seems know fine. really i can't imagine that a parenthesis will spend my 6 hours :) thanks ...so if anybody gets the error of "error is Connect: Socket operation on non-socket" should be more careful using the parenthesis.
berkay