Socket
Add this in your code:
#include <engine/network/network.h>
Description
Class to send and received data to/from a server.
Created with the NetworkManager class.
The socket will be closed when the object is destroyed.
Public methods
SendData (string)
Send data as a string to the server.
Parameters:
text
: Text to send
void SendData(const std::string& text)
Code sample:
// Fake ip
std::shared_ptr<Socket> socket = NetworkManager::CreateSocket("192.168.1.10", 6004);
if(socket)
{
socket->SendData("Hello World!");
}
SendData (binary)
Send binary data to the server.
Parameters:
data
: Pointer to the data to sendsize
: Size on byte to send
void SendData(const char* data, int size)
Code sample:
// Fake ip
std::shared_ptr<Socket> socket = NetworkManager::CreateSocket("192.168.1.10", 6004);
struct MyStruct
{
int value0 = 1;
int value1 = 2;
};
MyStruct myStruct = MyStruct();
if(socket)
{
socket->SendData(reinterpret_cast<char*>(&myStruct), sizeof(MyStruct));
}
Close
Send data as a string to the server.
void Close()
Code sample:
// Fake ip
std::shared_ptr<Socket> socket = NetworkManager::CreateSocket("192.168.1.10", 6004);
if(socket)
{
socket->SendData("Hello World!");
socket->Close();
}
GetIncommingData
Return recieved data since the last GetIncommingData call.
std::string GetIncommingData()
Code sample:
// Fake ip
std::shared_ptr<Socket> socket = NetworkManager::CreateSocket("192.168.1.10", 6004);
if(socket)
{
std::string data = socket->GetIncommingData();
}