Thread: Solving server socket's disconnection in c

  1. #1
    Registered User
    Join Date
    Aug 2018
    Posts
    1

    Solving server socket's disconnection in c

    Hi,

    I am writing client/server program using sockets in c. I need to solve following problem: client should detect disconnection with server and when it happens, try to connect another one. I have been looking for materials how socket disconnection works and I found out that there is not direct way to detect it. I know that if one side tries to make write on closed socket file descriptor, it will get SIGPIPE and if it tries to read data from it, read will return 0. The only solution I have found is that I will have background thread in client program which will connect server (who will accept this connection and also run thread for handling requests) and try to get data from server in busy waiting, as long as it read more than 0 byte, I know that connection is OK. If it reads 0 byte, than I will know that something bad happened and need to connect another server. Is there other better solutions for solving this problem? Or what kind of solution is mine?

  2. #2
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    85
    I once wrote a web-server (which is a long story) and used epoll. One of the functions I used was epoll_ctl(). Through this function one can determine for example that the client is no longer connected (EPOLLHUP).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Server socket closes after client disconnection
    By netpumber in forum C Programming
    Replies: 2
    Last Post: 06-21-2017, 01:47 PM
  2. BSD Socket Server Problem
    By ma_mazmaz in forum C Programming
    Replies: 3
    Last Post: 01-26-2009, 01:03 PM
  3. Socket Server
    By carrotcake1029 in forum Windows Programming
    Replies: 2
    Last Post: 07-21-2008, 11:46 AM
  4. Reset Socket w/o Disconnection :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 12:16 AM
  5. socket, udp,Halflife Server
    By Tolpan in forum C Programming
    Replies: 2
    Last Post: 06-21-2002, 09:02 AM

Tags for this Thread