C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 11-11-2007, 05:17 PM   #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: %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.
myle is offline   Reply With Quote
Old 11-11-2007, 05:24 PM   #2
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,492
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
CornedBee is offline   Reply With Quote
Old 11-11-2007, 05:58 PM   #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.
myle is offline   Reply With Quote
Old 11-21-2007, 01:53 PM   #4
and the hat of sweating
 
Join Date: Aug 2007
Location: Toronto, ON
Posts: 3,282
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()?
cpjust is offline   Reply With Quote
Old 11-22-2007, 10:16 PM   #5
Protocol Test Engineer
 
ssharish2005's Avatar
 
Join Date: Sep 2005
Location: fseek(UK)
Posts: 1,324
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
ssharish2005 is offline   Reply With Quote
Old 11-23-2007, 09:48 AM   #6
and the hat of sweating
 
Join Date: Aug 2007
Location: Toronto, ON
Posts: 3,282
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.
cpjust is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:43 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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