Socket Rarely Lasts
I have some code that is writing to a TCP client socket. The socket is non-blocking. Actually, I am a middle man between a VNC server and client (never mind why). It works for awhile and then quits. Before, during, and after the error, the value of errno in this client code is 11 (it is perpetually 11). The server side (the recipient of my socket connection), which I didn't write, often has the message: "Connection has been gracefully closed." Other times the server says "Failed to recv data from socket." Here is the code. The variable len is the size of the message to be sent.
for (int i=1; true; i++)
{
lenSent = write(serverSocket,sendBuf, len);
if (lenSent >= len)
break;
if (i > 20)
{
oss.clear();
oss << "Error writing to server. Error number is ";
oss << errno;
oss << ": ";
oss << strerror(errno);
LG1->Log(oss.str().c_str(), ERRORS, COMMUNICATION );
break;
}
usleep(100);
}
When I print out lenSent, it is always equal to len until the error occurs. A typical output would be:
len lenSent
---- -----------
1000 1000
1000 1000
1000 1000
1000 1000
1000 873
1000 -1
1000 -1
1000 -1
1000 -1
lenSent then remains -1 even on subsequent calls to write(). Any ideas? Is the socket just flaking out for network reasons having nothing to do with my code?
Thanks in advance.
Brandon
Last edited by Brandon9000; 02-27-2013 at 12:30 PM.
|