Jump to content
Sign in to follow this  
CHackbart

TSocket and UDP

Recommended Posts

Hi,

 

does somebody know if there is an example how to use the System.Net.Socket in combination with UDP? 

In the past I used synapse for this, but prefer in order to simplify my code use something which is officially supported on all platforms. 

What I did till now is the following: 

 

1. I collect the available network devices via:

procedure GetLocalIPs(iplist: TStrings; ipfamily: Integer);
var
  TcpSock: TTCPBlockSocket;
begin
    TcpSock := TTCPBlockSocket.create;
    case ipfamily of
      1 : TcpSock.family:=SF_IP4;
      2 : TcpSock.family:=SF_IP6;
    end;
    try
      TcpSock.ResolveNameToIP(TcpSock.LocalName, ipList);
    finally
      TcpSock.Free;
    end;
end;

 

2. I create a TUDPBlockSocket using: 

 Result := TUDPBlockSocket.Create();
 Result.bind(aIP, '0');
 Result.Connect('239.255.255.250', '1900');

resp.

Result := TUDPBlockSocket.Create();
  Result.EnableReuse(True);
  Result.bind(aIP, '1900');
  Result.MulticastTTL := 5;
  Result.AddMulticast('239.255.255.250');

3. These sockets where hosted in a TSocketList which I used to check if data is available using:

 

if FSocketList[0].GroupCanRead(fSockets, ftimeout, fActive) then

 

This worked well, but I would like to replace the whole code using the official Socket implementation instead. Anyhow I do not find a proper explanation how to use TSocket.Create(TSocketType.UDP, TEncoding.UTF8).

So if there is somebody who could point my nose to some code snippets, this would be great. I neither know how to get a list of the available network devices, nor how to bind the sockets or to send a request to socket I created first at step 2 

 

Cheers

Christian

 

Edited by CHackbart

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×