2012-02-14

Sockets in C: Error 141?

So, I've been working with sockets in C recently and encountered an issue I didn't easily find a solution. Hopefully this blog can end up on the top of the results because it's the post that helps others out.

Premise

So, the idea here is I have a service running that accepts connections for data to process. I have it configured to open a socket, then for each user that connects, it will create a thread and pass the client to that thread as it opens the socket for another potential connection. The thread handles the processing of data that passes for that connection and then terminates. Now, what happens when a client prematurely disconnects from the thread before it's finished? It would appear your application just dies, no "segmentation fault", no output, it just dies. You may notice it has exit code 141.

Problem

Turns out what really was happening was the service was sending itself signal 13, or SIGPIPE. If either the service or the client attempts to send data and the pipe is b0rk, it will send SIGPIPE and error out. You can suppress this with a flag.
Code:

ssize_t send(int sockfd, const void *buf, size_t len, int flags);

Just use MSG_NOSIGNAL as the [flags] parameter. send(2) will still return -1 and errno will still be set, but this makes a request to the application to not throw SIGPIPE. Also note, this is just a request; if the application is too busy to ignore this request, it will and the application will still get a SIGPIPE on occasion, so be sure to handle it with signal(2).

2012-02-07

Do You Fix Computers?

I often times get this question when I tell people that I work with computers. What's even more humorous is the number one complaint I see with most people is that it's slow...
I usually proceed with the next question: how much RAM do you have on it? Which is usually followed by "wat?" With a blank stare, or they will proudly boast about how little they know about computers, as if it were an achievement.
I wince as I ask the question "could you look on the box for this info?", which is followed up with "wat box?"
At this point a faceplant is usually issued as I explain the concept of RAM and how to obtain this info.

Since I've encountered a mass number if these questions and followed this conversation to no end, I think I'm going to change it up to avoid these questions and can get away with the excuse: "but... The Internet is much different from your computer...".
My new answer will be: I make the Internet work ^.^