This function creates a new client and initializes a connection with the specified host.
Parameter | Description |
---|---|
host | IP address to connect to |
port | port number to use |
If a connection can be initialized a new client is returned, otherwise NULL is returned.
Note that the client will not actuallly 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:
#include "UltraEngine.h"
using namespace UltraEngine;
int main(int argc, const char* argv[])
{
const int port = 8888;
const int messageID = 101;
//Create the server
auto server = CreateServer(port);
if (server == NULL) RuntimeError("Failed to create server");
//Create a client and connect
auto client = CreateClient("127.0.0.1", port);
if (client == NULL) RuntimeError("Failed to create client");
while (true)
{
//Evaluate server messages
auto msg = server->Update();
switch (msg.id)
{
case MESSAGE_CONNECT:
Print("Client connected");
break;
case messageID:
Print(msg.data->PeekString(0));
server->Disconnect(msg.source);
break;
}
//Evaluate client messages
msg = client->Update();
switch (msg.id)
{
case MESSAGE_DISCONNECT:
Print("Disconnected from server");
return 0;
break;
case MESSAGE_CONNECT:
client->Send(messageID, "Hello, Server!");
break;
}
Sleep(5);
}
return 0;
}