Kitcha
Kitcha

Reputation: 1731

Socket send () stuck in C

I'm trying to connect two machines, say machine A and B. I'm trying to send TCP message from A to B (One way). In normal scenario this works fine. When the communication is smooth, if the socket in B is closed, send() from A is stuck forever. And it puts process into Zombie state. I have socket in blocked mode in machine A. Below is the code that stuck forever.

           if (send (txSock,&txSockbuf,sizeof(sockstruct),0) == -1) {
                printf ("Error in sending the socket Data\n");
                            }
            else {
                printf ("The SENT String is %s \n",sock_buf);
            }

How do I find if the other side socket is closed?? What does send return if the destination socket is closed?? Would select be helpful.

Upvotes: 4

Views: 2127

Answers (1)

Greg Hewgill
Greg Hewgill

Reputation: 993173

A process in the "zombie" state means that it has already exited, but its parent has not yet read its return code. What's probably happening is that your process is receiving a SIGPIPE signal (this is what you'll get by default when you write to a closed socket), your program has already terminated, but the zombie state hasn't yet been resolved.

This related question gives more information about SIGPIPE and how to handle it: SIGPIPE, Broken pipe

Upvotes: 7

Related Questions