I'm new to network programming. I have been trying to write a threaded server application that accepts multiple connections from clients. It works fine with one thread, but I can't get it working with multiple connections.
Hope you can help. Thanks!
- Code: Select all
#include <Poco/Exception.h>
#include <Poco/Net/SocketAddress.h>
#include <Poco/Net/Socket.h>
#include <Poco/Net/StreamSocket.h>
#include <Poco/Net/ServerSocket.h>
#include <Poco/Thread.h>
#include <Poco/Runnable.h>
#include <Poco/ThreadPool.h>
using Poco::Exception;
using Poco::Net::SocketAddress;
using Poco::Net::Socket;
using Poco::Net::StreamSocket;
using Poco::Net::ServerSocket;
using Poco::Thread;
using Poco::Runnable;
using Poco::ThreadPool;
#include <iostream>
#include <sstream>
#include <algorithm>
#define BUFFER_SIZE 32
class Connection : public Runnable
{
public:
//constructor
Connection(StreamSocket *socket) : m_socket(socket) {}
//destructor
~Connection()
{
if(m_socket) delete m_socket;
}
//methods
virtual void run()
{
try
{
std::string host = m_socket->address().host().toString();
int port = m_socket->address().port();
//talk to the server
while(true)
{
std::string msg_in = _read_message();
std::cout << host << ":" << port << ": " << msg_in << std::endl;
//echo message back in uppercase
std::transform(msg_in.begin(),msg_in.end(),msg_in.begin(),::toupper);
std::string msg_out = msg_in;
_send_message(msg_out);
}
}
catch(Exception &e)
{
std::cout << e.displayText() << std::endl;
}
}
private:
//methods
//read the message. does not include termination sequence.
std::string _read_message()
{
//clear the accumulation buffer
m_accum_buffer.clear();
int num_bytes;
char prev_byte;
while(true)
{
num_bytes = m_socket->receiveBytes(m_buffer,BUFFER_SIZE);
//append the bytes to the accumulation buffer
for(int i=0; i<num_bytes; i++)
{
if(i > 0 && (i+1) < num_bytes && m_buffer[i] == '\n' && m_buffer[i+1] == '\r')
{
//got message
return m_accum_buffer;
}
m_accum_buffer += m_buffer[i];
prev_byte = m_buffer[i];
}
}
}
//does not assume termination sequence has been appended
void _send_message(const std::string &message)
{
std::string the_msg = message + "\n\r"; //add termination sequence
m_socket->sendBytes((const void *)the_msg.data(),the_msg.length());
}
//data
StreamSocket *m_socket;
char m_buffer[BUFFER_SIZE];
std::string m_accum_buffer;
protected:
//
};
int main(int argc,char **argv)
{
try
{
SocketAddress sa("localhost",54321);
ServerSocket sock(sa);
sock.listen();
const int MAX = 5;
int used = 0;
Thread threads[MAX];
while(used < MAX)
{
StreamSocket *ss = new StreamSocket(sock.acceptConnection());
Connection conn(ss);
//Thread thread;
//thread.start(conn);
//thread.join(); //blocks. how to do it?
threads[used++].start(conn);
}
//join all the threads
for(int i=0; i<used; i++)
{
threads[i].join();
}
}
catch(Exception &e)
{
std::cerr << e.displayText() << std::endl;
}
return 0;
}





