Accepting Incoming Connections
|Top Previous Next|
Master switch for incoming connections
It is possible to globally enable or disable acceptance of incoming connections on all sockets, irregardless of the setup of individual sockets. This is done using the sock.inconenabledmaster property. By default, this property is set to 1- YES.
Defining who can connect to your socket
The sock.inconmode ("incoming connections mode") property allows you do define whether incoming connections will be accepted and, if yes, from who. By default (0- PL_SOCK_INCONMODE_NONE), incoming connections are not allowed at all. For TCP this means that incoming connection requests will be rejected. For UDP, incoming datagrams will simply be ignored.
If you don't mind to accept an incoming connection from any host/port then set the sock.inconmode= 3- PL_SOCK_INCONMODE_ANY_IP_ANY_PORT. This way, whoever wants to connect to your socket will be able to do so as long as this party is using correct transport protocol (you define this through the sock.protocol property) and is connecting to the right port number (more on this below).
If you are only interested in accepting connections from a particular host on the network then set sock.inconmode= 2- PL_SOCK_INCONMODE_SPECIFIC_IP_ANY_PORT. This way, only the host with IP matching the one defined by the sock.targetip property will be able to connect to your socket.
You can restrict things further and demand that not only the IP of the other host must match the one you set in sock.targetip property, but also the port from which the connection is being originated must match the port defined by the sock.targetport property. To achieve this, set the sock.inconmode= 1- PL_SOCK_INCONMODE_SPECIFIC_IPPORT.
Here is an example of how to only accept incoming TCP connections from host 192.168.100.40 and port 1000:
The sock object rejects an incoming connection by sending out a reset TCP packet. This way, the other host is instantly notified of the rejection. There is an exception to this -- see Socket Behavior in the HTTP Mode.
Ports on which your socket will accept an incoming connection are called "listening ports". These are defined by two properties: the sock.localportlist and the sock.httpportlist. Notice that both properties are of string type, so each one can accept a list of ports.
For example, to accept a normal data connection either on port 1001, port 2000, or port 3000, set the sock.localportlist= "1001,2000,3000". Once the connection is in progress, you can check which of the socket's local ports is actually engaged in this connection. This is done through the sock.localport read-only property.
For UDP connections, the sock.localportlist is all there is. For TCP, which can be used for "plain vanilla data connections" or for HTTP, you have another property- sock.httpportlist. To be accepted by your socket, an incoming TCP connection has to target either one of the ports on the sock.localportlist, or one of the ports on the sock.httpportlist. The socket will automatically switch into the HTTP mode if the connection is accepted on one of the ports from the sock.httpportlist.
Here is an example:
sock.localportlist = "1001,2000"
sock.httpportlist = "80"
The above means that any incoming TCP connection that targets either port 1001 or port 2000 will be interpreted as a regular data connection. If connection target port 80 it will be accepted as an HTTP connection.
And what if the same port is listed both under the sock.localportlist and sock.httpportlist?
sock.localportlist = "1001,2000,80"
sock.httpportlist = "80"
In this example, if there is an incoming connection targeting "our" port 80 and the protocol is TCP then the mode will be HTTP- the sock.httpportlist has priority over sock.localportlist. Of course, for UDP the sock.httpportlist won't matter since the HTTP is only possible on TCP!
Setting allowed interfaces
The sock object is interface-independent and supports communications over more than one interface. The sock.allowedinterfaces property defines the list of interfaces on which the current socket will accept incoming connections. The list is different and depends on the platform. To find out what interfaces are available, refer to your device's platform documentation (for example, EM1000's is here).
The sock.allowedinterfaces property stored the string that lists all interfaces that the socket will listen on:
sock.allowedinterfaces = "NET,WLN" 'listen on Ethernet and Wi-Fi interfaces
Connections accepted even when the VM is paused
Once the socket has been setup it will accept an incoming connection even when the VM is paused (for example, has stopped on your breakpoint). All communications are handled by the master process, so the socket does not need the VM to accept an incoming connection (or, for that matter, receive and send the data).