自己写的一个客户端程序,源码的连接http://download.csdn.net/detail/qq_28637193/9615451
界面是这样的。注意在pro文件中需要加入
QT += network
界面是直接在用设计界面设计出来的不是代码写的,所以大家模仿着做出来就行
下面是头文件mainwindow.h的内容
</pre><pre name="code" class="cpp">#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<QTcpSocket>
#include<QTcpServer>
#include<QLabel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QTcpSocket *tcpSocket;
QString message;
// 用来存放数据的大小信息
quint16 blockSize;
//QTcpServer *tcpServer;
private slots:
void newConnect();//新连接
void readMessage();//读取数据
void displayError(QAbstractSocket::SocketError);
void sendMessage();//发送数据
};
#endif // MAINWINDOW_H
下面是main.cpp
#include "mainwindow.h"
#include <QApplication>
#include<QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
下面是mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtNetwork>
#include<QLabel>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
tcpSocket = new QTcpSocket(this);
connect(ui->pushButton_send, SIGNAL(clicked()), this, SLOT(sendMessage()));//当点击发送的时候发送数据出去
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readMessage()));//当有消息接受时会触发接收
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));
connect(ui->pushButton_login,SIGNAL(clicked()),this,SLOT(newConnect())); //新建一个连接
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::newConnect()
{
qDebug()<<5;
// 初始化数据大小信息为0
blockSize = 0;
// 取消已有的连接
tcpSocket->abort();//终止当前的连接
tcpSocket->connectToHost(ui->lineEdit_ip->text(),
ui->lineEdit_duankou->text().toInt());//与服务器连接,在对话框中输入ip和端口号
/*if(tcpSocket->waitForConnected(1000))
{
qDebug()<<"连接成功!"<<endl;
ui->messageLabel->setText("成功!");
}
else
ui->messageLabel->setText("超时");*/
}
void MainWindow::readMessage()
{
qDebug()<<4;
QDataStream in(tcpSocket);
// 设置数据流版本,这里要和服务器端相同
in.setVersion(QDataStream::Qt_4_6);
// 如果是刚开始接收数据
if (blockSize == 0) {
//判断接收的数据是否大于两字节,也就是文件的大小信息所占的空间
//如果是则保存到blockSize变量中,否则直接返回,继续接收数据
if(tcpSocket->bytesAvailable() < (int)sizeof(quint16)) return; //bytesAvailable()返回字节数
in >> blockSize;
}
// 如果没有得到全部的数据,则返回,继续接收数据
if(tcpSocket->bytesAvailable() < blockSize) return;
// 将接收到的数据存放到变量中
in >> message;
// 显示接收到的数据*/
qDebug() << message;
ui->messageLabel->setText(message);
if(message=="OK")
ui->messageLabel->setText("注册成功!");
blockSize = 0;
}
void MainWindow::displayError(QAbstractSocket::SocketError)
{
qDebug() << tcpSocket->errorString();
}
void MainWindow::sendMessage()
{
QString username;
username=ui->lineEdit_username->text();
int lenth1=username.length();
if(lenth1<10)
{
int n=10-lenth1;
for(int i=0;i<n;i++)
username=username+" ";
}
else
username=username.mid(0,10);
qDebug()<<"姓名:"<<username<<endl;
QString passward;
passward=ui->lineEdit_passward->text();
int lenth2=passward.length();
if(lenth2<10)
{
int m=10-lenth1;
for(int i=0;i<m;i++)
username=username+" ";
}
else
passward=passward.mid(0,10);
// 用于暂存我们要发送的数据
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
// 设置数据流的版本,客户端和服务器端使用的版本要相同
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out<<tr("helloworld");
out << username;
out<<passward;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
// 发送数据成功后,显示提示
tcpSocket->write(block);
qDebug()<<"发送成功"<<endl;
}