Socket Programming in C

  3 posts   Feedicon  
Replies: 2 - Last Post: January 25, 2010 05:52
by: Behzad Asadi Isfahani
showing 1 - 3 of 3
 
Posted: January 14, 2010 09:44 by Behzad Asadi Isfahani
Hello All;
I need to know how can understand when the client is disconnected from the server?


Thanks;
************************
Regards
Behzad Asadi Isfahani
************************
 
Posted: January 21, 2010 02:51 by B. Gian James
Hi Behzad,
There are a couple of ways that you can determine that a client is no longer connected to your server.
First, any
 send() or sendto(), recv(), recvfrom() 
to/from the socket fd will return zero. That can tell you something is up with the socket descriptor. You can also catch the SIGPIPE signal, which typically indicates that you can't write to a socket or file descriptor anymore. Further, you can use
setsockopt(), getsockopt() 
along with
 fcntl() 

and
ioctl() 
calls to get and set socket options. Some of the socket options allow you to get the status of a socket descriptor in real-time, like SO_ERROR, or passing a receive/send timeout that you set previously.

man getsockopt
to see a complete list.

Hope this helps.
Regards,
-gian
 
Posted: January 25, 2010 05:52 by Behzad Asadi Isfahani
Dear B. Gian James

************************************
Thanks a lot for your time, first.
************************************

Your info was very good.

Did you mean, I have to set O_NONBLOCK for a fd?
I'll tried to test your idea but I've had a problem with it, can you give me an example to show how I have to use them,please?

You know,functions to send and receive are in block mode and block the program till they can done,I've done it by select() function and gave it a timeout.

I'd like to know which way is The Best ?
( setsockopt(),getsockopt(),fcntl(),ioctl() ) or ( select() )?

I have a server and 5 clients.
When I'm testing return value of accept() I can't accept all clients, but I can accept all clients If I don't test it.
I need to keep all accepted client fd's, If I write it into a while() so I can't keep them.
Do you have an Idea for keep all accepted clients fd's?


+++++++++++++++++
Thanks in advance
Best Regard
Behzad Asadi Isfahani
+++++++++++++++++
Replies: 2 - Last Post: January 25, 2010 05:52
by: Behzad Asadi Isfahani
  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2010, Oracle Corporation and/or its affiliates
(revision 20120518.3c65429)
 
 
Close
loading
Please Confirm
Close