hi
im unable to communication data in my application server and client, i am use windows 7,when i use windows XP is work well, can anyone help me please
my code like this
in application Server :
main:
#include <QtCore/QCoreApplication>
#include "server.h"
int main(int argc, char *argv[])
{
MyServer server;
return a.exec();
}
#include <QtCore/QCoreApplication>
#include "server.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyServer server;
server.listen(QHostAddress::Any, 4711);
return a.exec();
}
To copy to clipboard, switch view to plain text mode
server.cpp
#include <QtCore>
#include <QtNetwork>
#include "server.h"
#include "MyServerHandlerMethod.h"
{
connect(this,SIGNAL(newConnection()),this,SLOT(MyServerHandler()));
}
void MyServer::MyServerHandler()
{
if(!socket) return;
new MyServerHandlerMethod(socket);
}
#include <QtCore>
#include <QtNetwork>
#include "server.h"
#include "MyServerHandlerMethod.h"
MyServer::MyServer(QObject *parent):QTcpServer (parent)
{
connect(this,SIGNAL(newConnection()),this,SLOT(MyServerHandler()));
}
void MyServer::MyServerHandler()
{
QTcpSocket *socket = nextPendingConnection();
if(!socket) return;
new MyServerHandlerMethod(socket);
}
To copy to clipboard, switch view to plain text mode
MyServerHandlerMethod.h
#ifndef MYSERVERHANDLERMETHOD_H
#define MYSERVERHANDLERMETHOD_H
#include <QtCore/QString>
#include <QtNetwork>
#include <iostream>
using namespace std;
class MyServerHandlerMethod
: public QObject{
Q_OBJECT
public:
{
connect(socket,SIGNAL(readyRead()),this,SLOT(HandlerConnection()));
this->socket = socket;
}
}
signals:
private:
void ConnectClose()
{
socket->close();
delete socket;
deleteLater();
}
private slots:
void HandlerConnection()
{
this->input = data; //QString input
cout<< qPrintable(this->input)<<endl;
InputHandler();
ConnectClose();
}
};
#endif // MYSERVERHANDLERMETHOD_H
#ifndef MYSERVERHANDLERMETHOD_H
#define MYSERVERHANDLERMETHOD_H
#include <QtCore/QString>
#include <QtNetwork>
#include <iostream>
using namespace std;
class MyServerHandlerMethod : public QObject
{
Q_OBJECT
public:
MyServerHandlerMethod(QTcpSocket *socket, QObject *parent = 0) : QObject(parent)
{
connect(socket,SIGNAL(readyRead()),this,SLOT(HandlerConnection()));
this->socket = socket;
}
}
signals:
private:
QTcpSocket *socket;
QString input;
void ConnectClose()
{
socket->close();
delete socket;
deleteLater();
}
private slots:
void HandlerConnection()
{
QByteArray data = socket->readAll();
this->input = data; //QString input
cout<< qPrintable(this->input)<<endl;
InputHandler();
ConnectClose();
}
};
#endif // MYSERVERHANDLERMETHOD_H
To copy to clipboard, switch view to plain text mode
in client:
main
#include <QtCore/QCoreApplication>
#include "client.h"
int main(int argc, char *argv[])
{
client tryClient;
tryClient.send();
return a.exec();
}
#include <QtCore/QCoreApplication>
#include "client.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
client tryClient;
tryClient.send();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
client.h
#ifndef CLIENT_H
#define CLIENT_H
#include <QtNetwork/QTcpSocket>
{
Q_OBJECT
public:
void send()
{
const int Timeout = 5 * 1000;
clientSocket->connectToHost("localhost",4711);
if (!clientSocket->waitForConnected(Timeout)) {
return;
}
clientSocket->write("ACK");
}
private:
signals:
private slots:
};
#endif // CLIENT_H
#ifndef CLIENT_H
#define CLIENT_H
#include <QtNetwork/QTcpSocket>
class client : public QObject
{
Q_OBJECT
public:
void send()
{
const int Timeout = 5 * 1000;
clientSocket = new QTcpSocket(this);
clientSocket->connectToHost("localhost",4711);
if (!clientSocket->waitForConnected(Timeout)) {
return;
}
clientSocket->write("ACK");
}
private:
QTcpSocket *clientSocket;
signals:
private slots:
};
#endif // CLIENT_H
To copy to clipboard, switch view to plain text mode
please help me
Bookmarks