It portrays this problem as optional, that only exists if the other side of the connection is misbehaving. He never mentions the problem outside this section, and none of his example code handles the problem. Thus, if you base your code on Steven's, it'll inherit this problem and sometimes crash.
The simplest solution is to configure the program to ignore the signal, such as putting the following line of code in your main function:. If you search popular projects, you'll find that this there solution most of the time, such as openssl. But there is a problem with this approach, as OpenSSL demonstrates: it's both a command-line program and a library. The command-line program handles this error, but the library doesn't.
Nowhere in the OpenSSL documentation does it mention that the user of the library needs to handle this. Ideally, library writers would like to deal with the problem internally. There are platform-specific ways to deal with this. But it's difficult. Browsing through cross platform projects like curl , which tries this library technique, I see the following bit:.
But that fails with Symbian because while it defines the constant in the source code, it doesn't actually support it, so it then must be undefined. So as you can see, solving this issue is hard.
It's an annoying set of things you have to do, but it's a non-optional thing you need to handle correctly, that must survive later programmers who may not understand this issue. Learn more. Asked 8 years, 1 month ago. Active 2 years, 5 months ago.
Viewed 86k times. Improve this question. Add a comment. Active Oldest Votes. Improve this answer. Dreyfus To clarify uncertainties on how to sert up signal handlers please post another question on this. Jason Chagas Jason Chagas 3 3 silver badges 2 2 bronze badges. That will then terminate the program instead of stopping in gdb unless the program ignores SIGPIPE or installed a handler for it, as per the accepted answer. Close flushes any buffered data.
If the other end has already been closed, then close will fail, and you will receive a sigpipe. If you are using buffered TCPIP, then a successful write just means your data has been queued to send, it doesn't mean it has been sent. Until you successfully call close, you don't know that your data has been sent.
Sigpipe tells you something has gone wrong, it doesn't tell you what, or what you should do about it. For more info read the man page. But for Ubuntu Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.
Asked 13 years, 1 month ago. Active 1 year, 1 month ago. Viewed k times. Improve this question. Olivia Stork 4, 4 4 gold badges 24 24 silver badges 39 39 bronze badges. Add a comment. Active Oldest Votes. Improve this answer. What exactly happens if my program writes to a broken pipe a socket in my case? Worth mentioning, since I found it once, then forgot it later and got confused, then discovered it a second time. Debuggers I know gdb does override your signal handling, so ignored signals are not ignored!
Show 3 more comments. The first reason this is a trap is that, in your case for example, one of the elements is a pointer. Except in very rare cases, the receiver will get that pointer which is useless on that end -- the pointer points to memory that is valid for the sending process.
The second, and less obvious reason this is a trap is that the layout in memory of a structure on one side of a socket might not be identical on the other side. This is a function of machine architectures and compiler settings, making it unsafe to trust that it will "just be ok". This trap is easy to get away with for a while, especially during development, where you're likely to have compatible architectures on each side of your test.
Your best bet is to send each field individually, even though that is painful. You can make it less painful by applying a little object oriented design to your code though, by creating a dedicated sender and receiver function for this structure. A packaged send as I described is fine as long as the structure hasn't changed from one side of the socket to the other, but you might want to be wary of having a different version of the structure on one end To handle this, you could consider tagging the data you send rather than assuming "first we send a , then b , etc.
0コメント