Writing Code |
Top Previous Next |
Below is the entire project. You can just copy/paste it into the main.tbs file -- it will be opened and "in front of you" when you create your tcp_echo project.
The only line that will probably need changing is the net_ip="" line. Choose a suitable IP address for your device and set it here. Obviously, the address you choose should be unoccupied -- you can verify this by pinging this IP and making sure nobody responds.
We don't want to go into a lengthy explanation here... just a few comments on how this works:
•The sys_on_sys_init event handler prepares a single listening TCP socket.
•The sock_on_sock_data_arrival event handler takes whatever is received by the TCP socket and sends it back.
•The sock_on_sock_event event handler is not actually necessary for the echo application. We added this because we wanted to have this proverbial "HELLO WORLD" somewhere. The string is sent whenever a TCP connection is established.
Notice how small your first Tibbo BASIC program is! This is the power of Tibbo BASIC -- you accomplish complex things in just several lines of code.
include "global.tbh"
sub on_sys_init() sys.halt
net.ip="192.168.1.222"
sock.rxbuffrq(1) sock.txbuffrq(1) sys.buffalloc
sock.protocol=PL_SOCK_PROTOCOL_TCP sock.localportlist="1000" sock.inconmode=PL_SOCK_INCONMODE_ANY_IP_ANY_PORT sock.reconmode=PL_SOCK_RECONMODE_3 end sub
sub on_sock_data_arrival() dim s as string
s=sock.getdata(sock.txfree) sock.setdata(s) sock.send end sub
sub on_sock_event(newstate as enum pl_sock_state,newstatesimple as enum pl_sock_state_simple) if newstatesimple=PL_SSTS_EST then sock.setdata("HELLO WORLD!") sock.send end if end sub |