基于qt的一个tcp通信小程序

//client端//
///main.cpp///
#include
#include "client.h"
int main(int argc,char **argv)
{
QApplication app(argc,argv);
Dialog dlg;
dlg.show();
return app.exec();

}
//client.h///
#ifndef _CLIENT_H_
#define _CLIENT_H_
#include
#include
#include
#include
#include
class Dialog:public QDialog
{
Q_OBJECT

public:
Dialog();
private:
QPushButton *connectButton;
QPushButton *sendButton;
QPushButton *exitButton;
QTextEdit *textEdit;
QGridLayout *Layout;
QTcpSocket *tcpSocket;
public slots:
void send_slot();
void recv_slot();
void connect_slot();
void enable_sendButton();
};
#endif
///client.cpp/
#include "client.h"
【基于qt的一个tcp通信小程序】Dialog::Dialog()
{
connectButton = new QPushButton("connect");
sendButton = new QPushButton("send");
exitButton = new QpushButton("exit");

textEdit = new QTextEdit;

layout = new QGridLayout;

layout -> addWidget(textEdit,0,0,4,8);
layout -> addWidget(connectButton,5,0,1,2);
layout -> addWidget(sendButton,5,3,1,2);
layout -> addWidget(exitButton,5,6,1,2);

setLayout(layout);

sendButton -> setDisabled(true);
tcpSocket = new QTcpSocket(this);

connect(connectButton,SIGNAL(clicked()),this,SLOT(connect_slot()));
connect(sendButton,SIGNAL(clicked()),this,SLOT(send_slot()));
connect(exitButton,SIGNAL(clicked()),this,SLOT(close()));
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(recv_slot()));
connect(tcpSocket,SIGNAL(connected()),this,SLOT(enable_sendButton));

}
void Dialog::enable_sendButton()
{
sendButton -> setDisabled(false);
}
void Dialog::connect_slot()
{
tcpSocket -> connectToHost(QHostAddress::LocalHost,8000);
}
void Dialog::send_slot()
{
QString str = textEdit -> toPlainText();
tcpSocket -> write(qPrintable(str));
}
void Dialog::recv_slot()
{

QByteArray byte;
byte = tcpSocket -> readAll();
textEdit -> setPlainText(QString(byte));
}
//server端//
//main.cpp//
#include
#include "server.h"
int main(int argc,char **argv)
{
QApplication app(argc,argv);

Dialog dlg;
dlg.show();

return app.exec();
}
/server.h///
#ifndef _SERVER_H_
#define _SERVER_H_
#include
#include
#include
#include
#include
#include
class Dialog:public QDialog
{
Q_OBJECT
public :
Dialog();
private:
QPushButton *sendButton;
QPushButton *exitButton;
QTextEdit *textEdit;
QGridLayout *layout
QTcpServer *tcpServer
QTcpSocket *tcpSocket;
public slot:
void send_slot();
void recv_slot();
void connect_slot();
};
#endif
/server.cpp///
#include "server.h"
Dialog::Dialog()
{
sendButton = new QPushButton("send");
exitButton = new QPushButton("exit");
textEdit = new QTextEdit();
layout = new QGridLayot();
tcpServer = new QTcpServer;
layout -> addWidget(textEdit,0,0,4,6);
layout -> addWidget(sendButton,5,0,1,2);
layout -> addWidget(exitButton,5,4,1,2);

setLayout(layout);

tcpServer -> listen(QHostAddress::LocalHost,8000);
connect(tcpServer,SIGNAL(newConnection()),this,SLOT(connect()));
connect(sendButton,SIGNAL(clicked()),this,SLOT(send_slot()));
connect(exitButton,SIGNAL(clicked()),this,SLOT(close()));
}
void Dialog::connect_slot()
{
tcpSocket = tcpServer -> nextPendingConnection();
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(recv_slot()));
}
void Dialog::send_slot()
{
QString str = textEdit -> toPlainText();
tcpSocket -> write(qPrintable(str));
}
void Dialog::recv_slot()
{
QByteArray byte;
byte = tcpSocket -> readAll();
textEdit -> setPlainText(QString(byte));
}

    推荐阅读