Closed socket but we can still write

This is a discussion on Closed socket but we can still write within the Networking/Device Communication forums, part of the General Programming Boards category; Why after closing a socket using the statement close(sd) we can still write on it? Another question is how can ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Greece
    Posts
    52

    Closed socket but we can still write

    Why after closing a socket using the statement close(sd) we can still write on it?
    Another question is how can I be sure that a socket is closed? How do I check it?
    I quote the following code that I can't understand how it works the part in red.
    Also, I have some questions more about how I can improve the code and understand some details that seem unclear to me.
    Sorry, if my questions seem silly to you, but I am new to socket programming.

    Code:
    ...
    sd = socket(AF_UNIX, SOCK_STREAM, 0);
    if(sd<0) {printf("Socket failure\n"); exit(1);}
    sockaddr.sa_family=AF_UNIX;
    strcpy(sockaddr.sa_data,"sock");			// Is strncpy a better function to use here?
    addrlen=strlen(sockaddr.sa_data)+sizeof(sockaddr.sa_family);
    unlink("sock");
    if(bind(sd, &sockaddr, addrlen)< 0)
    {
           	exit(1); 
    }
    listen(sd, 3);   // I have set the queue = 3. Does it mean that I can do up to 3 forks at the same time?
    for(;;)
    {
    	if((ns = accept(sd, &sockaddr, &addrlen))<0)
    	{
    		printf("Server accept failure\n");
    		exit(1);
    	}
    	printf("Connect accepted to client\n");
    	pid=fork();
    	if (pid==0)
    	{
    		read(ns,bufin,10);
    		printf("Client sends something and I print the bufin: &#37;s \n",bufin);
    		close(sd);				//Close socket sd
    		printf("I am sending my messages to the client\n");
                     // this is successful!! why??
    		write(ns,buf,100);			//Write the messages to the socket
    		close(ns);
    		exit(0);
    	}
    	else close(ns);				//close parent's socket
    }
    Last edited by myle; 11-11-2007 at 05:23 PM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Why should closing the listening socket automatically close the connection socket?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Greece
    Posts
    52
    So, we can't listen to anything because we have closed the listening connection and we can still write something to the socket because there is still the connection that was established by the accept function?

    Thanks again, I really appreciate that you are taking the time to reply to me.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I was just about to ask almost the same question.

    After closing a socket, if I try to bind another on the same port right away, I get a EADDRINUSE error.

    After reading a lot of man pages, I thought it might be because of the SO_LINGER sockoption, but when I checked the value of SO_LINGER it was 0 (disabled).

    Is there a way (in Linux) to actually close a socket when you call close()?

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    strcpy(sockaddr.sa_data,"sock"); // Is strncpy a better function to use here?
    Yes u could use that function as well. At the same time there is nothing wrong in using strcpy as well. Both are ANSI standard functions.

    Code:
    // I have set the queue = 3. Does it mean that I can do up to 3 forks at the same time?
    listen(sd, 3);
    What this does is, it sit near the socket listing for connection. At a time server can accept one connection. If there are multiple connection coming at the same time. Sever has to Q them before they can be accepted. So listen is the function which you specify to the kernel saying that at the most you can Q up to 3 connection. Any more connection which cannot be Q'ed will be dropped.


    Code:
     // this is successful!! why??
    write(ns,buf,100);
    That was a successful write because when the server get the connection it will create a new socket and assign that new socket to it leaving the main socket for accepting more connection. So you got the connection created a new socket and assigned the accept connection to the new socket and the closed the main socket. Closing the main socket will never effect the newly created socket. Closing the main socket would normally effect in not taking any more connection.

    >>Is there a way (in Linux) to actually close a socket when you call close()?
    Try shutdown()

    ssharish

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by ssharish2005 View Post
    >>Is there a way (in Linux) to actually close a socket when you call close()?
    Try shutdown()

    ssharish
    Thanks. I might try that, but I seem to have fixed my problem by setting the SO_REUSEADDR sockopt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking socket connection problem
    By cbalu in forum Linux Programming
    Replies: 25
    Last Post: 06-03-2009, 03:15 AM
  2. Problem with simple socket client/server program
    By spencer88 in forum C Programming
    Replies: 6
    Last Post: 05-06-2009, 12:05 AM
  3. socket program help
    By mhetfield in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-02-2007, 07:02 AM
  4. Socket class OOD
    By lord mazdak in forum C++ Programming
    Replies: 6
    Last Post: 12-17-2005, 01:11 AM
  5. write in c
    By PutoAmo in forum C Programming
    Replies: 6
    Last Post: 04-03-2002, 07:53 PM

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