QT版本:5.15.2
VS版本:2019
客戶端程序主要包含三塊:連接服務(wù)器,發(fā)送消息,關(guān)閉客戶端
服務(wù)端程序主要包含三塊:打開消息監(jiān)聽,接收消息并反饋,關(guān)閉服務(wù)端
1、先打開服務(wù)端監(jiān)聽功能
void TCPServer::listen()
{
initWsaData();
//創(chuàng)建套接字
sock = socket(AF_INET, SOCK_STREAM, 0);
//創(chuàng)建地址簇對象
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(9888);
sin.sin_addr.s_addr = htonl(INADDR_ANY);
//綁定套接字
int bindStatus = ::bind(sock, (struct sockaddr*)&sin, sizeof(sin));
if (bindStatus == -1) {
qDebug() << "socket bind failed!" << endl;
ui.textBrowser->setText("socket bind failed!");
return;
}
else {
qDebug() << "socket bind success!" << endl;
ui.textBrowser->setText("socket bind success!");
}
//將套接字設(shè)為監(jiān)聽模式,等待客戶端連接
int listenStatus = ::listen(sock, 128);
if (listenStatus == -1) {
qDebug() << "listen failed" << endl;
ui.textBrowser->setText("listen failed");
return;
}
else {
qDebug() << "set listen success, server is listening..." << endl;
ui.textBrowser->setText("set listen success, server is listening...");
}
//收到請求主后,接收連接請求,返回一個對應(yīng)此次連接的新套接字
//接受連接請求
sockaddr_in sinAccept;
int len = sizeof(sin);
newSock = accept(sock, (struct sockaddr*)&sinAccept, &len);
if (newSock == SOCKET_ERROR) {
qDebug() << "connect failed" << endl;
ui.textBrowser->setText("connect failed");
return;
}
else {
qDebug() << "connect success, ready to recv data" << endl;
ui.textBrowser->setText("connect success, ready to recv data");
}
}
2、點擊客戶端connect連接服務(wù)端
void TCPClient::Connect(const std::string ip, const int port)
{
bool res = initWsaData(); //初始化套接字庫
if (!res) return;
//常用協(xié)議族:AF_UNIX(本機通信)AF_INET(TCP/IP – IPv4)AF_INET6(TCP/IP – IPv6)
//套接字類型:SOCK_STREAM(TCP流)SOCK_DGRAM(UDP數(shù)據(jù)報)SOCK_RAW(原始套接字)
//protocol”一般設(shè)置為“0”
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == SOCKET_ERROR) {
qDebug() << "Failed to create socket" << endl;
ui.textBrowser->setText("Failed to create socket");
return;
}
else {
qDebug() << "Socket created successfully" << endl;
ui.textBrowser->setText("Socket created successfully");
}
//設(shè)置地址
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
sin.sin_addr.s_addr = inet_addr(ip.c_str());
//連接服務(wù)器
int connStatus = ::connect(sock, (struct sockaddr*)&sin, sizeof(sin));
if (connStatus == -1) {
qDebug() << "Failed to connect to the server" << endl;
ui.textBrowser->setText("Failed to connect to the server");
return;
}
}
3、在客戶端輸入消息點擊send發(fā)送到服務(wù)端
void TCPClient::DataTransmission()
{
string msg = ui.lineEdit->text().toStdString();
/*char sendBuf[MAX_MSG_SIZE];
memset(sendBuf, 0, MAX_MSG_SIZE)*/;
int sendStatus = send(sock, msg.c_str(), MAX_MSG_SIZE, 0);
if (sendStatus == 0) {
qDebug() << "Failed to send information" << endl;
ui.textBrowser->setText("Failed to send information");
//關(guān)閉套接字及套接字庫
closesocket(sock);
WSACleanup();
return;
}
char recvBuf[MAX_MSG_SIZE];
memset(recvBuf, 0, MAX_MSG_SIZE);
int recvStatus = recv(sock, recvBuf, MAX_MSG_SIZE, 0);
if (recvStatus == -1) {
qDebug() << "Failed to receive message" << endl;
ui.textBrowser->setText("Failed to receive message");
//關(guān)閉套接字及套接字庫
/* closesocket(sock);
WSACleanup();*/
return;
}
else {
qDebug() << "Server information:" << recvBuf << endl;
string msg = "---------Server information:" + string(recvBuf);
ui.textBrowser->setText(QString::fromStdString(msg));
}
}
4、在服務(wù)端點擊send接收客戶端消息并通知客戶端已收到消息
void TCPServer::DataTransmission()
{
//用新建立的套接字和客戶端進行通信
char recvBuf[MAX_MSG_SIZE];
char sendBuf[MAX_MSG_SIZE];
memset(recvBuf, 0, MAX_MSG_SIZE);
memset(sendBuf, 0, MAX_MSG_SIZE);
if (true) {
int recvStatus = recv(newSock, recvBuf, MAX_MSG_SIZE, 0);
if (recvStatus == -1) {
qDebug() << "recv data failed" << endl;
ui.textBrowser->setText("recv data failed");
closesocket(sock);
closesocket(newSock);
WSACleanup();
return;
}
else {
qDebug() << "recv client new msg:" << recvBuf << endl;
string msg = "--------------recv client new msg:" + string(recvBuf);
ui.textBrowser->setText(QString::fromStdString(msg));
}
/*qDebug() << "請輸入回復(fù)消息:";
cin >> sendBuf;*/
string sendMsg = "has recv msg-------";
int sendStatus = send(newSock, sendMsg.c_str(), sizeof(sendBuf), 0);
if (sendStatus == -1) {
qDebug() << "msg send failed" << endl;
ui.textBrowser->setText("msg send failed");
closesocket(sock);
closesocket(newSock);
WSACleanup();
return;
}
}
}
=====================文章來源:http://www.zghlxwxcb.cn/news/detail-775483.html
完整源碼下載

??博客主頁: 主頁
??歡迎點贊 ?? 收藏 ?留言 ?? 如有錯誤敬請指正!
??本文由 夢回闌珊 原創(chuàng),首發(fā)于 CSDN,轉(zhuǎn)載注明出處??
??代碼改變世界,你來改變代碼!?文章來源地址http://www.zghlxwxcb.cn/news/detail-775483.html
到了這里,關(guān)于《QT從基礎(chǔ)到進階·十六》QT實現(xiàn)客戶端和服務(wù)端的簡單交互的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!