Search code examples
delphiudpdelphi-7

Udp Socket handling


I want to use UDP Socket to simply send an audio stream. My problem is that i can't get it working i thought it would be simpler then using TCP IP.

What i did i droped a UDPSocket component on my form and for the server part i used this code

  with udpServer do
  begin
    LocalHost := '127.0.0.1';
    LocalPort := '5002';
    Open();
    Active := True;
  end;

For the client application this :

   with udpClient do
      begin
        RemoteHost := '192.0.168.100'; //my local address
        RemotePort := '5002';
        Open();
        Active := True;
      end;

The problem is i am not receving anything. What i am doing wrong i don't have any third-party software that can block the connection.

I didn't find any suitable example for using this component any source of inspiration will be greatly appreciated.


Solution

  • You have the server and the client connect on the same IP.

    Usually if you set the server application IP address to 0.0.0.0 it will bind to any available IP address on the given port, including 127.0.0.1.

    Then the client must connect to one of the bound IPs. Instead, you had the server listening on 127.0.0.1 and the client connect to 192.0.168.100.

    Don't be fooled by the "LocalHost" property name. "Local" here just mean you have to set a "local" IP, an IP assigned to the local machine, not a "remote" (of another machine) one, while the client of course will connect to a "remote" IP, that of the server.

    127.0.0.1 is a good choice if and only if you want your server to be available only to local application, because that IP scope is limited to the same machine. If you want to make it available outside the machine, you have to bind it to a valid IP.

    Whatever issue you have, tools like Wireshark or Microsoft Network Monitor are very useful to understand what's going on.