LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-04-2011, 08:06 AM   #1
assyrian1
LQ Newbie
 
Registered: Jun 2005
Location: sydney
Distribution: Fedora Core 7
Posts: 17

Rep: Reputation: 0
TCP server not detecting broken connection


I am attempting to write a server application in C on a linux machine which listens for TCP connections and transfers data. I am trying to detect on the server side when the connection is broken. The closest thing that I got to work was looking at the return value from sending data. For example the server’s job is to mainly read data from the socket but to test if the connection is still up the server sends data periodically back to the client. I look at the return value from send() to determine if the connection is broken e.g.
Code:
int ret = send(session->clientSocket, &data[sentCnt], count - sentCnt, MSG_NOSIGNAL);
I found that this does not immediately return an error when the connection is broken. The reason for this is because even though the connection is broken send() is still successful because it is able to put it on the network buffer. To fix the issue I did the following things;

Code:
  //set send timeout
    struct timeval timeout;
    timeout.tv_sec = 4;
    timeout.tv_usec = 0;

    //apply send timeout socket options
    setsockopt(tcp->socket, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(struct timeval));

    //set output buffer size so that send blocks
    int buffersize = 2;
    setsockopt(tcp->socket, SOL_SOCKET, SO_SNDBUF, &buffersize, 4);
Setting the timeout and the buffer should ensure that the socket will timeout if the data sent is more than 2 bytes and the connection is broken.

I have tested this and it worked i.e. when i pull the plug from the modem the server detects that the conneciton is broken fairly quickly.

The server runs as a virtual machine somewhere in the US and the client which im using to test is in Sydney Australia. I tested the client on my Telstra cable connection and it worked. I also tested on my Optus 3G wireless broadband connection and it also worked. The problem is however when I tested on my Telstra 3G wireless broadband and it DID NOT work. The server was happily thinking that it was sending data to the client when the client was well and truly disconnected from the internet. I had swaped modems and simcards around isolated the problem down to the network provider i.e. works on Telstra cable and Optus 3G but not on Telstra 3G.

How can the server think that it’s successfully sent data to the client which is not connected to the internet, I thought TCP was in end to end protocol.

Does anyone have any idea why this occurs?
 
Old 07-04-2011, 02:00 PM   #2
aysheaia
LQ Newbie
 
Registered: Jun 2011
Distribution: Ubuntu
Posts: 26

Rep: Reputation: Disabled
That's not a response to your question, but the need you describe can be satisfied with TCP keepalive activated on client side.
see man 7 tcp
=> SO_KEEPALIVE socket option for SOL_SOCKET level
=> TCP_KEEPIDLE, TCP_KEEPINTVL and TCP_KEEPCNT socket options for IPPROTO_TCP level (Linux specific)
 
0 members found this post helpful.
Old 07-04-2011, 04:48 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Moved: This thread is more suitable in Programming and has been moved accordingly to help your question get the exposure it deserves.
 
1 members found this post helpful.
Old 07-04-2011, 11:52 PM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Here's how I detect a closed connection when writing a TCP server. It always works for me.

First, I make every socket non-blocking, even the one I'll be using for listen()ing:
Code:
#include <sys/socket.h>   /* for socket(), PF_INET, and SOCK_STREAM */
#include <unistd.h>       /* for fcntl() */
#include <fcntl.h>        /* for fcntl() */

int fcntl_flags;
int socket_fd;

socket_fd=socket(PF_INET,SOCK_STREAM,0);

if(socket_fd==-1)
{
  // error handling here
}

fcntl_flags=fcntl(socket_fd,F_GETFL,0);

if(fcntl_flags==-1)
{
  // error handling here
}

if(fcntl(socket_fd,F_SETFL,fcntl_flags|O_NONBLOCK)==-1)
{
  // error handling here
}
Then, when I've done the listen(), I go into a select() loop, waiting for activity. I include the read file descriptor for all sockets, including the listening one, even for any socket on which I might not want to read, but just write. I include the write file descriptor for all sockets on which I actually have data to send, but no others.

If the result value from select() is -1 and errn is EINTR, I just go back and do another select().

If, upon return from select(), the listening socket seems to be ready to read, it's really ready to accept.

If, upon return from select(), any other socket seems to be ready to read, I read from it. Even if only one byte. If the number of bytes I get back (as the result value from calling function read() or recv()) is -1 and errno is EINTR, I ignore this event and go back to do another select(). But if the number of bytes I get back is 0, then the connection is broken.

That's all there is to it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
TCP Stack and Netstat: Could having lots of TIME_WAIT connection slow down the server helptonewbie Linux - Networking 7 06-20-2012 01:48 PM
X connection to localhost:10.0 broken (explicit kill or server shutdown) JATA01 Linux - Server 2 03-03-2011 05:34 AM
connection refused, tcp betwee client and server on different pc on same lan JoeyWong Linux - Networking 1 05-14-2006 02:14 AM
tcp/ip multithreaded server stops accepting connection debjyotidas Linux - Networking 2 07-23-2004 12:51 AM
Gdk-ERROR **: X connection to :0.0 broken (explicit kill or server shutdown) stelmed Slackware 3 02-02-2004 06:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration