Thread View: borland.public.cppbuilder.internet.socket
10 messages
10 total messages
Started by Paul
Sat, 01 Oct 2005 10:14
Server and Client Sockets
Author: Paul
Date: Sat, 01 Oct 2005 10:14
Date: Sat, 01 Oct 2005 10:14
103 lines
4686 bytes
4686 bytes
Hi,
I've written some classes to wrap the server and client components (and
serial) to create some connectivity classes.
It all seems to work in the testing I have done so far when instantiating
the class(es) from the main form.
Server Log:
2005-10-01 09:25:14:0025 : OpenConnection - enter
2005-10-01 09:25:14:0145 : Listening....
2005-10-01 09:25:14:0366 : OpenConnection - exit
2005-10-01 09:25:20:0625 : Get Socket....
2005-10-01 09:25:20:0625 : Connected
2005-10-01 09:25:20:0625 : Accept
2005-10-01 09:25:20:0625 : Write Event - Write Ready
2005-10-01 09:25:38:0590 : Disconnected
2005-10-01 09:25:40:0373 : CloseConnection - enter
2005-10-01 09:25:40:0373 : CloseConnection - exit
Client Log:
2005-10-01 09:25:20:0334 : OpenConnection - enter
2005-10-01 09:25:20:0434 : Lookup
2005-10-01 09:25:20:0444 : Connecting
2005-10-01 09:25:20:0625 : OpenConnection - exit
2005-10-01 09:25:20:0625 : Connected
2005-10-01 09:25:20:0625 : Write Event - Write Ready
2005-10-01 09:25:38:0590 : CloseConnection - enter
2005-10-01 09:25:38:0590 : Disconnected
2005-10-01 09:25:38:0590 : CloseConnection - exit
But if I try to instantiate the class(es) in a thread, then only the serial
connectivity works. The server and client socket fail (such as it never
gets the events to show client connecting or connected).
Server Log:
: CIP::OpenConnection - enter
: Listening....
: CIP::OpenConnection - exit
then nothing
Client Log:
: CIP::OpenConnection - enter
: Lookup
: Connecting
: CIP::OpenConnection - exit
then nothing
I'm setting the socket type to nonblocking, is this the problem, should non
blocking components be used in threads? And I'm passing NULL in the
constructor parameter for the component, does anyone know what this is used
for, as it doesn't seem to make much difference, is it only of use when
"drag and dropped" at design time?
All the event handlers have critical sections to make the handlers thread
safe.
Paul
//---------------------------------------------------------------
__fastcall CIP_Socket::CIP_Socket( CMailBox * OwnerMbx,
char * MyName,
ulong RxDataID,
ulong TxDoneID,
ulong FailedID,
bool AsServer )
: CConnectivity ( OwnerMbx, MyName, RxDataID, TxDoneID, FailedID )
{
if (AsServer)
{
// create TCP Server Socket, with default parameters
Ssocket = new TServerSocket(NULL);
Ssocket->ServerType = stNonBlocking;
Ssocket->Port = 20000;
// assign callbacks
Ssocket->OnClientConnect = SSocketConnect;
Ssocket->OnClientDisconnect = SSocketDisconnect;
Ssocket->OnClientError = SSocketError;
Ssocket->OnClientRead = SSocketRead;
Ssocket->OnClientWrite = SSocketWrite;
Ssocket->OnAccept = SSocketAccept;
Ssocket->OnListen = SSocketListen;
Ssocket->OnGetSocket = SSocketGetSocket;
}
else
{
// create TCP Client Socket, with default parameters
Csocket = new TClientSocket(NULL);
Csocket->ClientType = ctNonBlocking;
Csocket->Address = "127.0.0.1";
Csocket->Port = 20000;
// assign callbacks
Csocket->OnConnect = CSocketConnect;
Csocket->OnConnecting = CSocketConnecting;
Csocket->OnLookup = CSocketLookup;
Csocket->OnDisconnect = CSocketDisconnect;
Csocket->OnError = CSocketError;
Csocket->OnRead = CSocketRead;
Csocket->OnWrite = CSocketWrite;
}
}
Re: Server and Client Sockets
Author: Hans Galema
Date: Sat, 01 Oct 2005 10:59
Date: Sat, 01 Oct 2005 10:59
33 lines
2314 bytes
2314 bytes
Paul wrote:
> I'm setting the socket type to nonblocking, is this the problem, should non
> blocking components be used in threads?
Yes. You cannot use nonblocking in a thread as that depend on events triggered
from a message queue. There is no such queue in your thread. But you could
add such a loop I think.
> And I'm passing NULL in the
> constructor parameter for the component, does anyone know what this is used
> for,
As usual the -vcl-owner will delete that component when it is deleted
itself. If you pass NULL then your component should delete it in the
destructor. If you pass 'this' then it will be deleted automatically
provided that OwnerMbx is a vcl object.
> if (AsServer)
> {
> // create TCP Server Socket, with default parameters
> }
> else
> {
> // create TCP Client Socket, with default parameters
> }
Seeing that you make one class that can behave both like server
and client I shiver. I have done such things too but now my opinion
is that live is much easier having two classes. Maintainability
of code only for a server and only for a client is much easier then.
Hans.
Re: Server and Client Sockets
Author: Paul
Date: Sun, 02 Oct 2005 22:31
Date: Sun, 02 Oct 2005 22:31
25 lines
2018 bytes
2018 bytes
> Yes. You cannot use nonblocking in a thread as that depend on events > triggered > from a message queue. There is no such queue in your thread. But you could > add such a loop I think. Thought so, back to the drawing board! > As usual the -vcl-owner will delete that component when it is deleted > itself. If you pass NULL then your component should delete it in the > destructor. If you pass 'this' then it will be deleted automatically > provided that OwnerMbx is a vcl object. I'm deleteing the object, so I will leave it at NULL for now. > Seeing that you make one class that can behave both like server > and client I shiver. I have done such things too but now my opinion > is that live is much easier having two classes. Maintainability > of code only for a server and only for a client is much easier then. It seemed like a good idea at the time, but like you said, I'm not so sure its a good idea anymore. Thanks for your input. Paul
Re: Server and Client Sockets
Author: Remy Lebeau (TeamB)
Date: Mon, 03 Oct 2005 22:14
Date: Mon, 03 Oct 2005 22:14
14 lines
1207 bytes
1207 bytes
"Hans Galema" <notused@notused.nl> wrote in message news:433e6bce$1@newsgroups.borland.com... > You cannot use nonblocking in a thread Yes, you can. > as that depend on events triggered from a message queue. You can give the thread its own message queue. Gambit
Re: Server and Client Sockets
Author: Remy Lebeau (TeamB)
Date: Mon, 03 Oct 2005 22:18
Date: Mon, 03 Oct 2005 22:18
26 lines
1974 bytes
1974 bytes
"Paul" <plmacca@clara.co.uk> wrote in message news:433e524d$1@newsgroups.borland.com... > I've written some classes to wrap the server and client components > (and serial) to create some connectivity classes. You might consider using a pre-written library that already does all of that for you. Indy (http://www.indyproject.org), for instance. In particular, due to Indy's plugin architecture, adding serial port support would take minimal effort. > But if I try to instantiate the class(es) in a thread, then only the serial > connectivity works. The server and client socket fail (such as it never > gets the events to show client connecting or connected). Non-blocking notifications are message-based. Thus your thread would need its own message queue to dispatch the notifications properly. If that does not suit your needs, then you will have to use blocking sockets instead, which means that you will not need a message queue at all, but you will likely have to re-think your read/writing strategy a bit. Gambit
Re: Server and Client Sockets
Author: Steve Aletto
Date: Tue, 04 Oct 2005 06:59
Date: Tue, 04 Oct 2005 06:59
9 lines
1399 bytes
1399 bytes
>> You cannot use nonblocking in a thread > > Yes, you can. Correct. IMO, the right sentence to write was "You SHOULD NOT use nonblocking sockets in a thread". It doesn't make much sense as we all stated many times in the past. Steve.
Re: Server and Client Sockets
Author: Hans Galema
Date: Tue, 04 Oct 2005 08:00
Date: Tue, 04 Oct 2005 08:00
22 lines
1723 bytes
1723 bytes
Remy Lebeau (TeamB) wrote: >>You cannot use nonblocking in a thread > > Yes, you can. > >>as that depend on events triggered from a message queue. > > You can give the thread its own message queue. If you quote me then quote me in a reasonable way. I stated: >>> Yes. You cannot use nonblocking in a thread as that depend >>> on events triggered from a message queue. There is no such >>> queue in your thread. But you could add such a loop I think. So I already gave the hint that it was probably possible if a message queue was added. You could have confirmed my suggestion instead of omitting it and bringing the adding of a queue as your idea. Hans.
Re: Server and Client Sockets
Author: Remy Lebeau (TeamB)
Date: Tue, 04 Oct 2005 18:31
Date: Tue, 04 Oct 2005 18:31
28 lines
1809 bytes
1809 bytes
"Paul" <plmacca@clara.co.uk> wrote in message
news:4342c2c8$1@newsgroups.borland.com...
> All of you have mentioned adding a message Q to the
> thread, where can I get info on this?
A message queue is nothing more than calling the Win32 API
Get/PeekMessage(), TranslateMessage() and DispatchMessage() functions in a
loop for the lifetime of the thread. For example:
void __fastcall TMyThread::Execute()
{
MSG msg;
while( GetMessage(&msg, NULL, 0, 0) > 0 )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
void __fastcall TMyThread::TerminateThread()
{
PostThreadMessage(ThreadID, WM_QUIT, 0, 0);
}
Gambit
Re: Server and Client Sockets
Author: Paul
Date: Tue, 04 Oct 2005 19:03
Date: Tue, 04 Oct 2005 19:03
44 lines
2313 bytes
2313 bytes
Thanks for all your input. All of you have mentioned adding a message Q to the thread, where can I get info on this? Thanks Paul PS. I will look at the indy project site also. "Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message news:4341ae16$3@newsgroups.borland.com... > > "Paul" <plmacca@clara.co.uk> wrote in message > news:433e524d$1@newsgroups.borland.com... > >> I've written some classes to wrap the server and client components >> (and serial) to create some connectivity classes. > > You might consider using a pre-written library that already does all of > that > for you. Indy (http://www.indyproject.org), for instance. In particular, > due to Indy's plugin architecture, adding serial port support would take > minimal effort. > > >> But if I try to instantiate the class(es) in a thread, then only the > serial >> connectivity works. The server and client socket fail (such as it never >> gets the events to show client connecting or connected). > > Non-blocking notifications are message-based. Thus your thread would need > its own message queue to dispatch the notifications properly. If that > does > not suit your needs, then you will have to use blocking sockets instead, > which means that you will not need a message queue at all, but you will > likely have to re-think your read/writing strategy a bit. > > > Gambit > >
Re: Server and Client Sockets
Author: Paul
Date: Tue, 04 Oct 2005 20:00
Date: Tue, 04 Oct 2005 20:00
5 lines
1353 bytes
1353 bytes
that was quick. thanks > A message queue is nothing more than calling the Win32 API > Get/PeekMessage(), TranslateMessage() and DispatchMessage() functions in a > loop for the lifetime of the thread. For example:
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads