Article View: borland.public.cppbuilder.internet.socket
Article #3416Server and Client Sockets
From: 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;
}
}
Technical Details
Message-ID:
<433e524d$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!meganewsservers.com!feeder2.on.meganewsservers.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 11508e,dc3706a89a245de9,start X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Paul" <plmacca@clara.co.uk> Newsgroups: borland.public.cppbuilder.internet.socket Subject: Server and Client Sockets Date: Sat, 1 Oct 2005 10:14:02 -0000 Lines: 105 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-RFC2646: Format=Flowed; Original NNTP-Posting-Host: 86.132.253.112 Message-ID: <433e524d$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1128157773 86.132.253.112 (1 Oct 2005 02:09:33 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!meganewsservers.com!feeder2.on.meganewsservers.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail