Socket Selection

Top  Previous  Next

TiOS supports up to 16 sockets, but there may be platforms with less than 16 sockets available. You can obtain the number of sockets available for your platform using the sock.numofsock property.

Since there can be multiple sockets, you must state which socket are you referring to when changing properties or invoking methods. This is done using the sock.num property. For example:

 

 

sock.protocol = 1

 

 

Can you tell what socket the statement above applies to? Neither can the platform. Thus, the correct syntax would be:

 

 

sock.num = 0

sock.protocol = 1

 

 

Now the platform knows which socket you're working with. Once you have set the socket selector (using sock.num), every socket-specific method and property after that point is taken to refer to that socket. Thus:

 

 

sock.num = 0

sock.protocol = 1

sock.connectiontimeout = 10

sock.httpmode = 1 ' etc

 

 

The events for this object are not separate for each socket. An event such as on_sock_data_arrival serves all sockets on your platform. Thus, when an event handler for the socket object is entered, the socket selector is automatically switched to the socket number on which the event occurred:

 

 

sub on_sock_data_arrival

 dim s as string

 s = sock.getdata(255) ' Note that you did not have to specify any sock.num preceding this statement.

end sub

 

 

As a result of this automatic switching, when an event handler for a socket event terminates, the sock.num property retains its new value (nothing changes it back). You must take this into account when processing other event handlers which make use of a socket (and are not socket events). In other words, you should explicitly set the sock.num property whenever entering such an event handler, because the property might have been automatically changed prior to this event. To illustrate:

 

 

sub on_sys_init ' This is always the first event executed.

 sock.num = 0 ' Supposedly, this would make all subsequent properties and methods refer to this socket.

end sub

 

sub on_sock_data_arrival ' Then, supposing this event executes.

 dim s as string

 s = sock.getdata(255) ' However, this event happens on the second socket. So now sock.num = 1.

end sub

 

sub on_ser_data_arrival ' And then this serial port event executes.

 sock.txclear ' You meant to do this for sock.num = 0 (as specified at on_sys_init). But now sock.num was changed to 1! Oops...

end sub

 

 

To recap, only one of two things may change the current sock.num: (1) manual change or (2) a socket event. You cannot assume the number has remained unchanged if you set it somewhere else (because a socket event might have happened since).