Network
TCP and UDP connections, over IPv4 or IPv6: servers that clients connect to, and connections to servers, on this computer or anywhere. Nothing waits: a program checks for events (NetworkServerEvent, NetworkClientEvent) in its loop and reads what has arrived.
- A server is a number (0, 1, … or
#PB_Any); a connection is a value returned byOpenNetworkConnectionorEventClient. - Names (
"example.com") are looked up the way the system does. On Linux, x does it itself:/etc/hosts, then the nameservers in/etc/resolv.conf. - TLS (encrypted connections, the
#PB_Network_TLS…flags andUseNetworkTLS) isn't in x yet: a call with a TLS flag fails. - Compatible with PureBasic 6.50: constants, events and their order, IP address values, TCP and UDP. Strings are sent as UTF-8.
| Flag | |
|---|---|
#PB_Network_TCP (default), #PB_Network_UDP |
the protocol |
#PB_Network_IPv4 (default), #PB_Network_IPv6 |
the address family |
| Event | |
|---|---|
#PB_NetworkEvent_None (0) |
nothing |
#PB_NetworkEvent_Connect (1) |
a client connected to a server (TCP) |
#PB_NetworkEvent_Data (2) |
data arrived, to read with ReceiveNetworkData |
#PB_NetworkEvent_Disconnect (4) |
the other end closed the connection (TCP) |
Servers
CreateNetworkServer ✅
Result = CreateNetworkServer(#Server, Port [, Flags [, BoundIP$]])
Starts a server on Port. BoundIP$ limits it to one of this computer's addresses ("127.0.0.1": only this computer can connect). Returns zero if it can't (the port is in use, for one).
NetworkServerEvent ✅
Result = NetworkServerEvent([#Server])
What has happened at the servers (or at the one given), one event per call: #PB_NetworkEvent_Connect, _Data, _Disconnect or _None. A client that disconnects is closed by x: don't call CloseNetworkConnection for it. With UDP, a "client" is each address that sends to the server; it never connects or disconnects.
EventServer ✅, EventClient ✅
Server = EventServer()
Connection = EventClient()
The server and the client of the last event.
GetClientIP ✅, GetClientPort ✅
IP = GetClientIP(Connection)
Port = GetClientPort(Connection)
A client's address and port. An IPv6 address must be freed with FreeIP.
CloseNetworkServer ✅, ServerID ✅
CloseNetworkServer(#Server)
Result = ServerID(#Server)
Stops the server (its clients are disconnected); the system's socket.
Connections
OpenNetworkConnection ✅
Connection = OpenNetworkConnection(ServerName$, Port [, Flags [, TimeOut [, LocalIP$ [, LocalPort]]]])
Connects to a server, by name or address, waiting up to TimeOut milliseconds (15 seconds if not given). Returns zero if it can't. A UDP connection only sets where its data goes.
NetworkClientEvent ✅
Result = NetworkClientEvent(Connection)
What has happened on the connection: #PB_NetworkEvent_Data, _Disconnect (once), or _None.
ReceiveNetworkData ✅
Result = ReceiveNetworkData(Connection, *Buffer, Length)
Reads up to Length bytes that have arrived. Returns the number read: Length means there may be more; 0 nothing (or the connection closed); -1 an error.
SendNetworkData ✅, SendNetworkString ✅
Result = SendNetworkData(Connection, *Buffer, Length)
Result = SendNetworkString(Connection, Text$ [, Format])
Sends bytes, or a string (#PB_UTF8 by default, #PB_Ascii or #PB_Unicode, without a terminating zero). Returns the number of bytes sent, -1 if none could be.
CloseNetworkConnection ✅, ConnectionID ✅
CloseNetworkConnection(Connection)
Result = ConnectionID(Connection)
Closes the connection (a server's client is told: _Disconnect); the system's socket.
A server and a client in one program:
OpenConsole()
If CreateNetworkServer(0, 6840, #PB_Network_TCP, "127.0.0.1")
c = OpenNetworkConnection("127.0.0.1", 6840)
*buffer = AllocateMemory(1000)
Repeat
Select NetworkServerEvent()
Case #PB_NetworkEvent_Connect
PrintN("a client connected")
SendNetworkString(c, "Hello, server")
Case #PB_NetworkEvent_Data
n = ReceiveNetworkData(EventClient(), *buffer, 1000)
PrintN("the server got: " + PeekS(*buffer, n, #PB_UTF8 | #PB_ByteLength))
SendNetworkString(EventClient(), "Hello, client")
Break
Default
Delay(5)
EndSelect
ForEver
Repeat
Delay(5)
Until NetworkClientEvent(c) = #PB_NetworkEvent_Data
n = ReceiveNetworkData(c, *buffer, 1000)
PrintN("the client got: " + PeekS(*buffer, n, #PB_UTF8 | #PB_ByteLength))
CloseNetworkConnection(c)
CloseNetworkServer(0)
EndIf
Output:
a client connected
the server got: Hello, server
the client got: Hello, client
IP addresses
An IPv4 address is a number, its first field in the lowest byte. An IPv6 address is 16 bytes in memory, made by MakeIPAddress, NextIPAddress or GetClientIP: free it with FreeIP.
MakeIPAddress ✅, IPAddressField ✅, IPString ✅, FreeIP ✅
IP = MakeIPAddress(Field0, Field1, Field2, Field3 [, Field4, Field5, Field6, Field7])
Result = IPAddressField(IP, Field [, Format])
Text$ = IPString(IP [, Format])
FreeIP(IP)
Eight fields make an IPv6 address (each a 16-bit part). Format: #PB_Network_IPv4 (default) or #PB_Network_IPv6. IPv6 text is written the short way (::1).
OpenConsole()
ip = MakeIPAddress(192, 168, 1, 20)
PrintN(IPString(ip) + " = $" + Hex(ip) + ", third field " + Str(IPAddressField(ip, 2)))
ip6 = MakeIPAddress($2001, $db8, 0, 0, 0, 0, 0, 1)
PrintN(IPString(ip6, #PB_Network_IPv6))
FreeIP(ip6)
Output:
192.168.1.20 = $1401A8C0, third field 1
2001:db8::1
HostName ✅
Name$ = HostName()
This computer's name.
ExamineIPAddresses ✅, NextIPAddress ✅
Result = ExamineIPAddresses([Format])
IP = NextIPAddress()
Goes through this computer's addresses (not the loopback 127.0.0.1 / ::1); NextIPAddress returns 0 after the last.
Not yet available 🔜
- TLS:
UseNetworkTLSand the#PB_Network_TLS…flags.