Permalink
Browse files

Commit QTCP Server-Client

Fuck Sended Fuck Recieved
  • Loading branch information...
1 parent 8d56b4e commit 66a58c7990cf87266b46d8efb375103b1203d4f5 @insamo committed Sep 29, 2012
View
@@ -1,15 +1,19 @@
SOURCES += \
main.cpp \
- src/clientserver/client.cpp \
- src/clientserver/server.cpp \
- src/testpoligon/testpoligon.cpp
+ src/testpoligon/testpoligon.cpp \
+ src/clientserver/TCPServer.cpp \
+ src/clientserver/TCPClient.cpp \
+ src/clientserver/clientthread.cpp \
+ src/lib/reporting.cpp
QT += phonon, network
HEADERS += \
- src/clientserver/client.h \
- src/clientserver/server.h \
- src/testpoligon/testpoligon.h
+ src/testpoligon/testpoligon.h \
+ src/clientserver/TCPServer.h \
+ src/clientserver/TCPClient.h \
+ src/clientserver/clientthread.h \
+ src/lib/reporting.h
FORMS += \
src/testpoligon/testpoligon.ui
View
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
-<!-- Written by Qt Creator 2.4.1, 2012-09-24T20:04:19. -->
+<!-- Written by Qt Creator 2.4.1, 2012-09-30T00:17:56. -->
<qtcreator>
<data>
<variable>ProjectExplorer.Project.ActiveTarget</variable>
@@ -52,7 +52,7 @@
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Target.DesktopTarget</value>
- <value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value>
+ <value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
@@ -101,7 +101,7 @@
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4BuildConfiguration</value>
<value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">2</value>
- <value type="QString" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildDirectory">D:/QtSDK/pro/VidMan-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK_________</value>
+ <value type="QString" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildDirectory">D:/_WORKDIR_/Qt/DebubVidman</value>
<value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.QtVersionId">4</value>
<value type="bool" key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild">true</value>
</valuemap>
View
@@ -1,8 +1,15 @@
#include <QApplication>
+#include "src/clientserver/TCPServer.h"
+#include "src/clientserver/TCPClient.h"
+
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
+ TCPServer server;
+
+ //TCPClient client;
+
return a.exec();
}
@@ -1,6 +1,41 @@
-#include "client.h"
+#include "TCPClient.h"
-client::client(QObject *parent) :
+TCPClient::TCPClient(QObject *parent) :
QObject(parent)
{
+ tcpSocket = new QTcpSocket(this);
+
+ QString ip = "192.168.234.129";
+ quint16 port = 50000;
+
+ tcpSocket->connectToHost(ip,port);
+
+ if(tcpSocket->waitForConnected()) {
+ QString text = "fuck";
+ QByteArray oblock;
+ QDataStream out(&oblock, QIODevice::WriteOnly);
+ out.setVersion(QDataStream::Qt_4_8);
+ //out << (quint16)0;
+ out << text;
+ //out.device()->seek(0);
+ //out << (quint16)(oblock.size() - sizeof(quint16));
+
+ tcpSocket->write(oblock);
+ reporting("client write ",1,true);
+
+ if(tcpSocket->waitForBytesWritten()) {
+ QByteArray block;
+ QDataStream in(&block,QIODevice::ReadOnly);
+ in.setVersion(QDataStream::Qt_4_8);
+ tcpSocket->waitForReadyRead();
+ block = tcpSocket->readAll();
+ reporting("client readAll ",1,true);
+ text = "";
+ in >> text;
+ reporting("client " + text,1,true);
+ }
+
+ tcpSocket->disconnectFromHost();
+ tcpSocket->waitForDisconnected();
+ }
}
@@ -2,16 +2,21 @@
#define CLIENT_H
#include <QObject>
+#include <QTcpSocket>
+#include "src/lib/reporting.h"
-class client : public QObject
+class TCPClient : public QObject
{
Q_OBJECT
public:
- explicit client(QObject *parent = 0);
+ explicit TCPClient(QObject *parent = 0);
signals:
public slots:
+
+private:
+ QTcpSocket *tcpSocket;
};
@@ -1,6 +1,16 @@
-#include "server.h"
+#include "TCPServer.h"
-server::server(QObject *parent) :
- QObject(parent)
+TCPServer::TCPServer(QObject *parent) :
+ QTcpServer(parent)
{
+ ip = "127.0.0.1";
+ port = 50000;
+ this->listen(QHostAddress(ip),port);
+}
+
+void TCPServer::incomingConnection(int handle)
+{
+ ClientThread *thread = new ClientThread(handle, this);
+ connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
+ thread->start();
}
@@ -2,14 +2,21 @@
#define SERVER_H
#include <QObject>
+#include <QTcpServer>
+#include "src/clientserver/clientthread.h"
-class server : public QObject
+class TCPServer : public QTcpServer
{
Q_OBJECT
public:
- explicit server(QObject *parent = 0);
+ explicit TCPServer(QObject *parent = 0);
+
+private:
+ QString ip;
+ quint16 port;
-signals:
+protected:
+ void incomingConnection(int handle);
public slots:
@@ -0,0 +1,46 @@
+#include "clientthread.h"
+
+ClientThread::ClientThread(int handleSocket, QObject *parent) :
+ QThread(parent)
+{
+ this->handleSocket = handleSocket;
+}
+
+void ClientThread::run()
+{
+ QTcpSocket tcpSocket;
+
+ if (!tcpSocket.setSocketDescriptor(handleSocket)) {
+
+ return;
+ }
+
+
+ if(tcpSocket.waitForReadyRead())
+ {
+ QByteArray block;
+ QDataStream in(&block,QIODevice::ReadOnly);
+ in.setVersion(QDataStream::Qt_4_8);
+ block = tcpSocket.readAll();
+ reporting("server read ",1,true);
+ QString text;
+ in >> text;
+ reporting("thred " + text,1,true);
+
+ QByteArray oblock;
+ QDataStream out(&oblock, QIODevice::WriteOnly);
+ out.setVersion(QDataStream::Qt_4_8);
+ //out << (quint16)0;
+ out << text;
+ //out.device()->seek(0);
+ // out << (quint16)(oblock.size() - sizeof(quint16));
+
+ tcpSocket.write(oblock);
+ reporting("server write ",1,true);
+ tcpSocket.waitForBytesWritten();
+ tcpSocket.disconnectFromHost();
+ tcpSocket.waitForDisconnected();
+ }
+}
+
+
@@ -0,0 +1,24 @@
+#ifndef CLIENTTHREAD_H
+#define CLIENTTHREAD_H
+
+#include <QThread>
+#include <QTcpSocket>
+#include "src/lib/reporting.h"
+class ClientThread : public QThread
+{
+ Q_OBJECT
+public:
+ ClientThread(int handleSocket, QObject *parent = 0);
+ void run();
+
+private:
+ int handleSocket;
+
+signals:
+
+
+public slots:
+
+};
+
+#endif // CLIENTTHREAD_H
View
@@ -0,0 +1,22 @@
+#ifndef CMDCOLOR_H
+#define CMDCOLOR_H
+
+
+//Цвета раскраски консоли
+#define cmdBOLD "\033[1m"
+#define cmdred "\033[0;31m"
+#define cmdRED "\033[1;31m"
+#define cmdGREEN "\033[0;32m"
+#define cmdgreen "\033[1;32m"
+#define cmdYELLOW "\033[0;33m"
+#define cmdyellow "\033[1;33m"
+#define cmdblue "\033[0;34m"
+#define cmdBLUE "\033[1;34m"
+#define cmdMAGENTA "\033[0;35m"
+#define cmdmagenta "\033[1;35m"
+#define cmdcyan "\033[0;36m"
+#define cmdCYAN "\033[1;36m"
+#define cmdNC "\033[0m" // No Color
+
+
+#endif // CMDCOLOR_H
View
@@ -0,0 +1,52 @@
+#include "reporting.h"
+
+reporting::reporting(QString msg, quint8 type,bool console, QObject *parent):
+ QObject(parent)
+{
+
+ reportBegin = QTime::currentTime().toString()+" >> ";
+ reportMsg = msg;
+ reportType = type;
+ bConsole = console;
+
+ if(!path.exists("logs")) {
+ path.mkdir("logs");
+ }
+ log.setFileName("logs/"+QDate::currentDate().toString("yyyyMMdd")+".log");
+ log.open(QIODevice::Append);
+
+ write();
+}
+
+reporting::~reporting()
+{
+ log.flush();
+ log.close();
+}
+
+void reporting::write()
+{
+ QTextStream in(&log);
+
+ switch (reportType) {
+ case 0:
+ if(bConsole) qDebug()<<cmdGREEN<<"[OK] "+reportBegin+reportMsg<<cmdNC;
+ in<<"[OK] "+reportBegin<<reportMsg<<endl;
+ break;
+ case 1:
+ if(bConsole) qDebug()<<cmdYELLOW<<"[WARNING] "+reportBegin+reportMsg<<cmdNC;
+ in<<"[WARNING] "<<reportBegin<<reportMsg<<endl;
+ break;
+ case 2:
+ if(bConsole) qDebug()<<cmdRED<<"[ERROR] "+reportBegin+reportMsg<<cmdNC;
+ in<<"[ERROR] "<<reportBegin<<reportMsg<<endl;
+ break;
+ case 3:
+ if(bConsole) qDebug()<<cmdCYAN<<"[INFO]"+reportMsg<<cmdNC;
+ in<<"[INFO] "<<reportBegin<<reportMsg<<endl;
+ break;
+ }
+
+ deleteLater();
+
+}
View
@@ -0,0 +1,35 @@
+#ifndef REPORTING_H
+#define REPORTING_H
+
+#include <QObject>
+#include <QDebug>
+#include <QDir>
+#include <QFile>
+#include <QDateTime>
+#include <QTextStream>
+
+#include "src/cmdcolor.h"
+
+class reporting : public QObject
+{
+ Q_OBJECT
+public:
+ reporting(QString msg,quint8 type = 0,bool console = false, QObject *parent = 0);
+
+ ~reporting();
+private:
+ void write();
+
+ QDir path;
+ QFile log;
+ QString reportBegin;
+ QString reportMsg;
+ quint8 reportType;
+ bool bConsole;
+signals:
+
+public slots:
+
+};
+
+#endif // REPORTING_H

0 comments on commit 66a58c7

Please sign in to comment.