1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| Window::Window() : QWidget()
{
//...
connect( m_server, SIGNAL( newConnection() ), this, SLOT( on_server_newConnection() ));
}
void Window::on_server_newConnection()
{
Client *newClient = new Client( m_server->nextPendingConnection() );
m_clients << newClient;
connect( newClient->socket(), SIGNAL( readyRead() ), this, SLOT( on_client_readyRead() ) );
connect( newClient->socket(), SIGNAL( disconnected() ), this, SLOT( on_client_disconnected() ) );
}
void Window::on_client_readyRead()
{
QTcpSocket *socket = qobject_cast< QTcpSocket* >( sender() );
if (socket == 0) // If sender not found
return;
QDataStream in( socket );
QString msg;
in >> msg;
m_log->append( "Message length : " + QString::number( socket->bytesAvailable() ) + "<br />" + msg );
} |