Is there a way to check for errors in a non-boost asio program using tcp? And is there a way to add an error to the connection randomly? I made a simple Echo Server in C++ for which i now have to generate random errors, but the problem is that i have no clue how.
Or if that helps more, i need to check for two dimensional parity (DATA,ACK,NAK).
Hope you can help me.
Client:
#include <iostream>
#include <asio\include\asio.hpp>
#include <spdlog\spdlog.h>
using namespace std;
using namespace asio;
using namespace asio::ip;
auto console = spdlog::stderr_logger_st("console");
void usage(){
console->error("invalid argument exception");
cout << "usage: tcp_client [port]" << endl;
cout << "usage: tcp_client [host] [port]" << endl;
}
void connectServer(string host, string port){
tcp::iostream strm{ host, port };
if (strm){
cout << "Bitte Text eingeben: ";
string input;
getline(cin, input);
strm << input << endl;
console->info("Client sent to Server: " + input);
string data;
getline(strm, data);
console->info("Response from Server: " + data);
strm.close();
}
else{
cout << "no connection to server on " + host + " using port: " + port << endl;
}
}
int main(int argc, char* argv[]){
if (argc == 1){
usage();
}
else if (argc == 2){
string port = argv[1];
connectServer("localhost",port);
}
else if (argc == 3){
string host = argv[1];
string port = argv[2];
connectServer(host, port);
}
else{
usage();
}
}
Server:
#include <iostream>
#include <asio\include\asio.hpp>
#include <spdlog\spdlog.h>
using namespace std;
using namespace asio::ip;
auto console = spdlog::stderr_logger_st("console");
void usage(){
console->error("invalid argument exception");
cout << "usage: tcp_server [port]" << endl;
}
void responseClient(unsigned short port){
asio::io_context ctx;
tcp::endpoint ep{ tcp::v4(), port };
tcp::acceptor acceptor{ ctx, ep};
acceptor.listen();
tcp::iostream strm;
acceptor.accept(*strm.rdbuf());
string data;
getline(strm, data);
console->alert("Request from Client: " + data);
strm << data;
console->alert("Response to Client: " + data);
strm.close();
console->alert("Closing stream");
}
int main(int argc, char* argv[]) {
if (argc == 1){
usage();
}
else if (argc == 2){
unsigned short port = stoi(argv[1]);
responseClient(port);
}
}