PI.TCP - jedimatt42/tipi GitHub Wiki
TIPI supports socket access through DSR file access.
It supports the following operations:
- open
- close
- read
- write
"PI.TCP=" <address> ":" <port>
Follow the PI.TCP=
with the address, a colon and the port number you want to connect to. In TI BASIC this looks like:
10 OPEN #1:"PI.TCP=www.google.com:80"
The device should be opened in VARIABLE
mode. The record length will determine the amount of data that can be returned per read. If no length is specified when opened, then the default record length will be 80.
Reading from the socket:
20 INPUT #1:A$
BASIC will parse input into variables, so if you can use LINPUT in XB you will be less astonished.
Write the same as you would to other devices:
30 PRINT #1:"Hello World"
Record writes do not add any unspecified characters to the stream.
Finally close the socket:
40 CLOSE #1
Server sockets can be opened to listen for incoming connections. The device name format:
"PI.TCP=" <interface-addr> ":" <port> "." ("BIND"|<handle>)
Open a listening port with the .BIND
suffix. The interface-addr
may be *
to bind to all network interfaces, such as if you have an ethernet connection and wifi connection in the PI.
10 OPEN #1:"PI.TCP=*:8080.BIND"
To accept an incoming connection, read a numeric value that will become the suffix for opening the stream once a connection is made. If no connection is available, 0 is returned.
20 INPUT #1:HANDLE
30 IF HANDLE=0 THEN 20
Once a non-zero connection response is made, the stream to that connection can be opened. If 255 is returned, there is a problem with the bound port, and it might have to be closed and will have to be re-established.
40 OPEN #2:"PI.TCP=*:8080."&STR$(HANDLE)
Now data may be exchanged with read and write operations.
50 PRINT #2:"Hello, I am a TI-99/4A"&CHR$(13)&CHR$(10)
60 LINPUT #2:LINE$
70 PRINT LINE$
When done with a connection, it can be closed per normal, and the listening port is still active with the other open device. You can accept many incoming connections or let them queue handling one at a time.
80 CLOSE #2
90 GOTO 20
To release the listening port close the original device that had the .BIND
suffix
1 ON ERROR 1000
1000 CLOSE #1
Here is a complete example mini-bbs with error handling in 'Tidbit' style Extended BASIC:
BEGIN:
ON ERROR BINDERR
OPEN #1:"PI.TCP=*:8080.BIND"
PRINT "Bound server port 8080"
POLCON:
ON ERROR SOCERR
INPUT #1:HANDLE
IF HANDLE=255 THEN ACCERR
IF HANDLE<>0 THEN GETCON
CALL SOUND(1000,40000,30)
GOTO POLCON
GETCON:
OPEN #2:"PI.TCP=*:8080."&STR$(HANDLE)
PRINT #2:"Hello, I am a TI-99/4A"&CHR$(13)&CHR$(10)
PRINT #2:"Who are you? "
GETNAME:
LINPUT #2:LINE$
IF LINE$="" THEN GETNAME
PRINT "Login from: "&LINE$
PRINT #2:"Hello "&LINE$&CHR$(13)&CHR$(10)
CLOSE #2
GOTO POLCON
ACCERR:
PRINT "Accept Error"
SOCERR:
PRINT "Closing port"
ON ERROR SOCE2
CLOSE #1
SOCE2:
CALL SOUND(1000,40000,30)
GOTO BEGIN
BINDERR:
CALL SOUND(3000,40000,30)
GOTO BEGIN