Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 857
  • Last Modified:

Reconnection causes SIGPIPE signal

I have an infinite for loop where a client app is consistently requesting data from a server, using the GNU C socket library.  (It's all happening locally though.  I'm using my localhost as a server, since this is just a test program.)

The basic routine is:

initialize client socket

for (;;) {
     connect to server
     send request
     recv data
}

The data is received fine, and everything works normally when the above is done ONCE.  But if I do this in a loop, I get SIGPIPE signals after two iterations.  This causes the program to terminate.  I can pass MSG_NOSIGNAL to the send() function, but this just generates various broken pipe error messages.  The client continues to receive data normally, but the SIGPIPE messages keep happening.  

I know that a SIGPIPE means that I'm writing to a socket, but nothing is reading from it.  But this doesn't make sense, because the server clearly is reading from it, since I'm successfully getting the data.  Does anyone know what might be happening here?  

0
chsalvia
Asked:
chsalvia
  • 2
2 Solutions
 
Kent OlsenData Warehouse Architect / DBACommented:

After the receive, close the connection.

Or test that an active connection doesn't exist before you issue the connect().


Kent
0
 
_iskywalker_Commented:
yes, dont open too much connections, make the opning connection out from the intereaction
0
 
avsrivastavaCommented:
One possibility is that the pipe buffer gets full before the client reads any data, which raises the SIGPIPE message.
You anyways read the data correctly because once the client reads the data, the server can read more.

Solution:
Don't send the MSG_NOSIGNAL, catch the SIGPIPE signal.
When you get the message, sleep for a random amount of time(could be predetermined as well).
When the program wakes, try to write again starting from the position where you left(the return value of write tells you till which byte you managed).


0
 
avsrivastavaCommented:
The pipe by default on linux kernel is 64k, so if the data you are writing is only bytes this would not be the problem though.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

KuppingerCole Reviews AlgoSec in Executive Report

Leading analyst firm, KuppingerCole reviews AlgoSec's Security Policy Management Solution, and the security challenges faced by companies today in their Executive View report.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now