5

I'm trying to develop a TCP Server with POCO C++ libraries. I found some examples here. At first I tried example from Alex but shutdown event didn't work. EchoServer have the same problem. So, then I tried Cesar Ortiz example and got a unusual problem. After some time server throws an error:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
["src/ErrorHandler.cpp", line 60]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

And connections got connection timeout error, new connections as well. Example with eventhandler semeed more correct, but I don't know how can I fix shutdown event.

Community
  • 1
  • 1
Valera A.
  • 81
  • 1
  • 1
  • 4

1 Answers1

14

If all you want is multi-threaded TCP server, then "out of the box" Poco::Net::TCPServer will do - it is multi-threaded internally. Start with defining the connection, this one will just echo back whatever you send to it:

class EchoConnection: public TCPServerConnection {
public:
  EchoConnection(const StreamSocket& s): TCPServerConnection(s) { }

  void run() {
    StreamSocket& ss = socket();
    try {
      char buffer[256];
      int n = ss.receiveBytes(buffer, sizeof(buffer));
      while (n > 0) {
        ss.sendBytes(buffer, n);
        n = ss.receiveBytes(buffer, sizeof(buffer));
      }
    }
    catch (Poco::Exception& exc)
    { std::cerr << "EchoConnection: " << exc.displayText() << std::endl; }
  }
};

Then, run the server and send it some data:

TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>());
srv.start();

SocketAddress sa("localhost", srv.socket().address().port());
StreamSocket ss(sa);
std::string data("hello, world");
ss.sendBytes(data.data(), (int) data.size());
char buffer[256] = {0};
int n = ss.receiveBytes(buffer, sizeof(buffer));
std::cout << std::string(buffer, n) << std::endl;

srv.stop();
Alex
  • 5,159
  • 4
  • 25
  • 33
  • Thanks for reply Alex. As I said, now I use the second example like yours, but I can't find the cause of a bug with 'src/errorhandler.cpp' message. After this server accepts connections, but doesn't send or receive bytes for new connections and throw 'connection timeout' for existing connections. – Valera A. May 15 '13 at 05:15
  • You must be throwing an exception somewhere in your connection handler. Since it runs in a thread, it ends up in the default error handler. Exception is probably not a descendant from std::exception, so ErrorHandler does not know anything about it. But you are not providing enough info for a definite answer on what the problem is. Best to run your code in debugger and step through it to find where is the source of your problem. – Alex May 15 '13 at 14:21
  • As I said, I use the example of Cesar Ortiz, I just changed 'run' function - [here](http://hastebin.com/lokadobeve.coffee) As I found out, when a connection couldn't get data because it in queue, but I can't understand why? When current connections reach 16, all new connections are added to queue and refused after. In params I set maxqueued(3), maxthreads(20) and threadidletime(100) – Valera A. May 16 '13 at 15:40