Difference between revisions of "TWSocket.Listen"

From Overbyte
Jump to navigation Jump to search
(first entry)
 
 
(One intermediate revision by one other user not shown)
Line 7: Line 7:
 
== Description ==
 
== Description ==
  
'''Listen''' is either used on UDP sockets or on TCP-Server sockets. For UDP-sockets it makes receiving data possible, otherwise no [[TWSocket.OnDataAvailable|OnataAvailable]] would be triggered, for TCP-Servers it starts to watch for incomming new connections, which trigger [[TWSocket.OnSessionAvailable|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.
+
'''Listen''' is either used on UDP sockets or on TCP-Server sockets. For UDP-sockets it makes receiving data possible, otherwise no [[TWSocket.OnDataAvailable|OnDataAvailable]] would be triggered, for TCP-Servers it starts to watch for incomming new connections, which trigger [[TWSocket.OnSessionAvailable|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 ==
 
== Example ==
 
   MySocket:=TWSocket.create(nil);
 
   MySocket:=TWSocket.create(nil);
   MySocket.addr:='127.0.0.1';
+
   MySocket.addr:='127.0.0.1'; // localhost -> myself
 
   MySocket.port:='60000';
 
   MySocket.port:='60000';
 
   MySOcket.proto:='udp';
 
   MySOcket.proto:='udp';
   MySocket.Listen;
+
   MySocket.Listen; // incomming packets on port 60000 will trigger OnDataAvailable
  
 
== Best practices ==
 
== Best practices ==

Latest revision as of 17:40, 2 March 2011

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.