16,216
社区成员
发帖
与我相关
我的任务
分享
//trycli.h
#ifndef TRYCLI_H_
#define TRYCLI_H_
#include <QtGui/QWidget>
#include <QtNetwork/QTcpSocket>
#include <QtGui/QPushButton>
#include <QtCore/QString>
#include <QtCore/QVector>
#include <QtCore/QTimer>
#include "ui_client.h"
class client : public QWidget
{
Q_OBJECT
private:
QPushButton *start;
QTcpSocket *tcpclient;
quint16 blockSize;
QString sendString;
QString readString;
public:
client();
~client();
void Question(const QString &str);
public slots:
void displayError(QAbstractSocket::SocketError socketError);
void newConnect();
void clickedStartButton();
void readMessage();
void sendMessage();
};
#endif
//trycli.cpp
#include "trycli.h"
#include <QtGui/QMessageBox>
#include <QtGui/QHBoxLayout>
#include <QtEvents>
client::client()
{
setWindowTitle("client");
resize(100, 100);
start = new QPushButton("start");
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(start);
setLayout(layout);
tcpclient = new QTcpSocket(this);
connect(start, SIGNAL(clicked()), this, SLOT(clickedStartButton()));
connect(tcpclient, SIGNAL(connected()), this, SLOT(sendMessage()));
connect(tcpclient, SIGNAL(readyRead()), this, SLOT(readMessage()));
connect(tcpclient, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));
}
client::~client()
{
}
void client::newConnect()
{
blockSize = 0;
tcpclient->abort();
tcpclient->connectToHost("127.0.0.1", 8888);
tcpclient->waitForReadyRead();
}
void client::sendMessage()
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << sendString;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
tcpclient->write(block);
}
void client::readMessage()
{
QDataStream in(tcpclient);
in.setVersion(QDataStream::Qt_4_0);
if (blockSize == 0)
{
if (tcpclient->bytesAvailable() < (int)sizeof(quint16))
return;
in >> blockSize;
}
if (tcpclient->bytesAvailable() < blockSize)
return;
in >> readString;
if(readString[0] == 'A')
Question(readString);
else
QMessageBox::about(NULL, "AA", readString);
tcpclient->disconnectFromHost();
}
void client::displayError(QAbstractSocket::SocketError socketError)
{
switch (socketError) {
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
QMessageBox::information(this, tr("Fortune Client"),
tr("The host was not found. Please check the "
"host name and port settings."));
break;
case QAbstractSocket::ConnectionRefusedError:
QMessageBox::information(this, tr("Fortune Client"),
tr("The connection was refused by the peer. "
"Make sure the fortune server is running, "
"and check that the host name and port "
"settings are correct."));
break;
default:
QMessageBox::information(this, tr("Fortune Client"),
tr("The following error occurred: %1.")
.arg(tcpclient->errorString()));
}
}
void client::clickedStartButton()
{
sendString = "1";
newConnect();
}
void client::Question(const QString &str)
{
QMessageBox::StandardButton rb = QMessageBox::question(NULL,
"question", str,
QMessageBox::Yes | QMessageBox::No | QMessageBox::Close,
QMessageBox::Yes);
if(QMessageBox::Yes == rb)
{
sendString = "2";
newConnect();
}
else if(QMessageBox::No == rb)
{
sendString = "3";
newConnect();
}
}
//main.cpp
#include "trycli.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
client w;
w.show();
return app.exec();
}
//tryser.h
#ifndef TRYSER_H_
#define TRYSER_H_
#include <QtGui/QWidget>
#include <QtGui/QLabel>
#include <QtCore/QString>
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>
#include <QtCore/QTimer>
#include <QtCore/QVector>
class server : public QWidget
{
Q_OBJECT
private:
QLabel *label;
QString nowWord;
QTcpServer *tcpserver;
QTcpSocket *tcpsocket;
quint16 blockSize;
QString sendString;
public:
server();
~server();
void sendMessage();
public slots:
void readMessage();
void acceptConnection();
};
#endif
//tryser.cpp
#include "tryser.h"
#include <QtGui/QMessageBox>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLabel>
server::server()
{
label = new QLabel;
setWindowTitle("server");
label->setText("emtpy");
resize(200, 100);
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(label);
setLayout(mainLayout);
tcpserver = new QTcpServer;
tcpsocket = new QTcpSocket;
if( !tcpserver->listen(QHostAddress::Any, 8888) )
{
QMessageBox::warning(NULL, "error", "can not connect");
close();
return;
}
blockSize = 0;
connect(tcpserver, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
}
server::~server()
{
}
void server::acceptConnection()
{
tcpsocket = tcpserver->nextPendingConnection();
connect(tcpsocket, SIGNAL(readyRead()), this, SLOT(readMessage()));
connect(tcpsocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));
}
void server::readMessage()
{
QDataStream in(tcpsocket);
in.setVersion(QDataStream::Qt_4_0);
if (blockSize == 0)
{
if (tcpsocket->bytesAvailable() < (int)sizeof(quint16))
return;
in >> blockSize;
}
if (tcpsocket->bytesAvailable() < blockSize)
return;
QString word;
in >> word;
if("1" == word)
{
sendString = "Are you a boy?";
sendMessage();
}
else if("2" == word)
{
label->setText("He is a boy");
sendString = "Oh, you are a boy!";
sendMessage();
}
else if("3" == word)
{
label->setText("She is a girl");
sendString = "Oh, you are a girl!";
sendMessage();
}
blockSize = 0;
}
void server::sendMessage()
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << sendString;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
tcpsocket->write(block);
}
//main.cpp
#include "tryser.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
server w;
w.show();
return a.exec();
}