Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Socket operation on non-socket error help 1

Status
Not open for further replies.

ezeke1

Programmer
Mar 20, 2003
41
US
Hello All,
In writing a simple server application using C, I encountered a weird bind() error. After creating a socket, my bind will fail and I receive a return error of Error 88 Socket Operation on Nonsocket. Has anyone seen this problem before or know what may be causing it? I'll paste my code below....thank you all in advance!
[b/
int main(int argc, char *argv[])
{

BeginAcceptingConnections(); // Bind server

return 0;
}

void BeginAcceptingConnections()
{
struct sockaddr_in serv_addr; // Host server address
unsigned short servport; // Local server port
int servsocket_fd; // Server socket file desc


servport = HOST_SERVER_LISTENPORT; // Assign server port
// HOST_SERVER_LISTENPORT is defined in host_ports.h

// Create socket for incoming connections
if (servsocket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) < 0) {
printf("Served socket failed\n");
exit(1);
}

// Construct local address structure
memset(&serv_addr, 0, sizeof(serv_addr)); // Zero out structure
serv_addr.sin_family = AF_INET; // Internet address family
serv_addr.sin_addr.s_addr = inet_addr(HOST_SERVER_IP); // Any incoming interface
//serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); // Any incoming interface
serv_addr.sin_port = htons(servport); // Local port

printf("HOST SERVER IP IS : %s", HOST_SERVER_IP);
printf("\n");
printf("HOST SERVER PORT IS : %d", servport);
printf("\n");

// Bind to local address
if (bind(servsocket_fd, (struct sockaddr*) &serv_addr, sizeof(struct sockaddr_in)) < 0) {
printf("Binding failed. ERROR => %d %s\n", errno, strerror(errno));
//close(servsocket_fd);
//exit(1);
}

printf("------Server connection established------\n");

BeginListening(servsocket_fd); // Begin listening for connections


}
[/b]
 
> Does anyone have any advice?
Yeah, be patient.

> if (servsocket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) < 0) {
Check your operator precedence table.

You've written this
[tt]if (servsocket_fd = (socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) < 0)) {[/tt]
Which assigns to servsocket_fd the truth value of comparing the result of socket() with being less than 0.
The result is that servsocket_fd contains 0 or 1, and not the result of the socket() call.
This is why you get a "Socket Operation on Nonsocket"

What you want is this
[tt]if ((servsocket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {[/tt]
Which assigns to servsocket_fd the return result of socket(), and then compares with 0

--
 
Salem, good catch :) I looked at the code over and over and the incorrect syntax eluded me. You saved me alot of headaches, thank you very much!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top