1,主界面
2,代码
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include
<QTcpSocket> #include <QLabel> namespace Ui { class MainWindow; } class
MainWindow : public QMainWindow { Q_OBJECT private: QTcpSocket *tcpClient;
//socket QLabel *LabSocketState; //状态栏显示标签 QString getLocalIP();//获取本机IP地址
protected: void closeEvent(QCloseEvent *event); public: explicit
MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: //自定义槽函数 void
onConnected(); void onDisconnected(); void
onSocketStateChange(QAbstractSocket::SocketState socketState); void
onSocketReadyRead();//读取socket传入的数据 // void on_actConnect_triggered(); void
on_actDisconnect_triggered(); void on_actClear_triggered(); void
on_btnSend_clicked(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QHostAddress>
#include <QHostInfo> QString MainWindow::getLocalIP() { QString
hostName=QHostInfo::localHostName();//本地主机名 QHostInfo
hostInfo=QHostInfo::fromName(hostName); QString localIP=""; QList<QHostAddress>
addList=hostInfo.addresses();// if (!addList.isEmpty()) for (int
i=0;i<addList.count();i++) { QHostAddress aHost=addList.at(i); if
(QAbstractSocket::IPv4Protocol==aHost.protocol()) { localIP=aHost.toString();
break; } } return localIP; } void MainWindow::closeEvent(QCloseEvent *event) {
if (tcpClient->state()==QAbstractSocket::ConnectedState)
tcpClient->disconnectFromHost(); event->accept(); }
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new
Ui::MainWindow) { ui->setupUi(this); tcpClient=new QTcpSocket(this);
//创建socket变量 LabSocketState=new QLabel(u8"Socket状态:");//状态栏标签
LabSocketState->setMinimumWidth(250); ui->statusBar->addWidget(LabSocketState);
QString localIP=getLocalIP();//本机IP
this->setWindowTitle(this->windowTitle()+u8"----本机IP:"+localIP);
ui->comboServer->addItem(localIP);
connect(tcpClient,SIGNAL(connected()),this,SLOT(onConnected()));
connect(tcpClient,SIGNAL(disconnected()),this,SLOT(onDisconnected()));
connect(tcpClient,SIGNAL(stateChanged(QAbstractSocket::SocketState)),
this,SLOT(onSocketStateChange(QAbstractSocket::SocketState)));
connect(tcpClient,SIGNAL(readyRead()), this,SLOT(onSocketReadyRead())); }
MainWindow::~MainWindow() { delete ui; } void MainWindow::onConnected() {
//connected()信号槽函数 ui->plainTextEdit->appendPlainText(u8"**已连接到服务器");
ui->plainTextEdit->appendPlainText(u8"**peer address:"+
tcpClient->peerAddress().toString());
ui->plainTextEdit->appendPlainText(u8"**peer port:"+
QString::number(tcpClient->peerPort())); ui->actConnect->setEnabled(false);
ui->actDisconnect->setEnabled(true); } void MainWindow::onDisconnected()
{//disConnected()信号槽函数 ui->plainTextEdit->appendPlainText(u8"**已断开与服务器的连接");
ui->actConnect->setEnabled(true); ui->actDisconnect->setEnabled(false); } void
MainWindow::onSocketReadyRead() {//readyRead()信号槽函数
while(tcpClient->canReadLine()) ui->plainTextEdit->appendPlainText("[in]
"+tcpClient->readLine()); } void
MainWindow::onSocketStateChange(QAbstractSocket::SocketState socketState)
{//stateChange()信号槽函数 switch(socketState) { case
QAbstractSocket::UnconnectedState:
LabSocketState->setText(u8"scoket状态:UnconnectedState"); break; case
QAbstractSocket::HostLookupState:
LabSocketState->setText(u8"scoket状态:HostLookupState"); break; case
QAbstractSocket::ConnectingState:
LabSocketState->setText(u8"scoket状态:ConnectingState"); break; case
QAbstractSocket::ConnectedState:
LabSocketState->setText(u8"scoket状态:ConnectedState"); break; case
QAbstractSocket::BoundState: LabSocketState->setText(u8"scoket状态:BoundState");
break; case QAbstractSocket::ClosingState:
LabSocketState->setText(u8"scoket状态:ClosingState"); break; case
QAbstractSocket::ListeningState:
LabSocketState->setText(u8"scoket状态:ListeningState"); } } void
MainWindow::on_actConnect_triggered() {//连接到服务器 QString
addr=ui->comboServer->currentText(); quint16 port=ui->spinPort->value();
tcpClient->connectToHost(addr,port); //
tcpClient->connectToHost(QHostAddress::LocalHost,port); } void
MainWindow::on_actDisconnect_triggered() {//断开与服务器的连接 if
(tcpClient->state()==QAbstractSocket::ConnectedState)
tcpClient->disconnectFromHost(); } void MainWindow::on_actClear_triggered() {
ui->plainTextEdit->clear(); } void MainWindow::on_btnSend_clicked() {//发送数据
QString msg=ui->editMsg->text(); ui->plainTextEdit->appendPlainText("[out]
"+msg); ui->editMsg->clear(); ui->editMsg->setFocus(); QByteArray
str=msg.toUtf8(); str.append('\n'); tcpClient->write(str); }