servertest.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <socketio.hpp>
  2. #include <iostream>
  3. #include <exception>
  4. #include <random>
  5. template<typename T>
  6. std::ostream& operator<<(std::ostream& out, std::vector<T> o){
  7. out << "(";
  8. for(unsigned int i = 0;i < o.size();i++){
  9. if(o[i] == '\r')continue;
  10. out << o[i];
  11. if(i < (o.size() - 1))
  12. out << ", ";
  13. }
  14. return out << ")";
  15. }
  16. template<>
  17. std::ostream& operator<< <char>(std::ostream& out, std::vector<char> o){
  18. for(unsigned int i = 0;i < o.size();i++){
  19. out << o[i];
  20. }
  21. return out;
  22. }
  23. int main(){
  24. std::uniform_int_distribution<unsigned char> dis(0,20);
  25. std::mt19937_64 gen;
  26. server_socket ssock(80);
  27. while(true){
  28. cppsocket sock = ssock.accept_connection();
  29. try{
  30. while(true){
  31. std::vector<char> vec = sock.receive();
  32. std::cout << "Received: " << vec << std::endl;
  33. std::string resp(100, 'a');
  34. for(unsigned int i = 0;i < resp.size();i++){
  35. resp[i] = dis(gen) + 'a';
  36. }
  37. sock.write("Hallo " + resp);
  38. }
  39. sock.close();
  40. std::cout << "Closed\n";
  41. }
  42. catch(const std::exception& e){
  43. std::cout << e.what() << std::endl;
  44. }
  45. catch(...){
  46. std::cout << "Unidentified exception" << std::endl;
  47. }
  48. }
  49. }