#include <QCoreApplication>#include <QTcpSocket>#include <QTcpServer>#include <QDate>#include <QTime>#include <iostream>using namespace std;class TripServer : public QTcpServer{ Q_OBJECT public: TripServer(QObject *parent = 0); private: void incomingConnection(int socketId);};TripServer::TripServer(QObject *parent) :QTcpServer(parent){}class ClientSocket : public QTcpSocket{ Q_OBJECT public: ClientSocket(QObject *parent = 0); private slots: void readClient(); public: void generateRandomTrip(const QString &from,const QString &to, const QDate &date, const QTime &time); quint16 nextBlockSize;};ClientSocket::ClientSocket(QObject *parent) :QTcpSocket(parent){ connect(this, SIGNAL(readyRead()), this, SLOT(readClient())); connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater())); nextBlockSize = 0;}void TripServer::incomingConnection(int socketID){ ClientSocket *socket = new ClientSocket(this); socket->setSocketDescriptor(socketID);}void ClientSocket::readClient(){ QDataStream in(this); in.setVersion(QDataStream::Qt_4_5); if (nextBlockSize == 0) { if(bytesAvailable() < sizeof(quint16)) return; in >> nextBlockSize; } if(bytesAvailable() < nextBlockSize) return; quint8 requestType; QString from; QString to; QDate date; QTime time; quint8 flag; in >> requestType; if (requestType == 8) { in >> from >> to >> date >> time >> flag; srand(from.length() * 3600 + to.length() * 60 + time.hour()); int numTrips = rand() % 8; for(int i = 0; i < numTrips; ++i) generateRandomTrip(from, to, date, time); QDataStream out(this); out << "Спасибо"; } close();}void ClientSocket::generateRandomTrip(const QString &from, const QString &to, const QDate &date, const QTime &time){ QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_5); quint16 duration = rand() % 200; out << quint16(0) << date << time << duration << quint8(1) << QString ("InterCity"); out.device()->seek(0); out << quint16(block.size() - sizeof(quint16)); write(block);}int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); TripServer server; if(!server.listen(QHostAddress::Any, 6178)){ cerr << "Failed to bind to port" << endl; return 1; } else a.quit(); return a.exec();}#include "main.moc"
C++ (Qt)void ClientSocket::readClient(){ QDataStream in(this); in.setVersion(QDataStream::Qt_4_5); // А этот код что делает? if (nextBlockSize == 0) { if(bytesAvailable() < sizeof(quint16)) return; in >> nextBlockSize; } // А этот.... ? if(bytesAvailable() < nextBlockSize) return;
C++ (Qt)#include <QCoreApplication>#include <QTcpSocket>#include <QTcpServer>#include <iostream>using namespace std; class TripServer : public QTcpServer{ Q_OBJECT public: TripServer(QObject *parent = 0); private: void incomingConnection(int socketId);};TripServer::TripServer(QObject *parent) :QTcpServer(parent){}class ClientSocket : public QTcpSocket{ Q_OBJECT public: ClientSocket(QObject *parent = 0); private slots: void readClient(); }; ClientSocket::ClientSocket(QObject *parent) :QTcpSocket(parent){ connect(this, SIGNAL(readyRead()), this, SLOT(readClient())); connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater())); //nextBlockSize = 0;}void TripServer::incomingConnection(int socketID){ ClientSocket *socket = new ClientSocket(this); socket->setSocketDescriptor(socketID);} void ClientSocket::readClient(){ QByteArray block; int i; QDataStream in(this); in.setVersion(QDataStream::Qt_4_5); QDataStream out(&block, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_5); in >> i; if (i == 1) { block = "n"; out << block; } else { block = "y"; out << block; close(); }} int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); TripServer server; if(!server.listen(QHostAddress::Any, 6178)){ cerr << "Failed to bind to port" << endl; return 1; } else a.quit(); return a.exec();} #include "main.moc"
C++ (Qt)#include <QCoreApplication> #include <QTcpSocket> #include <QTcpServer> #include <QDebug> class TripServer : public QTcpServer{ Q_OBJECT public: TripServer( QObject *parent = 0 ) : QTcpServer( parent ) {} private: void incomingConnection( int socketId );}; class ClientSocket : public QTcpSocket{ Q_OBJECT public: ClientSocket(QObject *parent = 0); private slots: void readClient();}; void TripServer::incomingConnection( int socketID ){ ClientSocket *socket = new ClientSocket( this ); socket->setSocketDescriptor( socketID ); } ClientSocket::ClientSocket( QObject *parent ) : QTcpSocket( parent ) { connect( this, SIGNAL( readyRead() ), this, SLOT( readClient() ) ); connect( this, SIGNAL( disconnected() ), this, SLOT( deleteLater() ) );} void ClientSocket::readClient(){ quint16 id; QDataStream in( this ); in.setVersion(QDataStream::Qt_4_5); if( (int)bytesAvailable() < sizeof( quint16 ) ) return; in >> id; qDebug() << id; if( id == 8 ) qDebug() << "Yaa hoo!!!"} int main( int argc, char *argv[] ){ QCoreApplication app( argc, argv ); TripServer server; if( !server.listen( QHostAddress::Any, 6178 ) ) { qDebug() << "Failed to bind to port"; return 1; } return app.exec();} #include "main.moc"
C++ (Qt)#include <QCoreApplication>#include <QTcpSocket>#include <QDebug> int main( int argc, char *argv[] ){ QCoreApplication app( argc, argv ); QTcpSocket socket; socket.connectToHost( "localhost", 6178 ); if( !socket.waitForConnected( 1000 ) ) { qDebug( "Error host connected!" ); return 1; } qDebug( "Connected!" ); quint16 key = 8; QByteArray buf; QDataStream out( &buf, QIODevice::WriteOnly ); out << key; qDebug() << "Send key"; socket.write( buf ); socket.flush(); return 0;}