Writing Code

Top  Previous  Next

Below is your first project in its entirety. You can just copy/paste it into the main.tbs file.

The only line that will need your attention is the net.ip=" " line. You have to choose a suitable IP address for your device. (more)!

 

Few code comments:

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 the 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 (and C) — you accomplish complex things in very few lines of code.

 

Tibbo BASIC:

include "global.tbh"
 
sub on_sys_init()
  net.ip="192.168.1.222" '<—- set the correct IP
     
  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