Archived

This topic is now archived and is closed to further replies.

division

Socket does not close...

Recommended Posts

Hi there, I''ve got a litte problem closing a socket which is only receiving data. After I''ve called the close() function, I have to reboot the client, because it seams to be crashed. BTW: The client which receives data runns under DOS! Has anyone, any idea? - Thank you!

Share this post


Link to post
Share on other sites
My code looks like this, and it doesn''t close the socket! Why?
Hope you have any hints for me! :-)

/**** Server''s code ****/
int tcp_send( cd )
struct clientdata *cd;
{
struct tv timeout;
struct tv timeout_master;
fd_set write_fd;
fd_set write_fd_master;
int rc, i = 0;

timeout_master.tv_sec = cd->iTimeout;
timeout_master.tv_usec = 0;

FD_ZERO( &write_fd_master );
FD_SET( cd->iSocket, &write_fd_master );

while( 1 )
{
write_fd = write_fd_master;
timeout = timeout_master;

switch( select( cd->iSocket+1, 0, &write_fd, 0, (struct timeval*) &timeout ) )
{
case -1:
{
return TCP_CONNECTION_LOST;
}
case 0:
{
return TCP_TIMEOUT_WRITE;
}
default:
{
rc = send( cd->iSocket, cd->msg+i, cd->iMsgLen-i, 0 );
if( rc == -1 ) return TCP_ERROR;
i += rc;
if( i == cd->iMsgLen ) return TCP_OK;
if( i > cd->iMsgLen ) return TCP_ERROR;
}
}
}
}

/**** Client''s socketclose code ****/
int tcp_close( sock )
int* sock;
{
if( *sock )
{
shutdown( *sock, 2 );
close( *sock );
*sock = 0;
}
return 0;
}

Share this post


Link to post
Share on other sites
It''s hard to say.. What DOS TCP/IP library are you using?

You say the client hangs? How does it know when to close?

A "graceful" close will usually issue a shutdown() call,
followed by recv()''ing any data left on the line, followed by a closesocket().

You''re not doing the recv()''ing after the shutdown(), but if you''re client is only reading and only closes the socket on an error or when recv() returns 0 (graceful closure), then you shouldn''t need to do the buffer-flushing recv() calls.

// CHRIS

Share this post


Link to post
Share on other sites
> It''s hard to say.. What DOS TCP/IP library are you using?
PC/TCP

> You say the client hangs? How does it know when to close?
I press the escape-key and the application should close the socket.

> A "graceful" close will usually issue a shutdown() call,
> followed by recv()''ing any data left on the line, followed by
> a closesocket().
Hm, how has the server react to the shutdown call?
Or is that handled by the TCP/IP-Stack?

//Gerd

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
int shutdown(int socket, int how)

Disables sending, recieving, or both for a socket.

possible values for how:
0 = shutdown socket for recieving, can still send
1 = shutdown socket for sending, can still recieve
2 = shutdown socket for sending and recieving

Share this post


Link to post
Share on other sites