QT TCP客服端-服务端模式

client.h

****************************

#ifndef CLIENT_H

#define CLIENT_H

#include 
#include 
#include 
#include "enum.h"

class Client : public QWidget
{
Q_OBJECT
public:
explicit Client(QWidget *parent = 0);
private:
QTextEdit *textEdit;
QLineEdit *lineEdit;
QPushButton *sendBtn;
QPushButton *connectBtn;
QTcpSocket *m_tcpsocket;
QString userStr;

private slots:
void connectTcpServer();
void connectedServer();
void readMessage();
void sendMessage();
};

#endif // CLIENT_H
  
  
client.cpp
*******************************************
#include "client.h"

Client::Client(QWidget *parent) :
QWidget(parent)
{
userStr = QString(tr("刘红"));
textEdit = new QTextEdit;
textEdit->setReadOnly(true);

lineEdit = new QLineEdit;

connectBtn = new QPushButton("connect");
connect(connectBtn,SIGNAL(clicked()),this,SLOT(connectTcpServer()));
sendBtn = new QPushButton("send");
connect(sendBtn,SIGNAL(clicked()),this,SLOT(sendMessage()));

QHBoxLayout *bottomlayout = new QHBoxLayout;
bottomlayout->addWidget(connectBtn);
bottomlayout->addWidget(sendBtn);

QVBoxLayout *mainlayout = new QVBoxLayout;
mainlayout->addWidget(textEdit);
mainlayout->addWidget(lineEdit);
mainlayout->addLayout(bottomlayout);

setLayout(mainlayout);
}

void Client::connectTcpServer()
{

m_tcpsocket = new QTcpSocket(this);
m_tcpsocket->abort();
m_tcpsocket->connectToHost(QHostAddress::LocalHost,16666);

connect(m_tcpsocket,SIGNAL(connected()),this,SLOT(connectedServer()));
connect(m_tcpsocket,SIGNAL(readyRead()),this,SLOT(readMessage()));

textEdit->append(tr("连接服务器......")+'\n');
connectBtn->setEnabled(false);
}

void Client::connectedServer()
{
textEdit->append(tr("连接服务器成功!")+'\n');
}

void Client::readMessage()
{
QString string;
QByteArray block = m_tcpsocket->readAll();
QDataStream in(block);
in.setVersion(QDataStream::Qt_4_7);
in>>string;
textEdit->append(tr("用户有消息到来\n消息是:")+string+'\n');
}

void Client::sendMessage()
{
int type;
type = 1;
QString string = lineEdit->text();
QByteArray block;
QDataStream out(&block,QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
out<write(block);
lineEdit->clear();
}
 
server.h
**************************************
#ifndef SERVER_H
 
#define SERVER_H

#include 
#include 
#include 


class Server : public QWidget
{
Q_OBJECT
public:
explicit Server(QWidget *parent = 0);

private:
QTextEdit *textEdit;
QLineEdit *lineEdit;
QPushButton *sendBtn;
QPushButton *startBtn;
QTcpSocket *m_tcpsocket;
QTcpServer *m_tcpserver;

private slots:
void startTcpServer();
void sendMessage();
void newConnect();
void readMessage();
};

#endif // SERVER_H
  
server.cpp
****************************************
#include "server.h"

Server::Server(QWidget *parent) :
QWidget(parent)
{
textEdit = new QTextEdit;
textEdit->setReadOnly(true);

lineEdit = new QLineEdit;

startBtn = new QPushButton("start");
connect(startBtn,SIGNAL(clicked()),this,SLOT(startTcpServer()));
sendBtn = new QPushButton("send");
connect(sendBtn,SIGNAL(clicked()),this,SLOT(sendMessage()));
QHBoxLayout *bottomlayout = new QHBoxLayout;
bottomlayout->addWidget(startBtn);
bottomlayout->addWidget(sendBtn);

QVBoxLayout *mainlayout = new QVBoxLayout;
mainlayout->addWidget(textEdit);
mainlayout->addWidget(lineEdit);
mainlayout->addLayout(bottomlayout);

setLayout(mainlayout);
}

void Server::startTcpServer()
{
startBtn->setEnabled(false);
m_tcpserver = new QTcpServer;
m_tcpserver->listen(QHostAddress::Any,16666);
connect(m_tcpserver,SIGNAL(newConnection()),this,SLOT(newConnect()));
}

void Server::newConnect()
{
textEdit->append(tr("有用户链接...")+'\n');
m_tcpsocket = m_tcpserver->nextPendingConnection();
connect(m_tcpsocket,SIGNAL(readyRead()),this,SLOT(readMessage()));
}

void Server::readMessage()
{
QString userstr;
int type;
QString string;
QByteArray block = m_tcpsocket->readAll();
QDataStream in(block);
in.setVersion(QDataStream::Qt_4_7);
in>>userstr;
in>>type;
in>>string;
textEdit->append(userstr+tr("用户有消息到来\n消息是:")+string+'\n');
}

void Server::sendMessage()
{
QString string = lineEdit->text();
QByteArray block;
QDataStream out(&block,QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
out<write(block);
}



  
main.cpp
*********************************
#include 
#include 
#include "server.h"
#include "client.h"

int main(int argc,char* argv[])
{
QApplication app(argc,argv);
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
Server s;
s.show();
Client c1;
c1.show();
return app.exec();
} 

 

你可能感兴趣的:(QT)