UDP server/client

I am using XE7.1 with the installed Indy.

I wrote an application that uses the client and server classes from indy 
for UDP to comunicate.

There are many servers on the same network, and potentially many clients.

THe client send a broadcast to the broadcast ip to get signal the servers, 
so they send back an ack that the client can discover the server ips.

All this works fine for me, however th way I implemented could be wrong.


My server is using the TidUDPServer only.

But my clients are using both, the TidUDPServer and TidUDPClient.

Since the UDPCLient is blocking when sending a message and it waits for 1 
message, and not many, I have added the UDPServer just to use the listening 
thread that it implements.

However this brings a problems, if I open more than client in the same computer 
it causes an error, probably because the PORT that I choose is in user already 
(idUDPServer is set to 44444)

So how to implement this?

How can I have a UDPClient with a listener thread?

or a UDPServer that shares the same port?

or my approach is all wrong, besides it works when only one client is running 
(that is 99,99% of the cases)

thanks....


Eduardo
0
Eduardo
7/14/2015 11:28:16 PM
embarcadero.delphi.winsock 1874 articles. 2 followers. Follow

1 Replies
2865 Views

Similar Articles

[PageSpeed] 20

Eduardo wrote:

> Since the UDPCLient is blocking when sending a message and it waits
> for 1 message, and not many, I have added the UDPServer just to use
> the listening thread that it implements.

Why not just have the UDPServer read everything?  You don't really need the 
UDPClient, you can send out data using TIdUDPServer as well, just send using 
the Binding that you want to receive a reply with.  In your OnUDPRead event, 
look at the packet data and act accordingly.

> However this brings a problems, if I open more than client in the same
> computer it causes an error, probably because the PORT that I choose
> is in user already (idUDPServer is set to 44444)

Correct.  You cannot have multiple servers on the same machine listening 
on the same port at the same time.

> How can I have a UDPClient with a listener thread?

Move the TIdUDPClient into its own thread, and let it block that thread on 
reading.  What is the problem?

> or a UDPServer that shares the same port?

You cannot.

> or my approach is all wrong, besides it works when only one client is
> running (that is 99,99% of the cases)

If you really need multiple clients on the same machine, you have to make 
them listen on different ports.  You could let the OS pick an available port 
for you (set the Binding.Port to 0), or you can make the clients communicate 
with each other to coordinate their port usage with each other.  Either way, 
you need separate ports, period.

-- 
Remy Lebeau (TeamB)
0
Remy
7/14/2015 11:52:56 PM
Reply: