在VS上开发了一个TCP聊天小程序
使用软件为VS2015,包含了msvc2015版本qt,建立程序时选择QWidget,创建时模块选择记得勾上Network模块,否则无法识别。本程序未使用ui文件,所以环境配置一样的话直接可以使用。若环境不一样也可以用于新手学习参考,特别简单。
client.h
#pragma once
#include <QtWidgets/QWidget>
#include "ui_WeSpeak3_client.h"
#include <QtNetwork>
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
class QAbstractSocket;
class QPushButton;
class QTextEdit;
class QPlainTextEdit;
class QLabel;
class QTcpSocket;
class QString;
class QLineEdit;
class QTcpSocket;
class WeSpeak3_client : public QWidget
{
Q_OBJECT
public:
WeSpeak3_client(QWidget *parent = Q_NULLPTR);
private:
Ui::WeSpeak3_clientClass ui;
QTcpSocket * client;
QLabel *lab_sIP;
QLabel * lab_sPort;
QPushButton * button_enter;
QTextEdit * text_enter;
QPlainTextEdit * text_rev;
QLineEdit * sIP;
QLineEdit * sPort;
QPushButton* button_close;
QTcpSocket* conn;
private slots:
void on_sendBtn_clicked();
};
client.c
#include "WeSpeak3_client.h"
#include <QtNetwork>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <iostream>
#include <string>
#include <QTextEdit>
#include <QPlainTextEdit>
#include <QVBoxLayout>
#include <QHostAddress>
#include <QAbstractSocket>
#include <QTcpSocket>
#include <QByteArray>
#include <QString>
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
WeSpeak3_client::WeSpeak3_client(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
this->setFixedSize(800, 600);
lab_sIP = new QLabel(this);
lab_sPort = new QLabel(this);
lab_sIP->setText("IP");
lab_sIP->move(15, 35);
lab_sPort->setText("Port");
lab_sPort->move(15, 60);
sIP = new QLineEdit(this);
sPort = new QLineEdit(this);
sIP->setText("192.168.20.35");
sIP->move(50, 30);
sPort->setText("8888");
sPort->move(50, 55);
//发送按钮
button_enter = new QPushButton(this);
button_enter->setFixedSize(80, 30);
button_enter->move(460, 520);
button_enter->setText("发送(Enter)");
button_enter->setShortcut(QKeySequence(QLatin1String("Enter")));
//文本输入框
//QLineEdit* line_enter = new QLineEdit(this);
text_enter = new QTextEdit(this);
text_enter->setFixedSize(400, 100);
text_enter->move(50, 450);
text_enter->setPlaceholderText("please input what do you want to say by press 'enter'");
//消息获得框
text_rev = new QPlainTextEdit(this);
text_rev->setPlaceholderText("message recive");
text_rev->setFixedSize(400, 300);
text_rev->move(50, 100);
button_close = new QPushButton(this);
button_close->move(720, 10);
button_close->setText("关闭");
connect(button_close, &QPushButton::clicked, [=]()
{
close();
});
client = new QTcpSocket(this);
client->connectToHost(sIP->text(), sPort->text().toInt());
connect(client, &QTcpSocket::readyRead, this, [=]()
{ QByteArray buf;
QByteArray array = client->readAll();
array = "Server say:" + array+"\r";
//buf = array;
text_rev->appendPlainText(array);
}
);
connect(button_enter, &QPushButton::clicked, this, &WeSpeak3_client::on_sendBtn_clicked);
}
void WeSpeak3_client::on_sendBtn_clicked()
{
QString aha = text_enter->toPlainText();
client->write(aha.toUtf8());
this->text_rev->appendPlainText("Client Say:" + aha);
this->text_enter->clear();
}
server.h
#pragma once
#include <QtWidgets/QWidget>
#include "ui_WeSpeek3_SERVER.h"
#include <QtNetwork>
#include <QTcpServer>
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
class QAbstractSocket;
class QTcpServer;
class QPushButton;
class QTextEdit;
class QPlainTextEdit;
class QLabel;
class QTcpSocket;
class QString;
class QLineEdit;
class QTcpSocket;
class WeSpeek3_SERVER : public QWidget
{
Q_OBJECT
public:
WeSpeek3_SERVER(QWidget *parent = Q_NULLPTR);
private:
Ui::WeSpeek3_SERVERClass ui;
QTcpServer * server;
QLabel *lab_sIP;
QLabel * lab_sPort;
QPushButton * button_enter;
QTextEdit * text_enter;
QPlainTextEdit * text_rev;
QLineEdit * sIP;
QLineEdit * sPort;
QPushButton* button_close;
QTcpSocket* conn;
private slots:
void conn_read();
void on_sendBtn_clicked();
};
server.c
#include "WeSpeek3_SERVER.h"
#include <QtNetwork>
#include <QTcpServer>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <iostream>
#include <string>
#include <QTextEdit>
#include <QPlainTextEdit>
#include <QVBoxLayout>
#include <QHostAddress>
#include <QAbstractSocket>
#include <QTcpSocket>
#include <QByteArray>
#include <QString>
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
WeSpeek3_SERVER::WeSpeek3_SERVER(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
this->setFixedSize(800, 600);
lab_sIP = new QLabel(this);
lab_sPort = new QLabel(this);
lab_sIP->setText("IP");
lab_sIP->move(15, 35);
lab_sPort->setText("Port");
lab_sPort->move(15, 60);
sIP = new QLineEdit(this);
sPort = new QLineEdit(this);
sIP->setText("192.168.20.35");
sIP->move(50, 30);
sPort->setText("8888");
sPort->move(50, 55);
//发送按钮
button_enter = new QPushButton(this);
button_enter->setFixedSize(80, 30);
button_enter->move(460, 520);
button_enter->setText("发送(Enter)");
button_enter->setShortcut(QKeySequence(QLatin1String("Enter")));
//文本输入框
//QLineEdit* line_enter = new QLineEdit(this);
text_enter = new QTextEdit(this);
text_enter->setFixedSize(400, 100);
text_enter->move(50, 450);
text_enter->setPlaceholderText("please input what do you want to say by press 'enter'");
//消息获得框
text_rev = new QPlainTextEdit(this);
text_rev->setPlaceholderText("message recive");
text_rev->setFixedSize(400, 300);
text_rev->move(50, 100);
button_close = new QPushButton(this);
button_close->move(720, 10);
button_close->setText("关闭");
connect(button_close, &QPushButton::clicked, [=]()
{
close();
});
conn = new QTcpSocket(this);
server = new QTcpServer(this);
server->listen(QHostAddress(sIP->text()),sPort->text().toInt());
//newconnection
connect(server, &QTcpServer::newConnection, this, &WeSpeek3_SERVER::conn_read);
connect(button_enter, &QPushButton::clicked, this, &WeSpeek3_SERVER::on_sendBtn_clicked);
}
void WeSpeek3_SERVER::conn_read()
{
conn = server->nextPendingConnection();//返回客户端的套接字对象地址
text_rev->setPlainText("has new client connect");
connect(conn, &QTcpSocket::readyRead, this, [=]()
{
QByteArray array = conn->readAll();
array = "Client say:" + array;
this->text_rev->appendPlainText(array);
});
}
void WeSpeek3_SERVER::on_sendBtn_clicked()
{
QString aha = text_enter->toPlainText();
conn->write(aha.toUtf8());
this->text_rev->appendPlainText("Server Say:" + aha);
this->text_enter->clear();
}