[Ace-users] Connection closing before the answer arrives to the client
salvatore.gallo at gmail.com
salvatore.gallo at gmail.com
Thu Feb 7 04:47:36 CST 2008
ACE VERSION: 5.6.2
CLIENT MACHINE and OPERATING SYSTEM: Windows 2003 Server + Java socket
SERVER MACHINE and OPERATING SYSTEM: Linux RedHat + ACE 5.6.2
AREA/CLASS/EXAMPLE AFFECTED: Multi-thread application with TCP/IP
Socket; Affected: ReadHander class
SYNOPSIS: The handle_close method closes the connection before the
client ( Java Socket ) receiving the answer from the server.
DESCRIPTION:
The client (Java socket) returns the message "connection refused". It
happens because, sometimes, the handle_close method closes the
connection before the client receives the message. N.B. These
applications send only one message, i.e.the client (Java) sends only a
query and the server (C++) sends only an answer; after the connection
is closed.
Is there a way to be sure that the client receives the message without
being necessary that the server receives an ack from the client?
Or is there a better way to delay the closing?
SAMPLE FIX/WORKAROUND:
A workaround is:
put an ACE_OS::sleep(0.3) in handle_close() method. So the client can
receive the message before the connection is closed.
CODE:
ReadHandler::ReadHandler():ACE_Event_Handler(),mStream()
{
ACE_TRACE(ACE_TEXT("ReadHandler::ReadHandlerHandler()"));
}
ReadHandler::~ReadHandler()
{
ACE_TRACE(ACE_TEXT("ReadHandler::~ReadHandler()"));
if(mStream.close()==-1)
ACE_ERROR((LM_ERROR,ACE_TEXT("%N:%1 Failed to close socket. ")
ACE_TEXT("(errno = %i: %m)\n"),errno));
}
ACE_SOCK_Stream& ReadHandler::getStream(void)
{
ACE_TRACE(ACE_TEXT("ReadHandler::getStream(void)"));
return mStream;
}
ACE_HANDLE ReadHandler::get_handle(void) const
{
ACE_TRACE(ACE_TEXT("ReadHandler::get_handle(void)"));
return mStream.get_handle();
}
int ReadHandler::handle_input(ACE_HANDLE)
{
ACE_TRACE(ACE_TEXT("ReadHandler::get_handle(void)"));
TPProtocolManager protocol;
protocol.readQuery(mStream);
protocol.sendReply(mStream);
return -1;
}
int ReadHandler::handle_close(ACE_HANDLE,ACE_Reactor_Mask)
{
ACE_OS::sleep(0.3); //WORKAROUND
ACE_TRACE(ACE_TEXT("ReadHandler::handle_close"));
delete this;
return 0;
}
More information about the Ace-users
mailing list