Basic socket programming

This is a discussion on Basic socket programming within the C Programming forums, part of the General Programming Boards category; Sorry for pasting something as basic as this, but I don't seem to be able to figure this out. I'm ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    111

    Basic socket programming

    Sorry for pasting something as basic as this, but I don't seem to be able to figure this out. I'm using this example, pasted from an online tutorial, for a basic socket server application:

    Code:
    void dostuff(int);
    void error(char *);
    
    int main(int argc, char *argv[]) {
         	int unsigned sockfd, newsockfd, portno, clilen, pid;
         	struct sockaddr_in serv_addr, cli_addr;
    	
         	if (argc < 2) {
             	fprintf(stderr,"ERROR, no port provided\n");
             	exit(1);
         	}
    
         	sockfd = socket(AF_INET, SOCK_STREAM, 0);
    
         	if (sockfd < 0) {
         	   error("ERROR opening socket");
    	}
    
         	bzero((char *) &serv_addr, sizeof(serv_addr));
         	portno = atoi(argv[1]);
         	serv_addr.sin_family = AF_INET;
         	serv_addr.sin_addr.s_addr = INADDR_ANY;
         	serv_addr.sin_port = htons(portno);
         
    	if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
                  	error("ERROR on binding");
    	}     
    	
    	listen(sockfd,5);
         	clilen = sizeof(cli_addr);
         
    	while (1) {
             	newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
             	if (newsockfd < 0) {
                 		error("ERROR on accept");
    		}
             	pid = fork();
             	if (pid < 0) {
                 		error("ERROR on fork");
    		}
             	if (pid == 0)  {
                 		close(sockfd);
                 		dostuff(newsockfd);
                 		exit(0);                                         <------ line XXX
             	}
             	else close(newsockfd);
         	}
         
    	return 0; /* we never get here */
    }
    
    void error(char *msg) {
        	perror(msg);
        	exit(1);
    }
    
    void dostuff(int sock) {
       	int n;
       	char buffer[256];
       	   
       	bzero(buffer,256);
       	n = read(sock,buffer,255);
       	if (n < 0) {
    		error("ERROR reading from socket");
    	}
       	printf("Here is the message: %s\n",buffer);
       	n = write(sock,"I got your message",18);
       	if (n < 0) {
    		error("ERROR writing to socket");
    	}
    }
    The "problem" with this code is that, when I connect to the server (I'm just telnetting to the port), after sending a message to the server, the client just shuts down ("Connection closed by foreign host") after receiving the "I got your message" message from the server. I've tried commenting out line XXX (which reads "exit(0)", but that just results in the server returning this error:

    Code:
    ERROR reading from socket: Bad file descriptor
    Any ideas/hints/suggestions?
    Last edited by cnewbie1; 03-28-2010 at 04:00 PM.

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    979
    The server is exiting. You've forked off a child, had it read from the client, write to the client, then exit. You absolutely want the exit(0) (perhaps _exit(0), really) in the child, because otherwise your child will start acting like the parent. But if you want the server to continue talking, you have to continue communicating with the client! You're kind of doing a "one and done" thing.

    For a very quick test, you can do
    Code:
    while(1) dostuff(newsockfd);
    in the child. That should allow you to communicate back and forth, but the server will continue trying to talk even if the client shuts down. The point, though, is that you can't just call read() once and expect the server to continue reading as long as the client is writing. You must actively keep the conversation going.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 29
    Last Post: 10-22-2009, 11:01 AM
  2. Function call from another .c module
    By Ali.B in forum C Programming
    Replies: 14
    Last Post: 08-03-2009, 11:45 AM
  3. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  4. a few basic things...
    By crepincdotcom in forum C Programming
    Replies: 9
    Last Post: 02-25-2004, 12:04 PM

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