TWSocket.Listen

From Overbyte
Jump to: navigation, search

Main page -> ICS component reference -> TWSocket -> Listen

Definition

method Listen;

Description

Listen is either used on UDP sockets or on TCP-Server sockets. For UDP-sockets it makes receiving data possible, otherwise no OnDataAvailable would be triggered, for TCP-Servers it starts to watch for incomming new connections, which trigger OnSessionAvailable. Listen does not block the calling thread. It may throw a exception if the ip/port combination the socket should listen on is already in use with the same protocol (TCP/UDP). Some already by TCP used ip/port combination can be used simultaniously by a UDP connection as well.

Example

 MySocket:=TWSocket.create(nil);
 MySocket.addr:='127.0.0.1'; // localhost -> myself
 MySocket.port:='60000';
 MySOcket.proto:='udp';
 MySocket.Listen; // incomming packets on port 60000 will trigger OnDataAvailable

Best practices

  • set up addr and port before calling listen
  • put a try/except around the listen call

How to

listen on all network interfaces simultaniously

When you specify 0.0.0.0 as addr, you can listen on the specified port on all available network connections at the same time with the same socket.