C++ (Qt)#include <QtGui/QApplication>#include "widget.h"#include <QTextCodec>#include <QMessageBox>int main(int argc, char *argv[]){ QTextCodec *codec = QTextCodec::codecForName("cp1251"); QTextCodec::setCodecForLocale(codec); QTextCodec::setCodecForTr(codec); QTextCodec::setCodecForCStrings(codec); QApplication a(argc, argv); QWidget w; w.setGeometry(0, 0, 0, 0); w.show(); TripServer server; if( !server.listen( QHostAddress::Any, 6178 ) ) { QMessageBox::critical(NULL, "Error", "Ошибка открытия порта 6178"); return 1; } TripServer server2; if( !server2.listen( QHostAddress::Any, 6177 ) ) { QMessageBox::critical(NULL, "Error", "Ошибка открытия порта 6178"); return 1; }return a.exec();}
C++ (Qt)#ifndef WIDGET_H#define WIDGET_H #include <QtGui/QWidget>#include <QTcpServer>#include <QTcpSocket> class ClientSocket : public QTcpSocket{ Q_OBJECTpublic: ClientSocket(QObject *parent = 0); int id; QString strA;private slots: void readClient();};class TripServer : public QTcpServer{ Q_OBJECTpublic: TripServer( QObject *parent = 0 ) : QTcpServer( parent ){}private: void incomingConnection(int socketId);};#endif // WIDGET_H
C++ (Qt)#include "widget.h" void TripServer::incomingConnection(int socketId){ ClientSocket *socket = new ClientSocket(this); socket->setSocketDescriptor(socketId); } ClientSocket::ClientSocket( QObject *parent ) :QTcpSocket(parent){ QObject::connect( this, SIGNAL( readyRead() ), this, SLOT( readClient() ) ); QObject::connect( this, SIGNAL( disconnected() ), this, SLOT( deleteLater() ) );}void ClientSocket::readClient(){ QDataStream in(this); QDataStream out(this); in.setVersion(QDataStream::Qt_4_5); in >> strA; id = 1; if (strA == "Hi") out << id;}