C lang, Multi-Client Server, error: Socket operation on non-socket?

So you test if (FD_ISSET(i, &temp_fds)) and on the else branch you attempt to recv on I . So basically you're only trying to receive if I is invalid or would block.

Up vote 0 down vote favorite 2 share g+ share fb share tw.

I've got an assignment to write a server program that accepts multiple clients. I'm writing in C language and trying to accomplish this using a select() statement. I am able to compile, but whenever I telnet in, I get a "Socket operation on non-socket error.

" I've tried researching the error, but can't find anything too helpful. Any assistance would be appreciated. The output of my server is: $ .

/assign2 33333 Waiting for connection... fd is 0 EchoServ recv error: Socket operation on non-socket fd is 1 EchoServ recv error: Socket operation on non-socket EchoServ recv error: Socket operation on non-socket The output of telnet is: $ telnet localhost 33333 Trying 127.0.0.1... Connected to localhost. Localdomain (127.0.0.1). Escape character is '^'.

Welcome to EchoServ chat. My server code: #include #include #include #include #include #include #include #include #include #define PORT 50000 main( int argc, char *argv ) { char buf BUFSIZ , /* buffer for incoming data */ *endptr, /* for strtol() */ *message = "Welcome to EchoServ chat. \r\n"; /* welcome message */ int masterSocket, /* main listening socket for server */ newSocket, /* new sockets for connecting clients */ opt = 1, /* for port reuse code */ nBytes, /* # of incoming bytes */ addrlen; int i,j; /* temp vars */ short int port; /* port number */ fd_set master; /* master file descriptor list */ fd_set temp_fds; /* temp file descriptor list for select() */ int fdmax; /* maximum file descriptor number */ struct sockaddr_in sin; /* Get port number from the command line, or set to default port if no arguments were supplied */ if ( argc == 2 ) { port = strtol(argv1, &endptr, 0); if ( *endptr ) { fprintf(stderr, "EchoServ: Invalid port number.

\n"); exit(EXIT_FAILURE); } } else { port = PORT; } FD_ZERO(&master); /* clear the master and temp sets */ FD_ZERO(&temp_fds); /* Get an internet domain socket */ if ( ( masterSocket = socket( AF_INET, SOCK_STREAM, 0) ) == -1 ) { perror( "EchoServ socket error" ); exit( 1 ); } /* Complete the socket structure */ memset( &sin, 0, sizeof(sin) ); sin. Sin_family = AF_INET; sin. Sin_addr.s_addr = INADDR_ANY; sin.

Sin_port = htons(port); /* Bind the socket to the port number */ if ( bind( masterSocket, ( struct sockaddr *) &sin, sizeof( sin ) ) == -1) { perror( "EchoServ bind failed" ); exit( 1 ); } /* Allow port reuse */ if ( setsockopt( masterSocket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof( opt ) ) == -1 ) { perror( "EchoServ setsockopt error" ); exit(1); } /* Listen for clients that want to connect. */ if ( listen( masterSocket, 5 ) == -1 ) { perror( "EchoServ listen error" ); exit( 1 ); } /* add masterSocket to the master set */ FD_SET(masterSocket, &master); /* track largest file descriptor, starting with masterSocket */ fdmax = masterSocket; /* Wait for a client connection, then accept it. */ puts ( "Waiting for connection..." ); while(1){ temp_fds = master; /* copy master set to temp set */ //addrlen = sizeof(sin); /* wait for activity on a socket */ if (select(fdmax+1, &temp_fds, NULL, NULL, NULL) == -1) { perror("EchoServ select error"); exit(1); } for ( I = 0; I fdmax ) /* update max descriptor */ fdmax = newSocket; //print details of new connection printf("New connection on %s:%d, socket fd is %d \n", inet_ntoa(sin.

Sin_addr), ntohs(sin. Sin_port), newSocket ); //send new connection greeting message if( send( newSocket, message, strlen( message ), 0) == -1 ) { perror("EchoServ welcome message error"); } puts("Welcome message sent successfully"); } } } /* handle incoming data */ else{ if ( ( nBytes = recv( i, buf, sizeof( buf ), 0 ) ) = masterSocket ){ /* except self and masterSocket */ if ( send( j, buf, nBytes, 0) == -1) perror("EchoServ send error"); } } } } } } } return( 0 ); } Thanks for helping. C error-message link|improve this question asked 4 hours agorennX31.

So you test if (FD_ISSET(i, &temp_fds)) and on the else branch you attempt to recv on i. So basically you're only trying to receive if I is invalid or would block. This all of course stems from the unbounded use of blocks and braces.

You probably meant that else to be paired with if (i == masterSocket).

You're correct. I had the else associated with the wrong if. Too many brackets :P Now I'm able to connect, but the data is not passed between clients.

– rennX 3 hours ago Figured out why data wasn't being passed. I was short a set of parenths on: if ( ( nBytes = recv( i, buf, sizeof( buf ), 0 ) ).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions