CrossplatformSocketFramework (CSF) is a cross-platform C++ wrapper for network sockets.
Platform | ||
---|---|---|
Linux | Windows | MacOS |
CSFSocket is a superclass which can be extended to write your own socket wrapper implementation!
-
void Connect(std::string host, int port)
Description: Attempts to connect to a server. Can throw specific exceptions depending on the error.
Parameters:
host: IP address of the server to connect to
port: Port number to connect to -
void Bind(std::string host, int port)
Description: When building a server, use this function to bind a socket to an address described by parameters host and port. Socket will be able to listen for incoming connections on this address.
Parameters:
host: IP address of the server to connect to
port: Port number to connect to -
void Listen(int connections)
Description: When building a server, use this function to listen for incoming connections after binding the socket to an address.
Parameters:
connections: Number of connections server can handle simultaneously. -
void Accept()
Description: Accepts an incoming connection. -
void Close()
Description: Terminates the connection and closes the network socket. -
void SetBlocking(bool blocking)
Description: Sets blocking property of the socket. If socket is blocking, whenRecv
is called, it hangs the thread until incoming data is received. Nonblocking socket would let program continue execution even if no incoming data is available at the time.
Parameters:
blocking: Specifies blocking state in which to put the socket. -
size_t Recv(void* result, int bufsize)
Description: Checks if there is any incoming data available to read. If the socket is blocking, thread execution will be held until data becomes available. If the socket is nonblocking, even if no data is available, program will continue execution.
Parameters:
result: Buffer which received data will be put into
bufsize: Specifies number of bytes to be readReturns: Length of the received data in bytes
-
std::string Recv(int bufsize, size_t* outbytes)
Description: Checks if there is any incoming data available to read. If the socket is blocking, thread execution will be held until data becomes available. If the socket is nonblocking, even if no data is available, program will continue execution.
Parameters:
bufsize: Specifies number of bytes to be read
outbytes: Pointer to a variable which holds length of received data in bytesReturns: String object formed from the received data
-
size_t Send(void* data, int len)
Description: Sends data over the network
Parameters:
data: Pointer to the data to send
len: Number of bytes to sendReturns: If data was sent successfully, number of bytes sent is returned, otherwise -1 is returned and error is written to
errno
-
size_t Send(std::string data)
Description: Sends data over the network
Parameters:
data: String data to sendReturns: If data was sent successfully, number of bytes sent is returned, otherwise -1 is returned and error is written to
errno
-
void* GetNativeHandle()
Description: Returns pointer to a native handle object. On different platforms handle object will have a different data type.
std::shared_ptr<CSFSocket> socket = CreateSocket(); // Create socket object
socket->Bind("0.0.0.0", 4500); // Bind the socket
socket->Listen(1); // Listening for 1 connection
socket->Accept(); // Accepting incoming client connection
size_t len;
std::string clientmsg = socket->Recv(4096, &len); // Receive client message
socket->Send("Message Received!"); // Send client the reply message
socket->Close(); // Shutting down the connection
std::shared_ptr<CSFSocket> socket = CreateSocket(); // Create socket object
socket->Connect("301.0.113.100", 4500); // Connect to a server
socket->Send("Hello from client!"); // Send client the reply message
std::string clientmsg = socket->Recv(4096, nullptr); // Receive server response
socket->Close(); // Shutting down the connection