Skip to content

Commit

Permalink
New Readme.md (examples updated)
Browse files Browse the repository at this point in the history
  • Loading branch information
eminfedar committed Oct 24, 2019
1 parent 3e6333e commit 6601503
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 101 deletions.
180 changes: 79 additions & 101 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,57 +1,59 @@
# Asynchronous Sockets for C++
Simple thread-based, asynchronous Socket classes in C++ for TCP & UDP.
```cpp
// Initialize socket.
Socket tcpSocket;
// Initialize the UDP socket.
UDPSocket udpSocket;

// Send String:
udpSocket.SendTo("Test", IP, PORT);
```
Super Easy!

## Examples:

#### TCP Client:
[examples/tcp-client.cpp](https://github.com/eminfedar/async-sockets-cpp/blob/master/examples/tcp-client.cpp):
# Examples:
You can just open the `examples/` folder in terminal and run `make` to get working programs of these examples:

## Clients:
---

### [TCP Client](https://github.com/eminfedar/async-sockets-cpp/blob/master/examples/tcp-client.cpp):
```cpp
#include "../easysocket/socket.h"
#include <tcpsocket.h>
#include <iostream>

using namespace std;

int main()
{
// Initialize socket.
Socket tcpSocket;

// Connect to the host.
tcpSocket.Connect("127.0.0.1", 8888, [&] {
cout << "Connected to the server successfully." << endl;

// Send String:
tcpSocket.Send("Hello Server!");
});
TCPSocket tcpSocket;

// Start receiving from the host.
tcpSocket.onMessageReceived = [&](string message) {
tcpSocket.onMessageReceived = [](string message) {
cout << "Message from the Server: " << message << endl;
};

// On socket closed:
tcpSocket.onSocketClosed = [&]() {
cout << "Connection lost with the server!" << endl;
tcpSocket.onSocketClosed = []{
cout << "Connection closed." << endl;
};

// Check if there is any error:
tcpSocket.onError = [&](string error) {
cerr << error << endl;
};
// Connect to the host.
tcpSocket.Connect("127.0.0.1", 8888, [&] {
cout << "Connected to the server successfully." << endl;

// Send String:
tcpSocket.Send("Hello Server!");
});

// You should do an input loop so the program will not end immediately:
// Because socket listenings are non-blocking.
string input;
cin >> input;
getline(cin, input);
while (input != "exit")
{
tcpSocket.Send(input);
cin >> input;
getline(cin, input);
}

tcpSocket.Close();
Expand All @@ -61,66 +63,55 @@ int main()

```

### UDP Client:
[examples/udp-client.cpp](https://github.com/eminfedar/async-sockets-cpp/blob/master/examples/udp-client.cpp):
### [UDP Client](https://github.com/eminfedar/async-sockets-cpp/blob/master/examples/udp-client.cpp):
```cpp
#include "../easysocket/udpsocket.h"
#include <udpsocket.h>
#include <iostream>

using namespace std;

int main()
{
try
{
// Our constants:
const string IP = "127.0.0.1";
const uint16_t PORT = 8888;

// Initialize socket.
UDPSocket udpSocket;
// Our constants:
const string IP = "127.0.0.1";
const uint16_t PORT = 8888;

// Send String:
udpSocket.SendTo("Test", IP, PORT);
// Initialize socket.
UDPSocket udpSocket;

// Send Byte Array (char*):
const char byteArray[] = {65, 66, 67, 68, 69, 120, 52, 55};
udpSocket.SendTo(byteArray, 8, IP, PORT);
// Send String:
udpSocket.SendTo("Test", IP, PORT);

udpSocket.onMessageReceived = [&](string message, string ipv4, uint16_t port) {
cout << ipv4 << ":" << port << " => " << message << endl;
};
// Send Byte Array (char*):
const char byteArray[] = {65, 66, 67, 68, 69, 120, 52, 55};
udpSocket.SendTo(byteArray, 8, IP, PORT);

udpSocket.onError = [&](string error) {
cout << error << endl;
};
udpSocket.onMessageReceived = [&](string message, string ipv4, uint16_t port) {
cout << ipv4 << ":" << port << " => " << message << endl;
};

// You should do an input loop so the program will not terminated immediately:
string input;
cin >> input;
while (input != "exit")
{
udpSocket.SendTo(input, IP, PORT);
cin >> input;
}

// Close the socket.
udpSocket.Close();
}
catch (const char *exception)
// You should do an input loop so the program will not terminated immediately:
string input;
getline(cin, input);
while (input != "exit")
{
std::cerr << exception << std::endl;
udpSocket.SendTo(input, IP, PORT);
getline(cin, input);
}

// Close the socket.
udpSocket.Close();

return 0;
}

```
### TCP Server:
[examples/tcp-server.cpp](https://github.com/eminfedar/async-sockets-cpp/blob/master/examples/tcp-server.cpp):

## Servers:
---
### [TCP Server](https://github.com/eminfedar/async-sockets-cpp/blob/master/examples/tcp-server.cpp):
```cpp
#include "../easysocket/tcpserver.h"
#include "../easysocket/socket.h"
#include <tcpserver.h>
#include <iostream>

using namespace std;
Expand All @@ -131,27 +122,19 @@ int main()
TCPServer tcpServer;

// When a new client connected:
tcpServer.onNewConnection = [&](Socket *newClient) {
tcpServer.onNewConnection = [&](TCPSocket *newClient) {
cout << "New client: [";
cout << newClient->remoteAddress() << ":" << newClient->remotePort() << "]" << endl;

newClient->onMessageReceived = [newClient](string message) {
cout << newClient->remoteAddress() << ":" << newClient->remotePort() << " => " << message << endl;
};

newClient->onError = [newClient](string error) {
cout << "ERR on socket:" << newClient->remoteAddress() << " => " << error << endl;
};

newClient->onSocketClosed = [newClient]() {
cout << "Socket closed:" << newClient->remoteAddress() << endl;
cout << "Socket closed:" << newClient->remoteAddress() << ":" << newClient->remotePort() << endl;
};
};

tcpServer.onError = [&](string error) {
cerr << error << endl;
};

// Bind the server to a port.
tcpServer.Bind(8888);

Expand All @@ -160,10 +143,10 @@ int main()

// You should do an input loop so the program will not terminated immediately:
string input;
cin >> input;
getline(cin, input);
while (input != "exit")
{
cin >> input;
getline(cin, input);
}

// Close the server before exiting the program.
Expand All @@ -174,45 +157,40 @@ int main()

```

### UDP Server:
[examples/udp-server.cpp](https://github.com/eminfedar/async-sockets-cpp/blob/master/examples/udp-server.cpp):
### [UDP Server](https://github.com/eminfedar/async-sockets-cpp/blob/master/examples/udp-server.cpp):
```cpp
#include "../easysocket/udpserver.h"
#include <udpserver.h>
#include <iostream>

using namespace std;

int main()
{
// Initialize server socket..
UDPServer udpServer;

udpServer.onMessageReceived = [&](string message, string ipv4, uint16_t port) {
cout << ipv4 << ":" << port << " => " << message << endl;
// Initialize server socket..
UDPServer udpServer;

udpServer.SendTo("I got your UDP message!", ipv4, port);
};
// onMessageReceived will run when a message received with information of ip & port of sender:
udpServer.onMessageReceived = [&](string message, string ipv4, uint16_t port) {
cout << ipv4 << ":" << port << " => " << message << endl;

udpServer.onError = [&](string error) {
cerr << error << endl;
};
// Just send without control:
udpServer.SendTo("I got your message!", ipv4, port);
};

// Bind the server to a port.
udpServer.Bind(8888);
// Bind the server to a port.
udpServer.Bind(8888);

// You should do an input loop so the program will not terminated immediately:
string input;
cin >> input;
while (input != "exit")
{
cin >> input;
}
// You should do an input loop so the program will not terminated immediately:
string input;
getline(cin, input);
while (input != "exit")
{
getline(cin, input);
}

udpServer.Close();
udpServer.Close();

return 0;
}

```

**NOTE: Any usage or improvement ideas, pull requests are welcome!** *(feel free to open issues if you want a feature or report a bug :))*
Binary file added img/emptyRamUsages.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/programs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6601503

Please sign in to comment.