0

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);
    }
}
Tanner Sansbury
  • 51,153
  • 9
  • 112
  • 169
Patrick
  • 123
  • 2
  • 10
  • What is this *non-boost asio*? – SergeyA Mar 29 '16 at 19:07
  • @SergeyA http://think-async.com/Asio/AsioAndBoostAsio – pingul Mar 29 '16 at 19:10
  • So, noone got an idea? I need quick help on this one please. – Patrick Mar 29 '16 at 20:37
  • To manipulate the lower-level headers, including the checksums, use raw sockets. This [answer](http://stackoverflow.com/a/28257640/1053968) demonstrates using raw sockets for manipulating UDP headers. – Tanner Sansbury Mar 30 '16 at 19:43
  • Thanks for the informing answer, how the question is, does this work with UDP too? – Patrick Mar 31 '16 at 07:43
  • Sorry, I do not understand what you are asking. If you need to manually set or access lower-level fields in the protocol stack with Asio, then a raw socket can be used to accomplish this. Perhaps a higher-level tool, such as [scapy](https://github.com/secdev/scapy/), would help in packet manipulation? – Tanner Sansbury Apr 04 '16 at 15:10

0 Answers0