Find the answer to your Linux question:
Results 1 to 7 of 7
Hi, I'm teaching myself socket programming and I've come across a weird problem with the code below. I can compile and run this code(as root) with no problems using ./program&. ...
  1. #1
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714

    server socket program

    Hi,
    I'm teaching myself socket programming and I've come across a weird problem with the code below. I can compile and run this code(as root) with no problems using ./program&. It produces the data for the client and everything is great until I kill the process and try to restart it again then...well the server program will run but won't produce any data(also nmap 127.0.0.1 doesn't show the server's port). The only way I can restart the server and get data is to close the terminal and reopen another and type ./program& again....I know this isn't normal any suggestions? Thank-you for any comments - Gerard4143

    Note the book I'm using is 'Unix Network Programming by W. Richard Stevens'


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <arpa/inet.h>
    #include <strings.h>
    #include <string.h>
    
    #define DEFAULT_PROTOCOL 0
    
    int main(int argc, char**argv)
    {
    	int connfd, listenfd;
    	char ch[] = "this is the server socket message!\n";
    	struct sockaddr_in servaddr;
    
    	listenfd = socket(AF_INET, SOCK_STREAM, DEFAULT_PROTOCOL);
    
    	bzero(&servaddr, sizeof(servaddr));
    	servaddr.sin_family = AF_INET;
    	servaddr.sin_port = htons(13);
    	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    
    	bind(listenfd, (const struct sockaddr*)&servaddr, sizeof(servaddr));
    
    	listen(listenfd, 5);
    
    	for(;;)
    	{
    		int i; 
    		connfd = accept(listenfd, NULL, NULL);
    
    		for (i = 0; i < 1000; ++i)
    			write(connfd, ch, strlen(ch));
    		close(connfd);
    	}
    	close(listenfd);
    	exit(EXIT_SUCCESS);
    }
    Make mine Arch Linux

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Why are you using port 13h? That is reserved to chargen services. For user applications you should be using ports >= 49152. From /etc/services:
    Code:
    # The Well Known Ports are those from 0 through 1023.
    # The Registered Ports are those from 1024 through 49151
    # The Dynamic and/or Private Ports are those from 49152 through 65535
    You are using a "Well Known Port" that are forbidden for anything other than an application that is replacing that service. This is also the likely reason why you have to be root to run this server.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Changed to port number to 50000 and still the behaviour continues – kill the server process and restart and no data plus nmap 127.0.0.1 doesn't show a service on port 50000....G4143

    Note /etc/services has no entry for port 50000 so it should be O.K.
    Make mine Arch Linux

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Well, since the system is obviously binding the port to the process tree, the shell being the parent of the process, I would suggest that you add a signal handler for SIGHUP, SIGINT, and SIGKILL at the least and exit normally when you apply one of those to the server. kill pid without any options will apply SIGKILL to the process, so the handler should be invoked. The signal handler can do stuff, like shutting down all the connections and sockets before exiting. I would advise that for normal server development in any case. You want the remote client to be informed when the server is shut down, and that is the only reliable way to do so.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Thanks for the reply Rubberman...I think I found the problem it was the client program(s) going into TIME_WAIT state upon exit....G4143

    This is my output for nestat -a | grep 50000 immediately after calling the client 4 times(client exits automatically after data receive). Now if I wait until the TIME_WAIT state expires the server can be terminated and started again without incident.

    Code:
    tcp        0      0 *:50000                     *:*                         LISTEN      
    tcp        0      0 localhost:50000             localhost:50268             TIME_WAIT   
    tcp        0      0 localhost:50000             localhost:50267             TIME_WAIT   
    tcp        0      0 localhost:50000             localhost:50270             TIME_WAIT   
    tcp        0      0 localhost:50000             localhost:50269             TIME_WAIT
    Make mine Arch Linux

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Quote Originally Posted by gerard4143 View Post
    Thanks for the reply Rubberman...I think I found the problem it was the client program(s) going into TIME_WAIT state upon exit....G4143

    This is my output for nestat -a | grep 50000 immediately after calling the client 4 times(client exits automatically after data receive). Now if I wait until the TIME_WAIT state expires the server can be terminated and started again without incident.
    That is a good example why a server should perform diligent cleanup on exit, to keep poorly written client processes from holding on to resources when they shouldn't. Self-defensive programming is a necessity. That, input validation, and error checking can take up to 80% of an application server's code, while only 20% or so might be code that actually does something useful! Caveat Programmer! Given how that buffer overruns and improper error/exception handling can lead to security vulnerabilities, this is not something to give short shrift.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  7. #7
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Quote Originally Posted by Rubberman View Post
    That is a good example why a server should perform diligent cleanup on exit, to keep poorly written client processes from holding on to resources when they shouldn't. Self-defensive programming is a necessity. That, input validation, and error checking can take up to 80% of an application server's code, while only 20% or so might be code that actually does something useful! Caveat Programmer! Given how that buffer overruns and improper error/exception handling can lead to security vulnerabilities, this is not something to give short shrift.
    Are you saying that 'TIME_WAIT state' isn't normal? That it can be avoided...Gerard4143
    Make mine Arch Linux

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...