Step 2: Adding TCP Comms

Top  Previous  Next

This and other projects in the Code Examples section are published on our website under the name "test_wln_lib".


Now let's take the previous example and create a TCP socket that will accept connections over the Wi-Fi interface. We will make a simple loopback socket that echoes back the data.



May we suggest our very own I/O NINJA terminal/sniffer software as the tool for testing the loopback socket? Hint: use Connection Socket plugin.


Only main.tbs needs to be changed from the previous example. The socket is configured in on_sys_init(). Notice how we don't just pick a random socket number to use. We obtain the socket from the SOCK library by calling sock_get(). This is very important! Once any part of your project uses the SOCK library (and WLN library does), all other parts of your project must do the same. Mess will ensue if you fail to handle socket numbers correctly.

In the code below, we first call wln_start() and then configure the loopback socket. There is no significance to this order and you can reverse it. Same goes for on_sock_data_arrival(). There is no special reason to call wln_proc_data() first, and handle the loopback socket next. You can reverse the order and everything will still work.




include "global.tbh"


dim tcp_sock as byte



sub on_sys_init()

 wln.ip="" '<--- set suitable IP here




 end if


 'configure loopback socket











end sub



sub on_sys_timer()


end sub



sub on_sock_data_arrival()



 'loopback tcp data

 if sock.num=tcp_sock then



 end if

end sub



sub on_wln_task_complete(completed_task as pl_wln_tasks)


end sub



sub on_wln_event(wln_event as pl_wln_events)


end sub