Hi, I'm writing a socket based server program in C right now and I got a
"Socket operation on non-socket" error from a bind command in code which seems to be perfectly legal.
Also, the changes I made do the exact same thing as before but without error checking and the bind command succeeds. Thanks to anyone who can tell me why the error occurred.
CODE GENERATING ERROR
Code:
//Create and name the server's socket
if( server_sockfd=socket(AF_INET,SOCK_STREAM,0) == -1){
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
server_address.sin_family=AF_INET; //accept connections from any IP address
server_address.sin_addr.s_addr = htonl(INADDR_ANY); //using the SERVER_PORT
server_address.sin_port = htons(SERVER_PORT);
if(bind(server_sockfd, (struct sockaddr *)&server_address,addr_len)==-1){
perror("Socket binding failed");
exit(EXIT_FAILURE);
}
FIXED CODE
Code:
//Create and name the server's socket
server_sockfd=socket(AF_INET,SOCK_STREAM,0) ;
server_address.sin_family=AF_INET; //accept connections from any IP address
server_address.sin_addr.s_addr = htonl(INADDR_ANY); //using the SERVER_PORT
server_address.sin_port = htons(SERVER_PORT);
if(bind(server_sockfd, (struct sockaddr *)&server_address,addr_len)==-1){
perror("Socket binding failed");
exit(EXIT_FAILURE);
}