![]() |
| | #1 |
| Registered User Join Date: Apr 2007 Location: Greece
Posts: 52
| Closed socket but we can still write 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 | |
| | #2 |
| Cat without Hat 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 | |
| | #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 | |
| | #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 | |
| | #5 |
| Protocol Test Engineer Join Date: Sep 2005 Location: fseek(UK)
Posts: 1,324
| Code: strcpy(sockaddr.sa_data,"sock"); // Is strncpy a better function to use here? 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); Code: // this is successful!! why?? write(ns,buf,100); >>Is there a way (in Linux) to actually close a socket when you call close()? Try shutdown() ssharish |
| ssharish2005 is offline | |
| | #6 |
| and the hat of sweating Join Date: Aug 2007 Location: Toronto, ON
Posts: 3,282
| |
| cpjust is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |