Board index » delphi » UDP Broadcast

UDP Broadcast


2004-08-07 12:45:37 AM
delphi28
Hello...
I have a question.
I Want to do a chat for a LAN.. I want to use TIdUDPServer and TIdUDPClient
from INDY.
My question: How sent a message to all users connected? (broadcast??) and
how read this message
(Sorry 4 my english but I am polish...I write here beacuse at polish group
pl.comp.lang.delphi I didn't get an answer...grrrr)
Thx, TiGER
 
 

Re:UDP Broadcast

Użytkownik "TiGER" <XXXX@XXXXX.COM>napisa?w wiadomości
Quote
Hello...
I have a question.
I Want to do a chat for a LAN.. I want to use TIdUDPServer and
TIdUDPClient
from INDY.
My question: How sent a message to all users connected? (broadcast??) and
how read this message

(Sorry 4 my english but I am polish...I write here beacuse at polish group
pl.comp.lang.delphi I didn't get an answer...grrrr)

Thx, TiGER

one else....why when I want to use the UDPRead procedure in UDPServer I
have an error:
undeclared identifier: 'TIdSocketHandle'??
 

Re:UDP Broadcast

"TiGER" <XXXX@XXXXX.COM>writes
Quote
How sent a message to all users connected? (broadcast??)
TIdUDPBase, which both TIdUDPServer and TIdUDPClient derive from, has a
Broadcast() method for that.
Quote
and how read this message
The same way you read any other incoming UDP message.
Gambit
 

Re:UDP Broadcast

Użytkownik "Remy Lebeau (TeamB)" <XXXX@XXXXX.COM>
napisa?w wiadomości news:4113c668$XXXX@XXXXX.COM...
Quote

"TiGER" <XXXX@XXXXX.COM>writes
news:4113b5b3$XXXX@XXXXX.COM...

>How sent a message to all users connected? (broadcast??)

TIdUDPBase, which both TIdUDPServer and TIdUDPClient derive from, has a
Broadcast() method for that.

>and how read this message

The same way you read any other incoming UDP message.


Gambit


How read incoming UDP messages?? :\
 

Re:UDP Broadcast

"TiGER" <jzdz(DELETE.THIS)@poczta.onet.pl>writes
Quote
How read incoming UDP messages?? :\
TIdUDPServer does the reading for you automatically, and triggers its
OnUDPRead event when data arrives.
For TIdUDPClient, call its ReceiveBuffer() or ReceiveString() method as
needed.
Gambit
 

Re:UDP Broadcast

Użytkownik "Remy Lebeau (TeamB)" <XXXX@XXXXX.COM>
napisa?w wiadomości news:4113d5b1$XXXX@XXXXX.COM...
Quote

"TiGER" <jzdz(DELETE.THIS)@poczta.onet.pl>writes
news:XXXX@XXXXX.COM...

>How read incoming UDP messages?? :\

TIdUDPServer does the reading for you automatically, and triggers its
OnUDPRead event when data arrives.

For TIdUDPClient, call its ReceiveBuffer() or ReceiveString() method as
needed.


Gambit


OK but in OnUDPRead AData is a TStream.. how to for example place the AData
to TMemo ?
one else:
In OnUDPRead I Have an error undeclared identifier: TIdSocketHandle... ??
Do You know (have) any examples of broadcasting ....
I don't catch it...:/
THX
 

Re:UDP Broadcast

Add IdSocketHandle in your Uses clause
"TiGER" <jzdz(DELETE.THIS)@poczta.onet.pl>writes
Quote

Użytkownik "Remy Lebeau (TeamB)" <XXXX@XXXXX.COM>
napisa?w wiadomości news:4113d5b1$XXXX@XXXXX.COM...
>
>"TiGER" <jzdz(DELETE.THIS)@poczta.onet.pl>writes
>news:XXXX@XXXXX.COM...
>
>>How read incoming UDP messages?? :\
>
>TIdUDPServer does the reading for you automatically, and triggers its
>OnUDPRead event when data arrives.
>
>For TIdUDPClient, call its ReceiveBuffer() or ReceiveString() method as
>needed.
>
>
>Gambit
>
>
OK but in OnUDPRead AData is a TStream.. how to for example place the
AData
to TMemo ?
one else:
In OnUDPRead I Have an error undeclared identifier: TIdSocketHandle... ??
Do You know (have) any examples of broadcasting ....
I don't catch it...:/

THX


 

Re:UDP Broadcast

"TiGER" <jzdz(DELETE.THIS)@poczta.onet.pl>writes
Quote
OK but in OnUDPRead AData is a TStream.. how to
for example place the AData to TMemo ?
Extract the stream data into a string buffer first and the add the buffer to
the Memo. For example:
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
Str: String;
begin
SetLength(Str, AData.Size);
AData.Read(Pointer(Str)^, AData.Size);
Memo1.Lines.Add(Str);
end;
Gambit
 

Re:UDP Broadcast

Użytkownik "Remy Lebeau (TeamB)" <XXXX@XXXXX.COM>
napisa?w wiadomości news:4113f8a0$XXXX@XXXXX.COM...
Quote

"TiGER" <jzdz(DELETE.THIS)@poczta.onet.pl>writes
news:4113d9c3$XXXX@XXXXX.COM...

>OK but in OnUDPRead AData is a TStream.. how to
>for example place the AData to TMemo ?

Extract the stream data into a string buffer first and the add the buffer
to
the Memo. For example:

procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
Str: String;
begin
SetLength(Str, AData.Size);
AData.Read(Pointer(Str)^, AData.Size);
Memo1.Lines.Add(Str);
end;


Gambit


Thank You very much... It WORKS... :)
 

Re:UDP Broadcast

Hello,
my destination answers on a wrong port.
the following lines will send a udp broadcast
with a correct destination port, but a wrong source
port. how can I change the source port in the
UDPClient component ?
my code sample:
UDPClient.Active := true;
UDPClient.Broadcast(strHilf, 30718);
UDPClient.Active := false;
cu Casi.
 

Re:UDP Broadcast

I need to broadcast a udp packet across my lan. I get an invalid ip address
error when trying to with INADDR_ANY. I can successfully call sendto if I
specify an ip address. Below is the code I have written.
WSADATA wsaData;
SOCKET s;
sockaddr_in addr;
WSAStartup(MAKEWORD(2,0), &wsaData);
s = socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP);
if (s==INVALID_SOCKET) {throw AnsiString("");}
addr.sin_family = AF_INET;
addr.sin_port = htons(ToPort->Text.ToInt());
addr.sin_addr.s_addr = htonl(INADDR_ANY);
int rc = bind(s,(SOCKADDR*)&addr,sizeof(SOCKADDR_IN));
if (rc==SOCKET_ERROR) {throw AnsiString("Bind Error:
"+AnsiString(WSAGetLastError()));}
int ret = sendto(s,pkt,len,0,(SOCKADDR*)&addr,sizeof(addr));
if (ret == SOCKET_ERROR) {
throw AnsiString("Send Error: "+AnsiString(WSAGetLastError()));
}
closesocket(s);
WSACleanup();
Thanks for any help,
atg
 

Re:UDP Broadcast

Adam Griffin writes:
Quote
I need to broadcast a udp packet across my lan. I get an invalid ip address
error when trying to with INADDR_ANY. I can successfully call sendto if I
INADDR_ANY, as the name implies, is for INput, or receiving a
broadcast. The Output address is
#define UDP_GROUP "255.255.255.255"
#define UDP_PORT 12345 /*whatever you want to use*/
SendSocket=socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP )) )
/* turn on broadcast */
t = TRUE;
setsockopt(SendSocket,SOL_SOCKET,SO_BROADCAST,(char*)&t,sizeof(t));
/* set up destination address */
memset( &SendAddr, 0, sizeof(SendAddr) );
SendAddr.sin_family = AF_INET;
SendAddr.sin_addr.s_addr = inet_addr( UDP_GROUP );
SendAddr.sin_port = htons( UDP_PORT );
/* now just sendto() our destination! */
sendto( SendSocket, DataGram, sizeof(DataGram), 0,
(struct sockaddr *) &SendAddr, sizeof(SendAddr) );
 

Re:UDP Broadcast

Hello All,
I have a problem using the broadcast method of indy that ccomes with BCB6.
can someone just show me how to send a broadcast "ANY" and let the server
reply with "Ok"
I want to know if any computer on my LAN is running my App.
Thanks.
 

Re:UDP Broadcast

Apologies if this question has been asked frequently, but I can not find the
answer...
How can I receive a UDP broadcast? I am using the idUDPclient on one
machine to send a broadcast (since I don't know the IP of the machine I want
to talk to beforehand), and I assumed the idUDPserver would receive the
broadcast. However, either I have installed Indy incorrectly or something
else is amuck, but it is very frustrating: If I create an OnUDPRead event
handler for the UDP server and try to compile, it creates this line:
procedure IdUDPServer1UDPRead(Sender: TObject; AData: TBytes;
ABinding: TIdSocketHandle);
and flags it as "Undeclared identifier: TBytes" !!
I cannot find any instance of "TBytes" in any file in the whole Delphi5 file
tree (including Indy files). Indy never seems to work on the first try, it
always requires a bit of wrestling with it...
Thank you for any help
 

Re:UDP Broadcast

Forgot to mention, I am using Delphi 5, Windows XP