4

I am new to socket programming, I am trying to send some packets to server using the send() function in C++, but I am always getting a 'Broken pipe' error while sending packets to the server. Could you please help me for the below points?

  1. When the send() function returns the "Broken pipe" errors?
  2. What are the causes for "Broken pipe" errors in socket programming?
  3. What will be the solution for "Broken pipe" error from the send() function?

Note: I am using named socket to communicate between client and server.

user207421
  • 305,947
  • 44
  • 307
  • 483
K.H.Nagaradder
  • 71
  • 1
  • 1
  • 5

2 Answers2

8
  1. When send() returns the "Broken pipe" errors

When you have written to a connection that has already been closed by the peer.

  1. What are the causes for "Broken pipe" errors in socket programming

Writing to a connection that has already been closed by the peer.

  1. What will be the solution for "Broken pipe" error from send() function.

Don't write to a connection that has already been closed by the peer.

It usually indicates that you have committed a prior application protocol error, so the peer didn't understand you and gave up.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks for the reply, then when do get "Bad file Descriptor" error. – K.H.Nagaradder Dec 24 '15 at 06:52
  • @K.H.Nagaradder - Most all socket send errors, with the exception of EAGAIN and EWOULDBLOCK, should be treated as a failed connection and your code should just close the socket handle. If you are getting "Bad file descriptor", it probably means a programming error on your part. Like maybe you already closed the socket or the file handle you are sending on is not a socket. – selbie Dec 24 '15 at 06:54
  • @K.H.Nagaradder When you call a system function with a file descriptor that is closed or was never opened. One question at a time thanks. – user207421 Dec 24 '15 at 06:56
2

I had the same problem and figured you can put in MSG_NOSIGNAL as the flag paramether insted of 0 to prefent the send function from throwing a signal if the socket is closed. insted it will return a -1 to show you that sending was not successfull.

int rc = send(sockFD, "data", 4, MSG_NOSIGNAL);
if (rc == -1) cout << "socket send failed" << endl;
Phillip
  • 789
  • 4
  • 22
  • In which case `send()` will return -1 with `errno == EPIPE`, which is consistent with what the OP states. It isn't clear that he's getting a signal rather than an `errno`. – user207421 May 08 '23 at 11:37