I've never touched the networking commands before, I just learned today it had socket support. Decided to make a demo on how to use them.
This app will work as both server and client, operating on the localhost for simplicity so you don't have to mess with firewall settings or configure port forwarding on your router.
Upon launching the app, the server will start listening for incoming connections.
[keys]
c = connect the client to the server
space = sends a timestamp to the client
s = sends the timestamp back to the server, encoded in both hex and base64
esc = exists the program after closing the sockets
Yes, sockets are limited to only 1400 bytes per packet. This isn't necessarily an AppGameKit limitation, but a rather standard default packet size in most TCP situations. So what happens if you wish to send something larger than 1.4KB? You'd have to send the data in chunks and reassemble it on the receiving end. "Chunking" is a common practice when uploading large files over HTTP as a typical connection will timeout before the data can be sent be. It isn't too difficult to implement, it just requires a few extra steps. I plan to make another demo show how to do that.
// Project: socket
// Created: 2023-11-17
SetErrorMode(2)
SetWindowTitle( "socket" )
SetWindowSize( 1024, 768, 0 )
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
// The listener is our server. In this example, we will listen for incoming
// connections on the localhost at port 8080
listener = createSocketListener("127.0.0.1", 8080)
// This will be the server's connection to connected client. In a true server
// this would be an array as you'd potentially have multiple clients connecting.
// For this example, we'll only deal with a single client, ourself.
conn = 0
// The client's connection to the server.
client = 0
fromServer$ = ""
fromClient1$ = ""
fromClient2$ = ""
msg$ = "Client disconnected."
do
// Free the sockets on hitting ESC and exit the app
if getRawKeyPressed(27) = 1
deleteSocketListener(listener)
deleteSocket(conn)
deleteSocket(client)
end
endif
// Press 'c' to connect the client to the server
if getRawKeyPressed(67) = 1
client = connectSocket('127.0.0.1', 8080, 3000)
endif
// Press 'spacebar' to send timestamp from server to the client
if getRawKeyPressed(32) = 1
// Make sure the client is still connected
if getSocketConnected(conn) = 1
// Send our data to the buffer
sendSocketString(conn, str(GetMilliseconds()))
// Flush the data. Until you flush the socket, anything
// you write to the buffer will sit there.
// Caution: Once the buffer has reach 1.4KB, it will automatically flush the buffer.
flushSocket(conn)
endif
endif
// Press 's' to send data from the client to the server
if getRawKeyPressed(83) = 1
// Check client is still connected to server
if client > 0
if getSocketConnected(client) = 1
h$ = hex(val(fromServer$))
// While we had the server send us only 1 string,
// we're going to have the client send 2 strings back to the server.
// First string will take the timestamp this client received from the server and
// convert it to hex. The second string will be the hex value encoded in base64.
sendSocketString(client, h$)
sendSocketString(client, hexToBase64(h$))
flushSocket(client)
endif
endif
endif
// Until we have a client connected, the server will
// continue to listen for incoming connections. You
// would modify this to always listen if your server
// is to handle multiple clients at once.
if conn = 0
lc = getSocketListenerConnection(listener)
if lc > 0 then conn = lc
else
// If server is connected to a client, listen for data from that client
if getSocketConnected(conn) = 1
if getSocketBytesAvailable(conn) > 0
// This example expects 2 strings sent from the client
fromClient1$ = getSocketString(conn)
fromClient2$ = getSocketString(conn)
endif
endif
endif
// The client listens for incoming data from the server
if client > 0
// Make sure client is still connected to the server
if getSocketConnected(client) = 1
msg$ = "Connected to server at " + getSocketRemoteIP(client)
// Check if any data is awaiting to be read
if getSocketBytesAvailable(client) > 0
// In our example, we expect to get a string
fromServer$ = getSocketString(client)
endif
else
msg$ = "Client disconnected."
endif
endif
print(msg$)
print("")
print("Client received message from server: "+fromServer$)
print("")
print("Server received client's response 1: "+fromClient1$)
print("Server received client's response 2: "+fromClient2$)
Sync()
loop