Thread View: borland.public.cppbuilder.internet.socket
6 messages
6 total messages
Started by Paul
Sun, 28 May 2006 09:29
TIdUDPServer (and Client)
Author: Paul
Date: Sun, 28 May 2006 09:29
Date: Sun, 28 May 2006 09:29
95 lines
3615 bytes
3615 bytes
Hi,
I'm trying to get an idea of how Indy(9) TIdUDPServer and Client works and
have created a small test application.
I have got the client sending data to the server, and this seems ok (the
server event handler, and client thread extract is below). What I don't
understand or know how to achieve is the client receiving data back from the
server. I've not managed to get this to work so far.
Since the UDP protocol is connectionless, the read event must be at some
mid-point in the process of receiving the client data. So can the server
only respond in read event handler, using the socket binding information to
know where to return the data?
I've also noticed that when the server sends data, it receives its own data
(when using 127.0.0.1 addresses for both ends).
Last question, if I want two PCs to communicate async, do I need both ends
to be server and clients?
Paul
//---------------------------------------------------------------------------
// server form
void __fastcall TForm1::IdUDPServer1UDPRead(TObject *Sender,
TStream *AData, TIdSocketHandle *ABinding)
{
try
{
int BufSize = AData->Size;
int BufIndx = 0;
char * Buf = new char[BufSize];
while (BufSize > 0)
{
int iRead = AData->Read( &Buf[BufIndx], BufSize );
if( iRead > 0 )
{
// success
BufIndx += iRead;
BufSize -= iRead;
}
else
{
// fail
BufSize = 0;
}
}
// process buf
Memo1->Lines->Add(Buf);
// does not work???
char msg [] = "Server: Testing Loopback UDP to Client";
ABinding->Send(msg, sizeof(msg),0) ;
// delete buf
delete [] Buf;
}
catch(const Exception &)
{
;
}
}
//---------------------------------------------------------------------------
// client thread....
if (IdUDPClient)
{
try
{
char msg [] = "Client: Testing Loopback UDP to Server";
IdUDPClient->SendBuffer( msg, sizeof(msg) );
char rxd [100];
int rxl = 100;
AnsiString peer = "";
int peerport = 0;
int len = IdUDPClient->ReceiveBuffer( rxd, rxl, peer,
peerport, 100 );
if (len)
{
AnsiString str = len;
}
}
catch (...)
{
}
}
Re: TIdUDPServer (and Client)
Author: Remy Lebeau (TeamB)
Date: Sun, 28 May 2006 19:35
Date: Sun, 28 May 2006 19:35
34 lines
2174 bytes
2174 bytes
"Paul" <plmacca@clara.co.uk> wrote in message news:44796d7e$1@newsgroups.borland.com... > I have got the client sending data to the server, and this seems ok (the > server event handler, and client thread extract is below). What I don't > understand or know how to achieve is the client receiving data back > from the server. I've not managed to get this to work so far. In the server's OnUDPRead event, you are provided with the IP and port of the client. Send your data to that IP/port. > can the server only respond in read event handler using the socket > binding information to know where to return the data? Exactly. > I've also noticed that when the server sends data, it receives its own data > (when using 127.0.0.1 addresses for both ends). UDP data can appear on any network adapter that is installed. Depending on how you are sending the data and where, it is possible for the server to receive its own data back. It is your responsibility to look at the data and ignore anything you don't want. > if I want two PCs to communicate async, do I need both > ends to be server and clients? No. The server is already asynchronous. On the client side, simply use the UDPClient in a thread so the main thread is not blocked. Gambit
Re: TIdUDPServer (and Client)
Author: Paul
Date: Sun, 28 May 2006 20:26
Date: Sun, 28 May 2006 20:26
4 lines
1126 bytes
1126 bytes
Thanks. I'll give it another go tomorrow morning. "Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message news:4479fd0f$1@newsgroups.borland.com...
Re: TIdUDPServer (and Client)
Author: Paul
Date: Mon, 29 May 2006 12:00
Date: Mon, 29 May 2006 12:00
23 lines
1564 bytes
1564 bytes
Hi, Spotted a mistake in my code, and with that fixed I've got UDP packets being received by the server and responses received back at the client. I've also now moved both the TIdUDPServer and Client components in their own thread (each), and this seems to be ok too. For now, final two questions: 1) What does the property ThreadedEvent do for the TIdUDPServer component? 2) How does the IdUDPServerStatus event get triggered? I've had an event handler registered, but I don't get any events triggered? Thanks Paul "Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message news:4479fd0f$1@newsgroups.borland.com... >
Re: TIdUDPServer (and Client)
Author: Remy Lebeau (TeamB)
Date: Mon, 29 May 2006 17:23
Date: Mon, 29 May 2006 17:23
20 lines
1730 bytes
1730 bytes
"Paul" <plmacca@clara.co.uk> wrote in message news:447ae24c$1@newsgroups.borland.com... > What does the property ThreadedEvent do for the TIdUDPServer component? Like most of the Indy server components, TIdUDPServer is multi-threaded. It uses a worker thread to continuously read data from the UDP sockets. By default, TIdUDPServer uses TThread::Synchronize() to trigger the OnUDPRead event in the context of the main thread. Setting the ThreadedEvent property to true tells TIdUDPServer to not call Synchronize() so that the event handler is called in the context of the worker thread rather than the context of the main thread. > How does the IdUDPServerStatus event get triggered? There is no OnStatus event in TIdUDPServer in Indy 9. Gambit
Re: TIdUDPServer (and Client)
Author: Paul
Date: Mon, 29 May 2006 21:40
Date: Mon, 29 May 2006 21:40
9 lines
1306 bytes
1306 bytes
Thanks. "Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message news:447b2ea8@newsgroups.borland.com... > Setting the ThreadedEvent property > to true tells TIdUDPServer to not call Synchronize() so that the event > handler is called in the context of the worker thread rather than the > context of the main thread.
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