This function creates a new client and initializes a connection with the specified host.
CreateClient(host: string, port: number): Client
host
: IP address to connect toport
: (optional) port number to use, default is 8888If a connection can be initialized, a new client is returned. Otherwise, nil
is returned.
Note that the client will not actually be connected to the host until a MESSAGE_CONNECT
message is returned by Client:Update
.
This example shows one of the simplest possible networked programs. The order of events is as follows:
port = 8888
messageID = 101
--Create the server
server = CreateServer(port)
if server == nil then RuntimeError("Failed to create server") end
--Create a client and connect
client = CreateClient("127.0.0.1", port)
if client == nil then RuntimeError("Failed to create client") end
while true do
--Evaluate server messages
msg = server:Update()
if msg.id == MESSAGE_CONNECT then
Print("Client connected")
elseif msg.id == messageID then
Print(msg.data:PeekString(0))
server:Disconnect(msg.source)
end
--Evaluate client messages
msg = client:Update()
if msg.id == MESSAGE_DISCONNECT then
Print("Disconnected from server")
return 0
elseif msg.id == MESSAGE_CONNECT then
client:Send(messageID, "Hello, Server!")
end
Sleep(5)
end
return 0