重点提取:
1.保证服务端和客户端端口号一致
2.保证服务端和客户端在同一网段(可以通过网线连接)
3保证客户端界面输入的ip是服务段的ip
实现步骤:
首先,构造服务端界面和客户端界面如下
服务端界面
客户端界面
其次具体代码
在.pro文件的QT += core gui下面加入
QT += network
服务段代码
my_server.h
#ifndef MY_SERVER_H
#define MY_SERVER_H
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QString>
namespace Ui {
class My_server;
}
class My_server : public QWidget
{
Q_OBJECT
public:
explicit My_server(QWidget *parent = nullptr);
~My_server();
QTcpServer *tcpserver;
QTcpSocket *tcpsocket;
private slots:
void on_connectbt_clicked();
void newConnection_Slot();
void readyRead_Slot();
void on_disconnectbt_clicked();
void on_sendbt_clicked();
private:
Ui::My_server *ui;
};
#endif // MY_SERVER_H
my_server.cpp
#include "my_server.h"
#include "ui_my_server.h"
My_server::My_server(QWidget *parent) :
QWidget(parent),
ui(new Ui::My_server)
{
ui->setupUi(this);
setWindowTitle("服务器");
tcpserver=new QTcpServer(this);
tcpsocket=new QTcpSocket(this);
connect(tcpserver,SIGNAL(newConnection()),this,SLOT(newConnection_Slot()));
// connect(ui->connect,SIGNAL(clicked(bool)),this,SLOT(on_connectbt_clicked()));
// connect(ui->disconnect,SIGNAL(clicked(bool)),this,SLOT(on_disconnectbt_clicked()));
// connect(ui->send,SIGNAL(clicked(bool)),this,SLOT(on_sendbt_clicked()));
}
void My_server::newConnection_Slot()
{
//第三步:取出套接字
tcpsocket=tcpserver->nextPendingConnection(); //获取已经连接的客户端的SOCKET套接字
connect(tcpsocket,SIGNAL(readyRead()),this,SLOT(readyRead_Slot()));//若客户端有消息进来的话,会触发信号readyRead_Slot();
//获取对方的IP和端口
QString sIp = tcpsocket->peerAddress().toString();
quint16 sPort = tcpsocket->peerPort();
qDebug() << sIp << sPort;
//connect(tcpsocket, SIGNAL(disconnected()), this, SLOT(slotServerDisConnection()));//断开连接的话会触发disconnected();
}
void My_server::readyRead_Slot()
{
//第四步:读取套接字的内容
//从socket中读出数据
QString buf;
buf=tcpsocket->readAll();
ui->receivewd->appendPlainText(buf);
/*或
QByteArray baArray = tcpsocket->readAll();
QString sMsg = baArray;
ui->receivewd->appendPlainText(receivewd);
*/
}
void My_server::on_connectbt_clicked()//连接服务器
{
//第二部步:listen------监听是否有新的连接进来
tcpserver->listen(QHostAddress::Any,ui->portnum->text().toUInt());//监听端口号
}
void My_server::on_disconnectbt_clicked()//关闭服务器
{
tcpserver->close();
}
void My_server::on_sendbt_clicked()//发送信息
{
// toLatin1()
// tcpsocket->write(ui->sendwd->text().toLatin1());
// tcpsocket->write(ui->sendwd->text().toLocal8Bit(),ui->sendwd->text().length());
tcpsocket->write(ui->sendwd->text().toLocal8Bit().data());
}
My_server::~My_server()
{
delete ui;
}
客户端代码
my_client.h
#ifndef MY_CLIENT_H
#define MY_CLIENT_H
#include <QWidget>
#include <QTcpSocket>
namespace Ui {
class My_client;
}
class My_client : public QWidget
{
Q_OBJECT
public:
explicit My_client(QWidget *parent = nullptr);
~My_client();
QTcpSocket *tcpsocket;
private slots:
void on_openclient_clicked();
void connected_SLOT();
void readyRead_Slot();
void on_closeclient_clicked();
void on_sent_clicked();
private:
Ui::My_client *ui;
};
#endif // MY_CLIENT_H
my_client.cpp
#include "my_client.h"
#include "ui_my_client.h"
#include "QString"
#include "stdio.h"
My_client::My_client(QWidget *parent) :
QWidget(parent),
ui(new Ui::My_client)
{
ui->setupUi(this);
setWindowTitle("客户端");
tcpsocket=new QTcpSocket(this);
// connect(ui->open,SIGNAL(clicked(bool)),this,SLOT(on_openclient_clicked()));
// connect(ui->close,SIGNAL(clicked(bool)),this,SLOT(on_closeclient_clicked()));
// connect(ui->send,SIGNAL(clicked(bool)),this,SLOT(on_sent_clicked()));
}
void My_client::connected_SLOT()
{
QObject::connect(tcpsocket, &QTcpSocket::readyRead, this, &My_client::readyRead_Slot);
// connect(tcpsocket,SIGNAL(readyRead()),this,SLOT(readyRead_Slot()));//将信号连接到槽,书写比较明确
}
void My_client::readyRead_Slot()//定义接收信号的槽
{
QString buf;
buf=tcpsocket->readAll();
ui->receivewd->appendPlainText(buf);//接收由tcp发送过来的信息
// ui->receivewd->appendPlainText(buf.toUtf8());//接收由tcp发送过来的信息
}
void My_client::on_openclient_clicked()
{
//第一步:创建套接字,与服务端的IP地址和端口号连接.
//注:这里的端口号和上面服务端绑定的那个端口号是一样的,别搞错了.
//连接服务端
tcpsocket->connectToHost(ui->ipnum->text(),ui->portnum->text().toInt());//转为无符号,连接服务器端口
connect(tcpsocket,SIGNAL(connected()),this,SLOT(connected_SLOT()));
printf("打开客户端 ");
/*
//成功连接返回true,错误返回false
if(m_tsTcpSocket->waitForConnected())
{
qDebug() << "connect success";
}
*/
}
void My_client::on_closeclient_clicked()
{
tcpsocket->close();
printf("关闭客户端 ");
}
void My_client::on_sent_clicked()
{
tcpsocket->write(ui->sendwd->text().toLocal8Bit().data(),ui->sendwd->text().length());//丽丽
//tcpsocket->write(ui->sendwd->text().toLocal8Bit().data());//网上
//tcpsocket->write(ui->sendwd->toPlainText().toLatin1());//xie宏伟
/*
QString str;
int iSize = tcpsocket->write(str.toUtf8().data());//返回字节的大小
//等待write写完再写下一次的write
if(!tcpsocket->waitForBytesWritten(50000))
{
return;
}
if(iSize ==-1) //判断发送是否成功
{
qDebug() << "write error";
return;
}
}
My_client::~My_client()
{
delete ui;
}
两台ubuntu接上网线后网段设置
服务端
客户端
保证ip前三位一致,第四位稍作修改即可,就可以进行通讯啦。
结语
使用一台电脑操控另一台电脑的程序很有趣呢,请大家不妨试试看哈哈。