Skip to content

Single-header, cross-platform TCP/UDP socket wrapper in c++

License

Notifications You must be signed in to change notification settings

kaniteru/SimpleSocket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SimpleSocket

Easy to use C++ TCP/UDP socket wrapper

Features

  • Simple Usage
  • Single Header
  • Cross-Platform
    • Windows
    • Linux
    • Unix (macOS)
    • ...
  • IPv4 and IPv6 Support
  • C++98 Standard Support

Usage

You can see more example projects here.

#include <simple_socket.hpp>
kani::SocketInfo info;
info.m_node = "localhost"; // host name or ip address
info.m_service = 1234;     // node name or port number

TCP Server

kani::TcpServerSocketInfo tcpInfo;
info.m_node = "localhost";
info.m_service = 1234;
info.m_backlog = SOMAXCONN;

kani::TcpServer server(tcpInfo);

if (!server.is_valid() || server.start() != kani::SS_START_RESULT_SUCCESS) {
    return;
}

TcpNetClient client;

while (!server.wait_client(&client)) { }

kani::SendMsg msg("hello world!");
server.send_msg(&client, &msg);

kani::RecvMsg response;
server.recv_msg(&client, &response);

server.stop();

TCP Client

kani::TcpClient client(info);

if (!client.is_valid() || client.start() != kani::SS_START_RESULT_SUCCESS) {
    return;
}

if (!client.connect()) {
    return;
}

kani::SendMsg msg("hello world!");
client.send_msg(&msg);

kani::RecvMsg response;
client.recv_msg(&response);

client.stop();

UDP Server

kani::UdpServer server(info);

if (!server.is_valid() || server.start() != kani::SS_START_RESULT_SUCCESS) {
    return;
}

NetAddr client;

kani::RecvMsg msg;
server.recv_msg(&client, &msg);

kani::SendMsg response("hello world!");
server.send_msg(&client, &response);

server.stop();

UDP Client

kani::UdpClient client(info);

if (!client.is_valid() || client.start() != kani::SS_START_RESULT_SUCCESS) {
    return;
}

kani::SendMsg msg("hello world!");
client.send_msg(&msg);

kani::RecvMsg response;
client.recv_msg(&response);

client.stop();

Todo

  • Add more detailed error types to eSSMsgStatus
  • Add more example projects
    • Thread safe TCP/UDP Server interacting with multiple clients

License

Unless otherwise specified in subfolders or files, all files in this repository are distributed under the MIT License.

See LICENSE.txt for more information.

[BACK TO TOP]