c - socket programming , bind() error -
my code have written server implementation not working. program cant bind()
socket address , generate error.
code
#include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<string.h> #include<stdlib.h> #include <netdb.h> int main(int argc , char* argv[]) { int socket_descriptor; struct sockaddr_in server; char buffer[512]; int rval; int socket_a; if(socket_descriptor = socket(af_inet, sock_stream , 0) < 0) { perror("error @ creating socket"); exit(1); } server.sin_family = af_inet; server.sin_addr.s_addr = inaddr_any; server.sin_port = htons(5000); if(bind(socket_descriptor,(struct sockaddr * )&server,sizeof(server)) < 0) { perror("error @ binding"); exit(1); } }
error
error @ binding: socket operation on non-socket .
here's problem:
if(socket_descriptor = socket(af_inet, sock_stream , 0) < 0)
the less-than operator <
has higher precedence assignment operator =
. above equivalent to:
if(socket_descriptor = (socket(af_inet, sock_stream , 0) < 0))
assuming socket
succeeds, file descriptor returned not negative, comparison evaluate 0 (i.e. false) , value assigned socket_descriptor
. , since file descriptor 0 stdin
, error socket operation on non-socket.
adding proper set of parenthesis assign socket socket_descriptor
:
if ((socket_descriptor = socket(af_inet, sock_stream , 0)) < 0)
Comments
Post a Comment