Qt client__server代码实现

kain posted @ 2011年4月12日 09:06 in c&c++ with tags Qt , 2974 阅读

在用QT做网络实验,client,server程序,按照官方教程刚开始出现了问题,只能本地端口通信,bridge连接的virutualbox中的机子不能和外面通信,搞了N久,终于发现了问题所在,代码如下

server

 

server.h

 

 

 

#ifndef server
#define server
#include <QDialog>
#include <QtNetwork>
class QLabel;
class QLineEdit;
class QPushButton;
class Server : public QDialog
{
Q_OBJECT
public:
Server(QWidget *parent = 0);
QLabel *ListenLabel;
QLineEdit *ListenLineEdit;
QPushButton *ListenButton;
QTcpServer *tcpServer;
QTcpSocket *tcpSocket;
private slots:
void mylisten();
void read();
void con();
};
 
#endif
 
 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
server.cpp
 
#include <QtGui>
#include <QtNetwork>
 
#include "server.h"
 
Server :: Server(QWidget *parent)
:QDialog(parent)
{
    QString str;
    QHostInfo info = QHostInfo::fromName(QHostInfo::localHostName());
    QHostAddress address = info.addresses().first();
    str = address.toString();
    QLabel *name = new QLabel(QString("my ip is: ") + str);
tcpServer = new QTcpServer;
ListenLabel = new QLabel(tr("Listen Port"));
ListenLineEdit = new QLineEdit;
ListenLineEdit->setValidator(new QIntValidator(1,65535, this));
 
ListenLabel->setBuddy(ListenLineEdit);
ListenButton = new QPushButton(tr("Listen"));
 
connect(ListenButton, SIGNAL(clicked()), 
this, SLOT(mylisten()));
 
// tcpServer->listen(QHostAddress::LocalHost, 8000);
// connect(tcpServer, SIGNAL(newConnection()), this, SLOT(con()));
// QMessageBox::warning(NULL,"fe", "af", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
QVBoxLayout *mm = new QVBoxLayout;
    mm->addWidget(name);
mm->addWidget(ListenLabel);
mm->addWidget(ListenLineEdit);
mm->addWidget(ListenButton);
 
setLayout(mm);
}
void Server::mylisten()
{
QString port;
if(ListenLineEdit->text().isEmpty())
return;
 
port = ListenLineEdit->text();
 
tcpServer->listen(QHostAddress::Any, port.toUShort());//网上教程都是local,无疑两台机子不能通信,窘
 
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(con()));
 
}
void Server::con()
{
tcpSocket = tcpServer->nextPendingConnection();
// QMessageBox::warning(NULL,"fe", "af", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(read()));
}
void Server::read()
{
QByteArray byte;
byte = tcpSocket->readAll();
 
QDialog *mm = new QDialog;
unsigned short aa = tcpSocket->peerPort();
QLabel *port =  new QLabel(QString("sender send Mesg throug his port is %1").arg(aa)); //peer send through the port
QLabel *address = new QLabel(QString("client Adderss is") + tcpSocket->peerAddress().toString());
QLabel *Message = new QLabel(QString("Message is: ") + QString(byte));
 
QGridLayout *bb = new QGridLayout;
bb->addWidget(port,0,0);
bb->addWidget(address,1,0);
bb->addWidget(Message,2,0);
 
mm->setLayout(bb);
mm->show();
}
 
 
 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
main.cpp
 
 
#include <QApplication>
 
#include "server.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
Server mm;
mm.show();
return mm.exec();
 
}

 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Clinet端代码

 

 

client.h

 

 

 

#ifndef BLOCKINGCLIENT_H
#define BLOCKINGCLIENT_H
 
#include <QDialog>
#include <QtNetwork>
class QDialogButtonBox;
class QLabel;
class QLineEdit;
class QPushButton;
 
class Client : public QDialog
{
Q_OBJECT
public:
Client(QWidget *parent = 0);
private slots:
void sendMessage();
void send();
void con();
void wrong();
private:
QLabel *hostLabel;
QLabel *portLabel;
QLabel *MessageLabel;
QLabel *statusLabel;
QLineEdit *hostLineEdit;
QLineEdit *portLineEdit;
QLineEdit *MessageLineEdit;
QTcpSocket *socket;
QDialogButtonBox *buttonBox;
QPushButton *sendMessageButton;
QPushButton *connectButton;
};
#endif
 
 
 
////////////////////////////////////////////////////////////////////////////////////
client.cpp
 
#include <QtGui>
#include <QtNetwork>
 
#include "client.h"
 
Client::Client(QWidget *parent)
:QDialog(parent)
{
socket = new QTcpSocket;
hostLabel = new QLabel(tr("&Server name:"));
portLabel = new QLabel(tr("S&erver port"));
MessageLabel = new QLabel(tr("Message to send"));
statusLabel = new QLabel(tr("Would show the Error code"));
QString ipAddress;
 
 
hostLineEdit = new QLineEdit;
portLineEdit = new QLineEdit;
MessageLineEdit = new QLineEdit;
 
portLineEdit->setValidator(new QIntValidator(1, 65535,this));
 
hostLabel->setBuddy(hostLineEdit);
portLabel->setBuddy(portLineEdit);
MessageLabel->setBuddy(MessageLineEdit);
 
sendMessageButton = new QPushButton(tr("send"));
sendMessageButton->setEnabled(false);
connectButton = new QPushButton(tr("connect"));
buttonBox = new QDialogButtonBox;
 
buttonBox->addButton(connectButton, QDialogButtonBox::AcceptRole);
buttonBox->addButton(sendMessageButton,QDialogButtonBox:: AcceptRole);
 
 
connect(connectButton, SIGNAL(clicked()), this, SLOT(con()));
connect(sendMessageButton, SIGNAL(clicked()), 
this, SLOT(sendMessage()));
 
 
 
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(hostLabel, 0, 0);
mainLayout->addWidget(hostLineEdit, 0, 1);
mainLayout->addWidget(portLabel, 1, 0);
mainLayout->addWidget(portLineEdit, 1, 1);
mainLayout->addWidget(MessageLabel, 2, 0);
mainLayout->addWidget(MessageLineEdit,2,1);
mainLayout->addWidget(buttonBox, 3,0,3,1);
mainLayout->addWidget(statusLabel,6,0);
setLayout(mainLayout);
setWindowTitle(tr("send client"));
}
 
void Client :: con()
{
QString host;
QString port;
QString Message;
if(hostLineEdit->text().isEmpty() || portLineEdit->text().isEmpty() || MessageLineEdit->text().isEmpty())
return;
 
host = hostLineEdit->text();
port = portLineEdit->text();
Message = MessageLineEdit->text();
// QMessageBox::warning(NULL, host, port, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
 
socket->connectToHost(host,port.toUShort());
 
// socket->connectToHost(QHostAddress::LocalHost,port.toUShort());
// socket->write(qPrintable(Message));
connect(socket, SIGNAL(connected()), this,SLOT(send()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(wrong()));
}
 
void Client::wrong()
{
statusLabel->setText(QString("Error code %1").arg(socket->error()));
}
void Client::send()
{
sendMessageButton->setEnabled(true);
}
void Client :: sendMessage()
{
 
QString Message;
Message = MessageLineEdit->text();
socket->write(qPrintable(Message));
}
 
 
 
 
 
////////////////////////////////////////////////////////////////////////////////////////////////
main.cpp
 
#include <QApplication>
 
#include "client.h"
 
int main(int argc, char **argv)
{
QApplication app(argc, argv);
Client kain;
kain.show();
return kain.exec();
}
 
 
 
 
还有就是在.pro文件中加上 QT += network不然什么TCPserver之类的找不到

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter