Socket timing out
Socket timing out
- Subject: Socket timing out
- From: Aaron Alpher <email@hidden>
- Date: Thu, 18 Jul 2002 16:32:33 -0400
I have setup a socket to a printer using socket() then connect() calls.
I then send info across the socket using read() and write(). This works
well under normal processing, however if I pause the print and do not
send data for more than 2 minutes, the socket apparently times out. Is
there a way to prevent this? I am setting the SO_KEEPALIVE and the
TCP_KEEPALIVE using setsockopt() but my app still crashes after a 2
minute delay in sending. Is there a way to prevent the timeout? Or
perhaps a better way to solve this problem?
Here is my socket setup code:
theSocket = socket( AF_INET, SOCK_STREAM, 0 );
if ( theSocket > 0 )
{
const SInt32 myTrue = 1;
result = setsockopt( theSocket, SOL_SOCKET, SO_KEEPALIVE, &myTrue,
sizeof( myTrue ) );
if ( result == 0 )
{
result = setsockopt( theSocket, IPPROTO_TCP, TCP_NODELAY,
&myTrue, sizeof( myTrue ) );
if ( result == 0 )
{
result = setsockopt( theSocket, IPPROTO_TCP,
TCP_KEEPALIVE, &myTrue, sizeof( myTrue ) );
if ( result == 0 )
{
struct timeval timeoutTime;
timeoutTime.tv_sec = 180;
timeoutTime.tv_usec = 0;
result = setsockopt( theSocket, SOL_SOCKET,
SO_SNDTIMEO, &timeoutTime, sizeof( timeoutTime ) );
if ( result == 0 )
{
struct sockaddr_in myAddress;
ClearMemory( &myAddress, sizeof( myAddress ) );
myAddress.sin_family = AF_INET;
myAddress.sin_port = portID;
myAddress.sin_addr.s_addr = theIPAddress;
if ( connect( theSocket, (const sockaddr *)
&myAddress, sizeof( myAddress ) ) == 0 )
{
// A successful connection
result = theSocket;
}
}
else
{
DebugMsg( "\psetsockopt( SO_SNDTIMEO ) is invalid" );
}
}
else
{
DebugMsg( "\psetsockopt( TCP_KEEPALIVE ) is invalid" );
}
}
else
{
DebugMsg( "\psetsockopt( TCP_NODELAY ) is invalid" );
}
}
else
{
DebugMsg( "\psetsockopt( SO_KEEPALIVE ) is invalid" );
}
}
Aaron Alpher
Compatable Systems Engineering
101 East Holly Avenue
Suite 1
Sterling, VA 20164 USA
(703) 404-1795
(703) 404-1896 FAX
email@hidden
_______________________________________________
macnetworkprog mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/macnetworkprog
Do not post admin requests to the list. They will be ignored.