Why does not accept () a block?

I am new to socket programming under Linux sockets (UNIX). I found the following code on the Internet for a tcp server that spawns a stream for each connection. However, this will not work. The accept () function returns instantly and does not wait for a connection. What am I doing wrong?

this is code

int main(int argv, char *args[]) { struct sockaddr_in addr; int sd, port; port = htons(SERVER_PORT); /*--- create socket ---*/ sd = socket(PF_INET, SOCK_STREAM, 0); if ( sd < 0 ) panic("socket"); /*--- bind port/address to socket ---*/ memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = port; addr.sin_addr.s_addr = INADDR_ANY; /* any interface */ if ( bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 ) panic("bind"); /*--- make into listener with 10 slots ---*/ if ( listen(sd, 10) != 0 ) panic("listen") /*--- begin waiting for connections ---*/ else { int sd; pthread_t child; FILE *fp; while (1) /* process all incoming clients */ { sd = accept(sd, 0, 0); /* accept connection */ fp = fdopen(sd, "wr+"); /* convert into FILE* */ pthread_create(&child, 0, servlet, fp); /* start thread */ pthread_detach(child); /* don't track it */ } } } 
+4
source share
5 answers

You are obscuring the sd variable by passing an invalid socket to accept() , which causes it to crash immediately.

Most likely, it will return EBADF to signal a bad file descriptor. You would notice if you checked the return value in your code.

You should include additional compiler warnings to catch such things. With GCC, you can use the -Wshadow parameter to enable such a warning.

+5
source

You do not check the return value of the accept() call. Most likely, it returns an error.

+2
source

there is an override of the sd variable

 int sd; 
+2
source

Some problems:

1) You do not have enough comma to panic ("listen")

2) You declare "sd" twice (one on main () one on the other)

 #include <stdio.h> #include <pthread.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <netinet/ip.h> #define SERVER_PORT 30000 int main(int argv, char *args[]) { struct sockaddr_in addr; int sd, port; port = htons(SERVER_PORT); /*--- create socket ---*/ sd = socket(PF_INET, SOCK_STREAM, 0); /*--- bind port/address to socket ---*/ memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = port; addr.sin_addr.s_addr = INADDR_ANY; /* any interface */ bind(sd, (struct sockaddr*)&addr, sizeof(addr)); /*--- make into listener with 10 slots ---*/ listen(sd, 10); /*--- begin waiting for connections ---*/ pthread_t child; FILE *fp; while (1) /* process all incoming clients */ { printf("before accept\n"); sd = accept(sd, 0, 0); /* accept connection */ fp = fdopen(sd, "wr+"); /* convert into FILE* */ //pthread_create(&child, 0, servlet, fp); /* start thread */ //pthread_detach(child); /* don't track it */ printf("After accept\n"); } } 
0
source

their is overriding the sd variable.

 int sd; // at line 3 and 26 
0
source

All Articles