Thread: socket programming question, closing sockets...

  1. #1
    Registered User ursula's Avatar
    Join Date
    May 2009
    Location
    Madrid, Spain
    Posts
    11

    socket programming question, closing sockets...

    I have been provided a client and server code and asked to run them. THe server just echos what the client tells him. I need to fix whats wrong with the code. I have noticed that if i close the client and then the server the socket opened by the client remains open

    This is what netstat tells me when both have been closed:
    tcp 0 0 192.168.2.7:3000 192.168.2.7:52186 TIME_WAIT

    This is the connection for the client.

    I dont know if im reading this right but i have used tcpdump and noticed that the server tells teh client he is closing and the client acks it, shouldnt the client close the socket then?

    This is the client code:

    Code:
            char	buf[LINELEN+1];		/* buffer for one line of text	*/
    	int	s, n;			/* socket descriptor, read count*/
    	int	outchars, inchars;	/* characters sent and received	*/
    
    	s = connectTCP ("localhost", "echo");
    
    	/* don't even think of using other function than fgets for this */
    	while (fgets(buf, LINELEN+1, stdin)) 
           {
    		buf[LINELEN] = '\0';	/* insure line null-terminated	*/
    		outchars = strlen(buf);
    		(void) write(s, buf, outchars);
    		/* read it back */
    		for (inchars = 0; inchars < outchars; inchars+=n ) 
                   {
    		  n = read(s, &buf[inchars], outchars - inchars);
    			if (n < 0)
    				errexit("socket read failed: %s\n",
    					strerror(errno));
    		}
    		fputs(buf, stdout);
    	}


    this is how connectTCP configures the socket:

    Code:
    	struct hostent	*phe;	/* pointer to host information entry	*/
    	struct servent	*pse;	/* pointer to service information entry	*/
    	struct protoent *ppe;	/* pointer to protocol information entry*/
    	struct sockaddr_in sin;	/* an Internet endpoint address		*/
    	int	s, type;	/* socket descriptor and socket type	*/
    //	int no_nagle= 1;
    
    	memset(&sin, 0, sizeof(sin));
    	sin.sin_family = AF_INET;
    
        /* Map service name to port number */
    	if ( pse = getservbyname(service, transport) )
    		sin.sin_port = pse->s_port;
    	else if ( (sin.sin_port = htons((u_short)atoi(service))) == 0 )
    		errexit("can't get \"%s\" service entry\n", service);
    
        /* Map host name to IP address, allowing for dotted decimal */
    	if ( phe = gethostbyname(host) )
    		memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
    	else if ( (sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE )
    		errexit("can't get \"%s\" host entry\n", host);
    
        /* Map transport protocol name to protocol number */
    	if ( (ppe = getprotobyname(transport)) == 0)
    		errexit("can't get \"%s\" protocol entry\n", transport);
    
        /* Use protocol to choose a socket type */
    	if (strcmp(transport, "udp") == 0)
    		type = SOCK_DGRAM;
    	else
    		type = SOCK_STREAM;
    
        /* Allocate a socket */
    	s = socket(PF_INET, type, ppe->p_proto);
    	if (s < 0)
    		errexit("can't create socket: %s\n", strerror(errno));
    
    //	if (setsockopt(s,SOL_TCP,TCP_NODELAY,&no_nagle, sizeof(no_nagle)) != 0)
    //		errexit("setsockopt: no pude deshabilitar Nagle");
    
        /* Connect the socket */
    	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
    		errexit("can't connect to %s.%s: %s\n", host, service,
    			strerror(errno));
    	return s;
    tx!

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Using Google and searching for sockets TIME_WAIT brings up this link in the first result, which explains things pretty thoroughly.

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    The link is what you want. That said, you should nonetheless ensure that you are closing all of your sockets, files, etc. in your code.

    Also,
    Code:
    buf[LINELEN] = '\0';	/* insure line null-terminated	*/
    fgets() will null terminate the buffer. That will chop off the last byte if the buffer actually filled.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket Question
    By BENCHMARKMAN in forum C Programming
    Replies: 15
    Last Post: 03-12-2008, 09:57 PM
  2. Problem with POSIX sockets
    By MCRonald in forum C Programming
    Replies: 2
    Last Post: 07-23-2006, 10:41 AM
  3. problem closing socket
    By Wisefool in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-29-2003, 12:19 PM
  4. Hi all!, Another question about socket's & NetBios
    By Pandora in forum Windows Programming
    Replies: 9
    Last Post: 03-19-2003, 08:10 AM
  5. Question about socket programming
    By Bill in forum C Programming
    Replies: 3
    Last Post: 09-05-2001, 03:49 AM