C++ (Qt)#ifndef WIDGET_H#define WIDGET_H #include <QtGui/QWidget>#include <QTcpServer>#include <QTcpSocket> class QTcpServer; class Server : public QWidget{ Q_OBJECTpublic: Server(QWidget *parent = 0); int i; QTcpSocket *socket1, *socket2; QString strA, buf;public slots: void readPack(); void readClient();protected: void incomingConnection();};#endif // WIDGET_H
C++ (Qt)#include "widget.h"#include <QMessageBox>QTcpServer *server = new QTcpServer; Server::Server(QWidget *parent) :QWidget(parent){ connect(socket1, SIGNAL(readyRead()), this, SLOT(readPack())); connect(socket2, SIGNAL(readyRead()), this, SLOT(readClient())); i = 0; server->setParent(this); if (!server->listen(QHostAddress::Any, 6178)) { QMessageBox::critical(NULL, "Error", "Невозможно открыть порт 6178"); server->close(); return; }} void Server::incomingConnection(){ ++i; if (i == 2) { socket1 = server->nextPendingConnection(); socket2 = server->nextPendingConnection(); }} void Server::readClient() { QDataStream in(socket2); QDataStream out(socket1); in >> strA; if (strA == "Hello") buf = "Yeah"; else buf = "Nothing"; out << buf; } void Server::readPack() { QDataStream in(socket1); QDataStream out(socket2); in >> strA; out << strA; }
C++ (Qt)#include <QTextCodec>#include <QtGui/QApplication>#include "widget.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); QTextCodec *codec = QTextCodec::codecForName("cp1251"); QTextCodec::setCodecForLocale(codec); QTextCodec::setCodecForTr(codec); QTextCodec::setCodecForCStrings(codec); Server tcpServer; tcpServer.setGeometry(0, 0, 0, 0); tcpServer.show();;return a.exec();}
C++ (Qt)Server::Server(QWidget *parent):QWidget(parent){/////////////////////////////////////////////////////////////////////// socket1 = new QTcpSocket(); socket2 = new QTcpSocket();/////////////////////////////////////////////////////////////////////// connect(socket1, SIGNAL(readyRead()), this, SLOT(readPack())); connect(socket2, SIGNAL(readyRead()), this, SLOT(readClient())); i = 0; server->setParent(this); if (!server->listen(QHostAddress::Any, 6178)) { QMessageBox::critical(NULL, "Error", "Невозможно открыть порт 6178"); server->close(); return; }}
C++ (Qt)#ifndef WIDGET_H#define WIDGET_H #include <QtGui/QWidget>#include <QTcpServer>#include <QTcpSocket> class QTcpServer; class Server : public QWidget{ Q_OBJECTpublic: Server(QWidget *parent = 0); int i; QString strA; QTcpSocket *socket1, *socket2;public slots: void readPack(); void readClient();protected: void incomingConnection();};#endif // WIDGET_H
C++ (Qt)#include "widget.h"#include <QMessageBox>QTcpServer *server = new QTcpServer;Server::Server(QWidget *parent) :QWidget(parent){ socket1 = new QTcpSocket(); socket2 = new QTcpSocket(); connect(socket1, SIGNAL(readyRead()), this, SLOT(readPack())); connect(socket2, SIGNAL(readyRead()), this, SLOT(readClient())); connect(socket1, SIGNAL(disconnected()), this, SLOT(deleteLater())); connect(socket2, SIGNAL(disconnected()), this, SLOT(deleteLater())); i = 0; server->setParent(this); if (!server->listen(QHostAddress::Any, 6178)) { QMessageBox::critical(NULL, "Error", "Невозможно открыть порт 6178"); server->close(); return; }} void Server::incomingConnection(){ ++i; if (i == 2) { socket1 = server->nextPendingConnection(); socket2 = server->nextPendingConnection(); }} void Server::readClient() { QDataStream in(socket2); QDataStream out(socket1); in.setVersion(QDataStream::Qt_4_6); in >> strA;; out << strA; } void Server::readPack() { QDataStream recv(socket1); QDataStream send(socket2); recv.setVersion(QDataStream::Qt_4_6); recv >> strA; send << strA; }
C++ (Qt) connect(socket1, SIGNAL(readyRead()), this, SLOT(readPack())); connect(socket2, SIGNAL(readyRead()), this, SLOT(readClient()));
...class Server : public QWidget.......
...QTcpServer *server = new QTcpServer;Server::Server(QWidget *parent) :QWidget(parent)......