PBXB64 Networking
TCP & UDP
Standard
Full TCP and UDP networking with TCP OPEN/CLOSE/SEND/RECV/LISTEN/ACCEPT and UDP OPEN/CLOSE/SEND/RECV. Build servers, clients, and peer-to-peer applications. Real Win32 socket API (WSAStartup, socket, bind, listen, accept, send, recv).
TCP Client & Server
TCP client example
#COMPILE EXE
#DIM ALL
FUNCTION PBMAIN() AS LONG
LOCAL buffer AS STRING
' Connect to server (host is given after AT, 3 s send/receive timeout)
TCP OPEN PORT 8080 AT "192.168.1.1" AS #1 TIMEOUT 3000
' Send data
TCP SEND #1, "Hello Server"
' Receive response (up to 1024 bytes in one call)
TCP RECV #1, 1024, buffer
PRINT "Server said: "; buffer
TCP CLOSE #1
FUNCTION = 0
END FUNCTION
TCP OPEN PORT n AT host AS #1 TIMEOUT ms creates a client connection. TCP SEND sends a string. TCP RECV #1, count, buf receives up to count bytes. TCP CLOSE releases the socket. TCP LISTEN/ACCEPT for server-side.
UDP - Connectionless
UDP send/receive
#COMPILE EXE
#DIM ALL
FUNCTION PBMAIN() AS LONG
LOCAL buffer AS STRING
LOCAL peerAddr AS QUAD
LOCAL peerPort AS QUAD
LOCAL dest AS QUAD
' Open UDP socket bound to local port 5000 (3 s timeout)
UDP OPEN PORT 5000 AS #1 TIMEOUT 3000
' Send to peer: address comes from HOST ADDR, port 5001
HOST ADDR "192.168.1.2" TO dest
UDP SEND #1, AT dest, 5001, "Hello UDP!"
' Receive from any peer: sender address and port are returned
UDP RECV #1, FROM peerAddr, peerPort, buffer
PRINT "Got: "; buffer; " from port "; peerPort
UDP CLOSE #1
FUNCTION = 0
END FUNCTION
UDP is connectionless - each send specifies the destination address and port; the packed address comes from HOST ADDR. UDP RECV ... FROM addr, port, buf returns the sender address and port. Ideal for discovery, streaming, and lightweight protocols.
HOST ADDR / HOST NAME
DNS resolution
#COMPILE EXE
#DIM ALL
FUNCTION PBMAIN() AS LONG
LOCAL ip AS QUAD ' packed IPv4 value
LOCAL name AS STRING
HOST ADDR "smart-ai-robot.com" TO ip
PRINT "IP: "; (ip AND 255); "."; ((ip \ 256) AND 255); "."; _
((ip \ 65536) AND 255); "."; ((ip \ 16777216) AND 255)
HOST NAME ip TO name ' reverse lookup of the packed address
PRINT "Name: "; name
HOST NAME TO name ' local computer name
PRINT "This computer: "; name
FUNCTION = 0
END FUNCTION
HOST ADDR resolves a hostname to a packed IPv4 address. HOST NAME returns the local host name, or performs a reverse lookup when given an address. Both use real gethostbyname/getaddrinfo Win32 APIs.
PBXB64 is part of the PBX-Compiler package. Free for private use, cleanly licensed for business.