Thread: Basic socket programming

  1. #1
    Registered User
    Join Date
    Nov 2009

    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:

    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");
         	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");
         	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)  {
                 		exit(0);                                         <------ line XXX
             	else close(newsockfd);
    	return 0; /* we never get here */
    void error(char *msg) {
    void dostuff(int sock) {
       	int n;
       	char 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:

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

  2. #2
    Registered User
    Join Date
    Sep 2007
    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
    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, 01:04 PM