TCP connection check


 
Thread Tools Search this Thread
Top Forums Programming TCP connection check
# 1  
Old 06-18-2011
TCP connection check

Hi.
I am writing client - server application using TCP sockets.
I need some very basic functionality, namely: how to check if another "participant" of the connection is still present?
I want to handle situations, when client is gone, or server breaks down, etc.
# 2  
Old 06-18-2011
Quote:
Originally Posted by Shang
Hi.
I am writing client - server application using TCP sockets.
I need some very basic functionality, namely: how to check if another "participant" of the connection is still present?
I want to handle situations, when client is gone, or server breaks down, etc.
You must first be prepared that the server close unexpectedly the connection (e.g. due to a crash), that is:
  • read returns -1 and errno is set (e.g. ECONNRESET).
  • write cause SIGPIPE to be sent to your process. The classical way to handle this is to ignore SIGPIPE, in which case write() shall fail and set errno to EPIPE.
This is however usually not enough to cover all scenarios (e.g. when the connection get physically broken, well simulated by plugging the "internet cable" out). Depending on our context, you may need to have a heartbeat mechanism, that is a way to exchange message between client and server to verify the aliveness of the connection.

If applicable, you may want to check the keep alive feature offered by TCP (there are however some pitfalls), our roll your own heartbeat mechanism in the protocol.

HTH, Loïc
# 3  
Old 06-22-2011
I have already written a function which aim is to send some data (request) and then receive some data (response) from the server.
Code:
int send_request(int socket, request_s *request) {
    char *buffer;

    if ((buffer = (char *) malloc(MSG_SIZE)) == NULL) {
        ERR("malloc");
        return -1;
    }

    request_to_string(request, buffer);

    if (TEMP_FAILURE_RETRY(send(socket, buffer, MSG_SIZE, 0)) < 0) {
        free(buffer);
        ERR("send");
    }

    free(buffer);
    return 0;
}

/***
 * Communicates with server. First sends the request, then gets the response.
 * Returns:
 *             0    success
 *             -1    provided request is NULL
 *             -2    sending request failed
 *             -3    receiving response failed
 */
int communicate(int socket, request_s *request, response_s *response) {
    fd_set rfds;
    sigset_t mask, old_mask;
    char buffer[MSG_SIZE];

    if (request == NULL)
        return -1;
    else {
        if (send_request(socket, request) < 0) {
            printf("Sending request failed.\n");
            return -2;
        }
    }

    FD_ZERO(&rfds);
    FD_SET(socket, &rfds);
    sigemptyset(&mask);
    sigaddset(&mask, SIGINT);
    sigprocmask(SIG_BLOCK, &mask, &old_mask);

    if (pselect(socket + 1, &rfds, NULL, NULL, NULL, &old_mask) > 0) {
        if (FD_ISSET(socket, &rfds)) {
            if (TEMP_FAILURE_RETRY(recv(socket, (void *) buffer, MSG_SIZE, 0))
                    < 0)
                return -3;
            string_to_response(buffer, response);
            return 0;
        }
    }
    if (errno == EINTR) {
        request->type = MSG_EXIT_REQ;
        send_request(socket, request);
        TEMP_FAILURE_RETRY(close(socket));
        exit(EXIT_FAILURE);
    }
    return -4;
}

There is a very strange problem with it.
Even if server is down it behaves like nothing happened. send and recv don't return -1 and response does not changes.
Function communicate always returns 0.
Could you help me debugging it? I have no bloody idea what to do.

Last edited by Shang; 06-22-2011 at 06:47 PM..
# 4  
Old 06-22-2011
I'm suspicious of your TEMP_FAILURE_RETRY macro. What happens if you leave it out?

You should also check for <=0, not just <0, since 0 means the connection has closed too, albeit in an orderly way.
# 5  
Old 06-22-2011
I have tried <=, does not help.
I have also tried removing TEMP_FAILURE_RETRY, nothing changes.
More strange is fact, that when I execute this first time on the client side (of course during server breakdown) it behaves like nothing happened. When I execute this second time (I have some menu which handles different operations, every operation uses communicate to exchange messages with server), I have noticed that while executing:
Code:
    if (TEMP_FAILURE_RETRY(send(socket, buffer, MSG_SIZE, 0)) <= 0) {
        free(buffer);
        ERR("send");
    }

it kills my program! Now I am totally confused.
# 6  
Old 06-22-2011
I'm still suspicious of that macro. Macros complicated enough to be loops can't return values AFAIK. Can we see its contents?

You could also put fprintf(stderr, "debugging statements"); into your program. Print out return values and the like.

Does it tell you what signal killed it?
# 7  
Old 06-22-2011
ERR() already does it.
Code:
#define ERR(source) (fprintf(stderr,"%s:%d\n",__FILE__,__LINE__),\
                     perror(source),kill(0,SIGKILL),\
                          exit(EXIT_FAILURE))

It is hard to print any value, because this if (... <= 0) is never satisfied, instructions in brackets are never executed. I have only checked how many bytes was sent. First execution: 150, second execution: program quits.

How to check what killed program?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Tcp connection to Linux server fails

I am trying to send json messages to a port on a linux server from a remote server running a .net program. I have one implementation running with successful incoming messages to port 1514. I tried to replicate the same thing but just to another port but cannot get it to work as I get the following... (3 Replies)
Discussion started by: unienewbie
3 Replies

2. Solaris

many tcp connection in close-wait

Hi, I use solaris Unix . I find there is some problem in application and it generate many "close-wait" tcp connect and stay in the server . it is generate by process id 7740 root@XX # netstat -an | grep CLOSE_WAIT | wc -l 285 root@XX # netstat -an | grep CLOSE_WAIT 10.158.35.4.34805 ... (2 Replies)
Discussion started by: abcdef
2 Replies

3. IP Networking

false tcp connection

Why this happens? How to solve this? $netstat -na |grep 9325 tcp 0 0 127.0.0.1:9325 127.0.0.1:9325 ESTABLISHED When a client socket repeatedly tries to connect to an inactive(no server socket is listening on this port) local port,connect succeeds. ... (1 Reply)
Discussion started by: johnbach
1 Replies

4. UNIX for Dummies Questions & Answers

TCP failed connection attempts from netstat -s

Dear experts, I am seeing a lot of TCP failed connection attempts from "netstat -s" on one of our servers. How can I pin point what connection failed and what are the ports involved? Any tools/commands I can dig in deeper to diag. what went wrong on these "failed connection attempts"? ... (2 Replies)
Discussion started by: cache51
2 Replies

5. Solaris

How to kill the TCP ESTABLISHED connection in netstat

Hello, Actually there are some bugs in application which does not close the TCP connection to other server though CORBA. We need to kill that ESTABLISHED connections as new connection are not happeneing as the allocated ports were used and showing as ESTABLISHED Is there any... (4 Replies)
Discussion started by: GIC1986
4 Replies

6. Programming

close existing tcp connection in C

Hello. I would like to know how to close an existing tcp socket. I have read some stuff and learned how to create a socket and then close it but have not found anything about how to close an existing tcp socket created by another application. The situation is this: I have an ODBC server running and... (6 Replies)
Discussion started by: raidzero
6 Replies

7. Shell Programming and Scripting

Create a TCP/IP Connection

Hello, I am trying to write a script in Perl which will send some data from a UNIX Box to a windows box. I am planning to create a TCP/IP communication port for the same. How do I go about this? Kindly help. Regards, Garric (50 Replies)
Discussion started by: garric
50 Replies

8. UNIX for Advanced & Expert Users

about TCP connection

Hi Experts, need help about release or refresh TCP Connection: i have the sample like below : application log connection: 0500 ( 192.168.0.1:36053) 00919 2007/05/10 23:30:25 112 13 2007/05/10 23:30:25 1969/12/31 17:00:00 0500 ( 192.168.0.1:36054) 00920 2007/05/10 23:30:26 000 00... (3 Replies)
Discussion started by: bucci
3 Replies

9. UNIX for Dummies Questions & Answers

How to check the TCP/UDP port of a connection

Hi, Users are connecting thru a KCML Client to UNIX machine, and I want to know which TCP/UDP port that client uses? How can I check the port of a user logged in? Regards, Tayyab (2 Replies)
Discussion started by: tayyabq8
2 Replies

10. UNIX for Advanced & Expert Users

TCP/IP Connection getting slower...

Hi, We have developed a server program using TCP/IP Communication to communicate with another client program. After running for some days we find the TCP/IP connection from the server program is getting slower. What i mean to say is since the send() function in the server program (it is... (2 Replies)
Discussion started by: rajesh_puru
2 Replies
Login or Register to Ask a Question