https://github.com/42loco42/simplesockets
An API for sockets on Linux. Contains packets, easier address resolution and both single and multi connection servers and clients.
https://github.com/42loco42/simplesockets
Last synced: 3 months ago
JSON representation
An API for sockets on Linux. Contains packets, easier address resolution and both single and multi connection servers and clients.
- Host: GitHub
- URL: https://github.com/42loco42/simplesockets
- Owner: 42LoCo42
- License: gpl-3.0
- Created: 2019-11-15T10:04:24.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-12-26T15:45:25.000Z (over 5 years ago)
- Last Synced: 2025-01-13T04:29:55.459Z (4 months ago)
- Language: C++
- Size: 41 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# simplesockets - Tutorial
This tutorial doesn't cover everything, please refer to the header files for more operations.
1. Basics
2. Address
3. Packet
4. SingleClient
5. SingleServer
6. MultiServer# 1. Basics
Choose which header to use:
- Are you a client? Use `singleclient.h`
- Do you serve only a single client at a time? `Use singleserver.h`
- Do you serve mutliple clients at once? Use `multiserver.h`
All headers include `address.h` and `packet.h`, which we will cover now.# 2. Address
If you are a client:
```C++
Address my_addr("", "");
```
If you are a server (type doesn't matter):
```C++
Address my_addr(nullptr, "");
```
since a server doesn't connect, only listen on localhost.# 3. Packet
simplesockets transfers data using a simple packet system.
A packet doesn't actually store the data, only the header.
Therefore, your data object must be permanent.
```C++
// Won't work
Packet p("some data");// Will work
std::string data = "some data";
Packet p(data);
```# 4. SingleClient
```C++
Address addr("www.example.com", "42");
SingleClient client;
if(!client.connect(addr)) {
// error handling
}std::string data = "hello";
client.sendPacket(data);
client.recvPacket(data); // data is now changed
```# 5. SingleServer
Identical to SingleClient except for the name.
```C++
Address addr(nullptr, "42");
SingleServer server;
if(!server.start(addr)) {
// error handling
}// Packet handling identical to SingleClient
```# 6. MultiServer
This one is interesting: `start()` doesn't immediately listen to client connections.
Instead, `recvPacket()` is used both to receive data from clients as well as to handle connecting and disconnecting clients.
```C++
Address addr(nullptr, "42");
MultiServer server;
if(!server.start(addr)) {
// error handling
}std::string data;
Packet p(data);while(true) {
// Storage for MultiServer status information
size_t client_id; // indicates the origin of the received data
ConInfo info; // indicates the type of the received dataserver.recvPacket(p, client_id, info);
switch(info) {
case ConInfo::CONNECT:
// do stuff on new connection
std::cout << "New client in slot " << client_id << '\n';
break;
case ConInfo::DISCONNECT:
// do stuff on disconnect
std::cout << "Client disconnected from slot " << client_id << '\n';
break;
case ConInfo::NORMAL:
// a client has given data to us
std::cout << "From client " << client_id << " : " << p.data() << '\n';auto response = std::string("thanks, ") + std::to_string(client_id);
p = Packet(response);
server.sendPacket(p, client_id); // thank sender for their data
break;
}
}
```
You can also add the argument `false` to `recvPacket` to forbid new connections.