class ServerInfo : public QVBox
{
Q_OBJECT
public:
ServerInfo()
{
server = new QServerSocket( 4242, 1, parent );
if ( !server->ok() ) {
qWarning("Failed to bind to port 4242");
exit(1);
}
socket = new QSocket(this);
void server->newConnection( int socket )
{
socket->setSocket( socket );
connect( socket, SIGNAL(readyRead()), SLOT(readClient()) );
connect( socket, SIGNAL(connectionClosed()), SLOT(connectionClosed()) );
emit newConnect();
}
QString itext = QString(
"This is a small server example.\n"
"Connect with the client now."
);
QLabel *lb = new QLabel( itext, this );
lb->setAlignment( AlignHCenter );
infoText = new QTextView( this );
QPushButton *quit = new QPushButton( "Quit" , this );
connect( server, SIGNAL(newConnect()), SLOT(newConnection()) );
connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) );
}
~ServerInfo()
{
}
private slots:
void readClient()
{
line=0;
while ( socket->canReadLine() ) {
/* QTextStream os( this );
os << line << ": " <<socket->readLine();
line++; */
infoText->append(socket->readLine());
}
}
void connectionClosed()
{
delete this;
}
void newConnection()
{
infoText->append( "New connection\n" );
}
signals:
void newConnect();
private:
int line;
QTextView *infoText;
QServerSocket *server;
QSocket *socket;
};