Board index » cppbuilder » TUdpSocket samples?

TUdpSocket samples?


2006-03-01 01:40:49 PM
cppbuilder80
Hi,
I'm totally lost in trying to get a UDP connection to work usinf TUdpSocket.
I admit that I know totally nothing about networking and such, but I jist
simply don't understand why this damn thing is so hard to get to work.
I and pleaing anyone to show me an example of how to get 2 PCs to
communicate using TUdpSocket. It is suppost to work in a server broadcasting
to all connected client environment, but a 1-to-1 example should be enough
to get me started. I need example of sending text (TUdpSocket->Sendln())
and/or sending binary data (say, a 128 byte binary data packets) if the two
procedure is significantly different.
So far, in my venture, I never manage to recieve any thing from the
connection. That damn OnRecieve() event just never freaking triggered
anyway. Why it's so difficult to get this freak event to fire?
Pardon my language, but I'm really pissed by the lack of documentation about
this UDP thing. People keep it like top secrets, never willing to talk about
it in any forum/newsgroup/website. The document in C++Builder is even worst,
with most of the explaination derived from other objects (TBaseSocket) and
most of the time the damn lazy documentor (or whatever it is called) never
bother to explain what the arguments are and how they work.
Thanks in advance.
Sigh......(frustrated enough to start looking elsewhere for a better
product)
 
 

Re:TUdpSocket samples?

Look like I'm right, the usage of UDP is indeed top secret, no one ever want
to talk about it.
okay, I'll put in more detail, following is the code I used to tranmit the
data:
//------------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
static int count = 0;
char buffer[255];
memset(buffer, 0, 255);
AnsiString str = "Send Data Number " + IntToStr(++count);
int size = str.Length();
strcpy(buffer, str.c_str());
if(UdpSock->WaitForData(1000))
{
Memo->Lines->Add("Ready");
UdpSock->SendBuf(buffer, size, 0);
Memo->Lines->Add("Sending " + IntToStr(UdpSock->BytesSent) + "
bytes");
}
else
Memo->Lines->Add("Failed");
}
//-----------------------------------------------------------------------------
The socket seems to be connected correctly (I checked all the possible
events and report the status), there is nothing much I could tweak around
the component, so I guess I have it configured correctly (I only setup the
hosts and ports, nothing else much to set anyway). However, the problem is
the WaitForData() always failed and no data was actually sent. Why would the
method always failed? I never managed to get the socket to get ready for
transfering any data with this {*word*81} always failed!
 

Re:TUdpSocket samples?

choykw wrote:
Quote
if(UdpSock->WaitForData(1000))
You could have started by telling us what component you use.
Quote
{
Memo->Lines->Add("Ready");
UdpSock->SendBuf(buffer, size, 0);
You are not checking the return value of SendBuf. (If there is any).
Quote
.. However, the problem is
the WaitForData() always failed and no data was actually sent. Why would the
method always failed?
Why are you waiting for data if you want to send ? Why not just use SendBuf ?
Hans.
 

{smallsort}

Re:TUdpSocket samples?

"Hans Galema" < XXXX@XXXXX.COM >
???????:4406c5a2$ XXXX@XXXXX.COM ...
Quote
choykw wrote:

>if(UdpSock->WaitForData(1000))

You could have started by telling us what component you use.
I thought I have stated it CLEARLY in the subject of this post: TUdpSocket?
Quote
>{
>Memo->Lines->Add("Ready");
>UdpSock->SendBuf(buffer, size, 0);

You are not checking the return value of SendBuf. (If there is any).
I did, in another version of this code, but since the data was never sent,
I just omit that check untill I found out why there isn't any dat sent.
Quote
>.. However, the problem is
>the WaitForData() always failed and no data was actually sent. Why would
>the method always failed?

Why are you waiting for data if you want to send ? Why not just use
SendBuf ?
That is because, I did use SendBuf() and I could never get any thing send
out. After some investigation, I realized that the socket is never ready to
send anything, thus causing the OnRecieve() event to be never get triggered.
As for why I use WaitForData(), well, to find out whether the socket is
ready for data of course. WaitForData doesn't just Wait For Data as the name
suggested, but according to Borland's help documentation:
*quote*
Call WaitForData to ensure that the socket connection is ready to read or
write information. WaitForData returns true if the socket connection is
ready. WaitForData returns false if the socket connection is not ready after
t (time-out) milliseconds elapse.
Call WaitForData before reading or writing information over the socket
connection. Otherwise, ReceiveBuf, Receiveln, SendBuf, and Sendln method
calls may time out before any data is transferred.
*end quote*
as this document suggested, I should call WaitForData() before I send
anything to make sure the socket is ready to do any data transfer.
Quote

Hans.
 

Re:TUdpSocket samples?

choykw wrote:
Quote
I thought I have stated it CLEARLY in the subject of this post: TUdpSocket?
Well then that is a clear example of never refering to the subject of
a mail. Put all info in the body. I really did not see it. But this is not a
Borland socket is it ? I do not see it in bcb5.
Quote
I did, in another version of this code, but since the data was never sent,
How did you check ? Are there clients ?
Quote
as this document suggested, I should call WaitForData() before I send
anything to make sure the socket is ready to do any data transfer.
Ok. But then did you try just to use SendBuf ? It looks to me that
you you have to use WaitForData only in blocking mode. Which mode do you use ?
Hans.
 

Re:TUdpSocket samples?

"Hans Galema" < XXXX@XXXXX.COM >
???????:4407ede5$ XXXX@XXXXX.COM ...
Quote
choykw wrote:

>I thought I have stated it CLEARLY in the subject of this post:
>TUdpSocket?

Well then that is a clear example of never refering to the subject of
a mail. Put all info in the body. I really did not see it. But this is not
a
Borland socket is it ? I do not see it in bcb5.
It is Borland's. I'm using BCB6.
Quote
>I did, in another version of this code, but since the data was never
>sent,

How did you check ? Are there clients ?
sigh, do I look _that_ _sutpid_? Of course I have to create a server and a
client before I go testing any socket. If I don't have a client, how do I
expect to recieve any data that is transmitted by a server?
Quote
>as this document suggested, I should call WaitForData() before I send
>anything to make sure the socket is ready to do any data transfer.

Ok. But then did you try just to use SendBuf ? It looks to me that
you you have to use WaitForData only in blocking mode. Which mode do you
use ?
I tried everything I could think of on that socket, it's derived from
TBaseSocket and there isn't much added to that component. I have exhausted
every single test (SendBuf, Sendln, SendTo, ReceiveBuf, Receiveln,
ReceiveFrom, Blocking, nonBlocking, everything) before I got desperate
enough to ask in a group that don't answer my request for help!
Hans, I appreciate your helpfulness, but if all you will do is answering my
questions with even more questions, I don't see how we can get any further.
I guess I'll just forget asking anymore questions here and start digging
into MSDN to find out how to do things with VC++ and Windows API. Not even
the Borland officials care to answer this question, hack, I bet even they
don't know anything about this cryptic little component and it is actually a
non-working component that actually serves as a joke to mock on desperate
programmers trying to get networking app up and running.
Just forget I've ever asked any question., will ya? I just wish my Boss
never have bought this damn Builder thing and ask me to use it.
 

Re:TUdpSocket samples?

"choykw" < XXXX@XXXXX.COM >wrote in message
Quote
Hi,

I'm totally lost in trying to get a UDP connection to work usinf
TUdpSocket. I admit that I know totally nothing about networking and such,
but I jist simply don't understand why this damn thing is so hard to get
to work.
You might like to read this for some general background on UDP
www.overbyte.be/eng/support/tcpudp_primer.html
The ICS software is part of the extra stuff you get with BDS2006 and can be
downloaded free anyway. Their user group is helpful too.
John Mulvey