Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IdUDPServer issue
#1
I use an IdUDPServer to open multiple Ports like this:
Code:
IdUDPServer1.Active := false;

Socket1 := IdUDPServer1.Bindings.Add;
     Socket1.IPVersion := Id_IPv4;
     Socket1.IP := HyteraSettings.TCPLocalIP;
     Socket1.Port := HyteraSettings.RRSPort; // RRS PORT

     if HyteraSettings.Debug then LocalLog('UDPServer Bindings.Add: '+HyteraSettings.TCPLocalIP+' Port '+IntToStr(HyteraSettings.GPSPort));
     Socket1 := IdUDPServer1.Bindings.Add;
     Socket1.IPVersion := Id_IPv4;
     Socket1.IP := HyteraSettings.TCPLocalIP;
     Socket1.Port := HyteraSettings.GPSPort; // GPS PORT
Then I set Active to TRUE.

Afterwards I got an exception caused by the other side: , UDPServer Exception: Socket Error # 10054Connection reset by peer.
I then call IdUDPServer1.Active := false; and I completely FreeAndNil it.

Then the loop starts again from fresh, with a newly created UDPServer.
However, now I get the Exception: Could not bind socket. Address and port are already in use

Why does this happen? Should I somehow manually clear the sockets before setting the Active to False?

Bart
---
Bart Kindt
CEO and Developer
SARTrack Limited
New Zealand
www.sartrack.nz
Reply
#2
(11-12-2018, 07:03 PM)BartKindt Wrote: Afterwards I got an exception caused by the other side: , UDPServer Exception: Socket Error # 10054Connection reset by peer.

You can't get that kind of error with UDP, since there is no connection.

You are binding your UDP server to an IP address that has "TCP" in its settings name. Are you, by chance, trying to bind the UDP server to the same IP/Port that is also being used by a TCP client/server? Not all platforms support that (Windows does, though).

(11-12-2018, 07:03 PM)BartKindt Wrote: I then call IdUDPServer1.Active := false; and I completely FreeAndNil it.

Then the loop starts again from fresh, with a newly created UDPServer.
However, now I get the Exception: Could not bind socket. Address and port are already in use

Why does this happen?

Because the previously bound IP/Port is still in use by the OS, obviously. The OS does not lie about that. Just because a socket is closed does not always mean its bound IP/Port is released right away. It may take a few minutes.

For your purposes, since you are running a server, try setting its ReuseSocket property to rsTrue. It is set to rsOSDependent by default, which resolves to rsTrue on Unix systems, and rsFalse on others (such as Windows).

(11-12-2018, 07:03 PM)BartKindt Wrote: Should I somehow manually clear the sockets before setting the Active to False?

No, not before. But, you typically should clear the Bindings after setting Active to False and before repopulating the Bindings. But, if you are not going to change any settings, you just want to reactivate using the same IP/Port pairs, then you don't need to clear the Bindings.

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)