Accepting Incoming Connections
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 to 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 accepting an incoming connection from any host/port, then set 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 the 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 the 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, 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 sock.inconmode = 1 — PL_SOCK_INCONMODE_SPECIFIC_IPPORT.
Here is an example of how to only accept incoming TCP connections from a host with the IP address of 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: sock.localportlist and sock.httpportlist. Notice that both properties are of the string type, so each one can accept a list of ports.
For example, set sock.localportlist = "1001,2000,3000" to accept a normal data connection on port 1001, 2000, or 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, 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 sock.localportlist or one of the ports on sock.httpportlist. The socket will automatically switch into the HTTP mode if the connection is accepted on one of the ports from 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 a connection targets port 80, it will be accepted as an HTTP connection.
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 — sock.httpportlist has priority over sock.localportlist. Of course, for UDP, sock.httpportlist won't matter since HTTP is only possible over 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, the EM1000's is here).
The sock.allowedinterfaces property stores 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 set up, 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, to receive and send data).