Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ServerSocket and Threads

35 views
Skip to first unread message

Patryk B.

unread,
Sep 12, 2007, 5:19:55 AM9/12/07
to
Hello,

Im using ServerSocket and Threads (http://dn.codegear.com/article/26276).

I have problem with closing server application when some clients are
connected.
On FormClose event i have code:

ServerSocet->Active=false;

And everythink is ok when any client isn't connected.
When client is connected and im trying to close application it hang.
I think that i should disconnect client/terminate active threads befor
close application?

Best regards,

Patryk B.

Brendan Melia

unread,
Sep 12, 2007, 6:57:48 AM9/12/07
to


You need to close the server. This then ensures the relevant TCP/IP
state packets are sent to any attached clients.

ServerSocket->Close();

Bren.

Patryk B.

unread,
Sep 12, 2007, 7:34:35 AM9/12/07
to
> You need to close the server. This then ensures the relevant TCP/IP
> state packets are sent to any attached clients.
>
> ServerSocket->Close();
>

void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
{
ServerSocket->Close();
}

It dosent work, still when some client is connected it hang out,
I need to close client, then server is closing properly.

Patryk.

Remy Lebeau (TeamB)

unread,
Sep 12, 2007, 1:14:54 PM9/12/07
to

"Patryk B." <pbo...@no.spam.o2.pl> wrote in message
news:op.tyjav...@dune.intranet...

> I have problem with closing server application when
> some clients are connected.

Please show the actual code that you are using. Your thread code is likely
not responding to disconnects properly.


Gambit


Remy Lebeau (TeamB)

unread,
Sep 12, 2007, 1:14:05 PM9/12/07
to

"Brendan Melia" <bme...@hiden.co.uk> wrote in message
news:46e7...@newsgroups.borland.com...

> You need to close the server.

He already is. All Close() does is sets the Active property to false, which
he's doing directly.


Gambit


Patryk B.

unread,
Sep 13, 2007, 3:30:00 AM9/13/07
to
Dnia 12-09-2007 o 19:14:54 Remy Lebeau (TeamB) <no....@no.spam.com>
napisał(a):

> Please show the actual code that you are using. Your thread code is
> likely
> not responding to disconnects properly.

For test im using code from http://dn.codegear.com/article/26276
As client im using telnet. It hang beacuse i delete:

if (pStream->WaitForData(CLIENTWAITTIME))

I want disconnect client only when server is going to shutdown.
In all I can set time for few hours and for my future use it will be ok
but im
interesting how to do that.


> Gambit
>
>

Patryk.

--
Używam klienta poczty Opera Mail: http://www.opera.com/mail/

Remy Lebeau (TeamB)

unread,
Sep 13, 2007, 3:55:44 AM9/13/07
to

"Patryk B." <pbo...@no.spam.o2.pl> wrote in message
news:op.tyk0g...@dune.intranet...

> It hang beacuse i delete:
>
> if (pStream->WaitForData(CLIENTWAITTIME))

You need that line in order to detect a graceful disconnect from the client.
When WaitForData() returns true, and then Read() returns 0, the connection
has been disconnected by the client.

> I want disconnect client only when server is going to shutdown.

Then simply remove the call to Close() when WaitForData() returns false, ie:

void __fastcall TMyServerThread::ClientExecute(void)
{
char buffer[BUFFERSIZE];
int iRead;

try
{
TWinSocketStream *pStream = new TWinSocketStream(ClientSocket,
CLIENTWAITTIME);
try
{
while( (!Terminated) && (ClientSocket->Connected) )
{
if( !pStream->WaitForData(CLIENTWAITTIME) )
continue;

iRead = pStream->Read(buffer, sizeof(buffer);
if( iRead < 1 )
{
if( iRead == 0 )
// client disconnected...
else
// socket error...

ClientSocket->Close();
}
else
{
// process buffer up to iRead bytes...
}
}
}
__finally
{
delete pStream;
}
}
catch (...)
{
// handle exception...
}
}


Gambit


Patryk B.

unread,
Sep 13, 2007, 4:34:25 AM9/13/07
to
Dnia 13-09-2007 o 09:55:44 Remy Lebeau (TeamB) <no....@no.spam.com>
napisał(a):

Work perfectly.
Thanks for help and quick answer :)

Patryk.

Pere Mateu

unread,
Dec 18, 2007, 11:32:42 AM12/18/07
to
Hi,
I'm also using these components, and my app doesn't hang, but when I close
the server, the client socket believes it's active (Active property still
set is TRUE). How can I detect in the client socket that the server has been
shut down?
Thanks,
PEre

"Patryk B." <pbo...@no.spam.o2.pl> escribió en el mensaje
news:op.tyk3f...@dune.intranet...

Remy Lebeau (TeamB)

unread,
Dec 18, 2007, 2:04:08 PM12/18/07
to

"Pere Mateu" <pma...@siam.es> wrote in message
news:4767...@newsgroups.borland.com...

> when I close the server, the client socket believes it's active
> (Active property still set is TRUE).

As it should be. That is not what the Active property is for. You should
be looking at the Socket.Connected property instead.

> How can I detect in the client socket that the server has been shut down?

If you are using the client in non-blocking mode, then you will get an
OnDisconnect event. If you are using it in blocking mode instead, then
SendBuf() and ReceiveBuf() will return 0 the next time you write to, or read
from, the connection.


Gambit


Pere Mateu

unread,
Dec 20, 2007, 4:45:11 AM12/20/07
to
Hi Remy,
Thanks for the answer

>> when I close the server, the client socket believes it's active
>> (Active property still set is TRUE).
>
> As it should be. That is not what the Active property is for. You should
> be looking at the Socket.Connected property instead.

I tried but it's the same: ClientSocket->Socket->Connected remains TRUE
after the server socket is closed.

>
>> How can I detect in the client socket that the server has been shut down?
>
> If you are using the client in non-blocking mode, then you will get an
> OnDisconnect event. If you are using it in blocking mode instead, then
> SendBuf() and ReceiveBuf() will return 0 the next time you write to, or
> read from, the connection.

I'm working in blocking mode, but using a TWinSocketStream as in the
CodeGear example.
I guess I'll have to use these socket functions, because there's nothing I
can use in the stream.

Thanks again,
Pere


Remy Lebeau (TeamB)

unread,
Dec 20, 2007, 12:48:37 PM12/20/07
to

"Pere Mateu" <pma...@siam.es> wrote in message
news:476a...@newsgroups.borland.com...

> I'm working in blocking mode

The Connected property is not accurate in blocking mode, because there is
nothing to reset it when needed. You have to use a TWinSocketStream to
read/write data, and TWinSocketStream does not make updates to
TClientSocket's properties. So you have to look at the return values of
your actual reading/writing operations instead.

When reading, if TWinSocketStream::WaitForData() returns true and then
TWinSocketStream::Read() returns 0, the connection was disconnected
gracefully. If the socket was disconnected abnormally, however, then
WaitForData() will never return true, so you have to watch out for that.
TWinSocketStream.Read() will throw an exception if it couldn't read from the
socket.

When writing, there is no waiting. If TWinSocketStream.Write() returns 0,
the socket was disconnected gracefully. If there is an error writing to the
socket, an exception is thrown.

> I guess I'll have to use these socket functions, because there's
> nothing I can use in the stream.

Yes, there is. See above.


Gambit


0 new messages