Why does this call to accept hang this way?

This is a discussion on Why does this call to accept hang this way? within the Networking/Device Communication forums, part of the General Programming Boards category; I sure thought I was familiar with the Sockets API but I dont understand why the server code doesn't print ...

  1. #1
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265

    Why does this call to accept hang this way?

    I sure thought I was familiar with the Sockets API but I dont understand why the server code doesn't print "got a connection from [ ip addr && port]" string until something is written on the socket from the client. I've used the library for a few things in the past but perhaps there is something I'm missing here.

    server.c
    Code:
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <strings.h>
    #include <string.h>
    #include <errno.h>
    
    
    void error(const char *err_str){
    	printf("%s: %s\n",err_str, strerror(errno));
    	exit(-1);
    }
    
    int main(int argc, char **argv){
    
    	if(argc != 2){
    		printf("Usage: %s <port>\n", argv[0]);
    		exit(-1);
    	}
    
    	int listen_fd, accept_fd, nread, port;
    	socklen_t length;
    	char buffer[32], ipv4[INET_ADDRSTRLEN];
    	struct sockaddr_in server, client;
    
    	if((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) <= 0)
    		error("socket()");
    
    	bzero(&server, sizeof(server));
    	bzero(&client, sizeof(client));
    
    
    	server.sin_family = AF_INET;
    	server.sin_port = htons(atoi(argv[1]));
    	server.sin_addr.s_addr = htonl(INADDR_ANY);
    
    	if(bind(listen_fd, (struct sockaddr *)&server, sizeof(server)) == -1)
    		error("bind()");
    
    	listen(listen_fd, 10);
    
    	for(;;){
    		length = sizeof(client);
    
    		accept_fd = accept(listen_fd,(struct sockaddr *)&client, &length);
    
    		inet_ntop(AF_INET, &client.sin_addr, ipv4, INET_ADDRSTRLEN);
    		port = ntohs(client.sin_port);
    
    		printf("*************** Got a connection from %s %u **********************",ipv4,port);
    
    		while(nread = read(accept_fd, buffer, sizeof(buffer))){
    			buffer[nread] = 0;
    			fputs(buffer,stdout);
    		}
    		close(accept_fd);
    		fputs("**************** Connection closed *********************", stdout);
    	}
    
    	return 0;
    }
    client.c
    Code:
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <strings.h>
    #include <string.h>
    #include <errno.h>
    
    void error(const char *err_str){
    	printf("%s: %s\n", err_str, strerror(errno));
    	exit(-1);
    }
    
    int main(int argc, char **argv){
    
    	if(argc != 3){
    		printf("Usage: %s <ip address> <port number>\n",argv[0]);
    		exit(-1);
    	}
    
    	int sock_fd, nread, nleft, nwritten;
    	char buffer[32];
    	struct sockaddr_in server;
    
    	if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) <= 0)
    		error("socket()");
    
    	bzero(&server, sizeof(server));
    	server.sin_family = AF_INET;
    	server.sin_port = htons(atoi(argv[2]));
    
    	inet_pton(AF_INET, argv[1], &server.sin_addr);
    
    	if(connect(sock_fd, (struct sockaddr *)&server, sizeof(server)) < 0)
    		error("connect()");
    
    	fputs("************ Got a connection from remote host *************\n", stdout);
    
    
    	fgets(buffer, sizeof(buffer), stdin);
    	while(nwritten = write(sock_fd, buffer, strlen(buffer))){
    		bzero(&buffer, sizeof(buffer));
    		fgets(buffer,sizeof(buffer), stdin);
    	}
    
    	close(sock_fd);
    
    	return 0;
    
    }

  2. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    So I added these few printing lines as sort of a debugging measure and when I do, now it seems like my calls to printf and fputs hang. What the heck? how can that be?


    Code:
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <strings.h>
    #include <string.h>
    #include <errno.h>
    
    
    void error(const char *err_str){
    	printf("%s: %s\n",err_str, strerror(errno));
    	exit(-1);
    }
    
    int main(int argc, char **argv){
    
    	if(argc != 2){
    		printf("Usage: %s <port>\n", argv[0]);
    		exit(-1);
    	}
    
    	int listen_fd, accept_fd, nread, port;
    	socklen_t length;
    	char buffer[32], ipv4[INET_ADDRSTRLEN];
    	struct sockaddr_in server, client;
    
    	if((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) <= 0)
    		error("socket()");
    fputs("socket()\n",stdout);
    	bzero(&server, sizeof(server));
    	bzero(&client, sizeof(client));
    
    
    	server.sin_family = AF_INET;
    	server.sin_port = htons(atoi(argv[1]));
    	server.sin_addr.s_addr = htonl(INADDR_ANY);
    
    	if(bind(listen_fd, (struct sockaddr *)&server, sizeof(server)) == -1)
    		error("bind()");
    fputs("bind()\n",stdout);
    	listen(listen_fd, 10);
    fputs("listen()\n",stdout);	
    	length = sizeof(client);
    	for(;;){
    
    
    		accept_fd = accept(listen_fd,(struct sockaddr *)&client, &length);
    fputs("accept()\n",stdout);
    
    		inet_ntop(AF_INET, &client.sin_addr, ipv4, sizeof(ipv4));
    fputs("inet_ntop()\n",stdout);
    		port = ntohs(client.sin_port);
    fputs("ntohs()\n",stdout);
    		printf("*************** Got a connection from %s %u **********************",ipv4,port);
    fputs("printf()\n",stdout);
    		while(nread = read(accept_fd, buffer, sizeof(buffer))){
    			buffer[nread] = 0;
    			fputs(buffer,stdout);
    		}
    		close(accept_fd);
    fputs("close()\n",stdout);
    		fputs("**************** Connection closed *********************", stdout);
    	}
    
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    You might be encountering output stream buffering. Either call fflush(stdout) after each printf/fputs or switch to using stderr which is unbuffered by default.

  4. #4
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    I found that re-writing the code solved my problem. Not sure what went wrong here. But those few printf/fputs statements on the server would only occur after my calls to write in the client code. Never had that happen.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Syscal View Post
    I found that re-writing the code solved my problem. Not sure what went wrong here. But those few printf/fputs statements on the server would only occur after my calls to write in the client code. Never had that happen.
    If you are just writing diagnostics to a console screen, use puts() ... primative but very fast.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. minix system call pls help for project
    By porvas in forum Linux Programming
    Replies: 2
    Last Post: 06-14-2009, 02:40 AM
  2. How to get RSSI value, send to sensor, sensor receive package, repackage it?
    By techissue2008 in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-04-2009, 09:13 AM
  3. Wrong accept() call?
    By Nephiroth in forum Networking/Device Communication
    Replies: 1
    Last Post: 02-15-2006, 11:42 PM
  4. Iterative Tree Traversal using a stack
    By BigDaddyDrew in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2003, 04:44 PM
  5. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21