您好,登錄后才能下訂單哦!
本篇內容介紹了“怎么用C++實現聊天小程序”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
C++寫一個游戲聊天服務器,供大家參考,具體內容如下
最近學習網絡編程寫了個聊天小程序,寫個博客記錄下自己的代碼
c++網絡編程
c++多線程
c++ STL
以一個結構體的形式存儲客戶端,用vector存取存在的客戶端,開啟多線程處理邏輯
服務器允許登陸多個客戶端,允許公屏聊天也允許私聊,默認情況下屬于公屏聊天,若想私聊,格式為“@用戶名+要發送的消息”;運行效果如下圖:
#include "stdafx.h" #include <iostream> #include "windows.h" //一定要包含該頭文件 #include "process.h" #include <string> #include <vector> #include <algorithm> using namespace std; #pragma comment(lib, "WS2_32.lib") //顯示加載 ws2_32.dll ws2_32.dll就是最新socket版本 int g_curPlayerNum = 0; //當前連接數 const char*g_PlayerName[] = //假定的聊天者名字 { "aaaa", "bbbb", "cccc", "dddd", }; struct PlayerInfo //利用結構存儲連接的客戶端 { SOCKET sock; string name; }; vector<PlayerInfo>g_clientSockList; //利用vector存取已連接的客戶端 void process(void*param) { int index = *(int*)param; //當前子線程編號 while (1) { //服務器接收信息 //int index = *(int*)param; char buf[2048] = { 0 }; //接收緩沖區 int bytes; if ((bytes = recv(g_clientSockList[index].sock, buf, sizeof(buf), 0)) == SOCKET_ERROR) { cout << "服務器接收數據失敗!" << endl; } //服務器轉發(含邏輯處理) if (buf[0] == '@') { //私聊 string Buf(buf); string recvPlayerName = Buf.substr(1, 4); //分離出接收者名字 copy(g_clientSockList[index].name.begin(), g_clientSockList[index].name.end(), &buf[1]); for (vector<PlayerInfo>::iterator it = g_clientSockList.begin(); it != g_clientSockList.end(); it++) { if (it->name == recvPlayerName) { if (send(it->sock, buf, strlen(buf), 0) == SOCKET_ERROR) { cout << "發送數據失敗 to" << it->name << endl; } break; } } } else //群聊 cout << g_clientSockList[index].name << "對" << "所有人說:" << buf << endl; } } int main() { cout << "-----------聊天室服務器-----------" << endl; //套接字初始化 WSADATA wsaData; //這個結構被用來存儲被WSAStartup函數調用后返回的 Windows Sockets 數據。 WORD sockVersion = MAKEWORD(2, 2); //windows網絡編程庫的版本號信息 if (WSAStartup(sockVersion, &wsaData) != 0) //WSAStartup函數是在程序中初始化并加載Windows網絡 { cout << "套接字初始化失敗!" << endl; return 0; } //創建服務器套接字 SOCKET SeverSocket; if ((SeverSocket = socket(AF_INET, SOCK_STREAM, 0)) == SOCKET_ERROR) { cout << "套接字創建失敗!" << endl; return 0; } struct sockaddr_in SeverAddress; //一個綁定地址:有IP地址,有端口號,有協議族 memset(&SeverAddress, 0, sizeof(sockaddr_in)); //初始化結構體 SeverAddress.sin_family = AF_INET; SeverAddress.sin_addr.S_un.S_addr = htonl(INADDR_ANY);//填入本機IP地址 SeverAddress.sin_port = htons(60000);//設定端口號 //綁定套接字 指定綁定的IP地址和端口號 if (bind(SeverSocket, (sockaddr*)&SeverAddress, sizeof(SeverAddress)) == SOCKET_ERROR) { cout << "套接字綁定失敗!"<<endl; return 0; } //服務器監聽 if (listen(SeverSocket, SOMAXCONN) == SOCKET_ERROR) //監聽的第二個參數就是:能存放多少個客戶端請求,到并發編程的時候很有用 { cout << "監聽失敗!" << endl; return 0; } else cout << "服務器等待連接......" << endl; while (1) { //服務器接受連接請求 sockaddr_in revClientAddress; //套接字的地址,端口 SOCKET revClientSocket = INVALID_SOCKET; //用來接收客戶端連接 //memset(&revClientAddress, 0, sizeof(revClientAddress)); int addlen = sizeof(revClientAddress); if ((revClientSocket = accept(SeverSocket, (sockaddr*)&revClientAddress, &addlen)) == INVALID_SOCKET) { cout << "接受客戶端連接失敗!" << endl; return 0; } PlayerInfo stPlayerInfo; stPlayerInfo.sock = revClientSocket; stPlayerInfo.name = g_PlayerName[g_curPlayerNum]; g_clientSockList.push_back(stPlayerInfo); int temp = g_curPlayerNum; _beginthread(process, 0, &temp); //創建子線程來收發數據 g_curPlayerNum++; cout << stPlayerInfo.name << "上線啦!" << endl; } return 0; }
#include "stdafx.h" #include "windows.h" #include "iostream" #include "process.h" #include <string> using namespace std; #pragma comment(lib, "ws2_32.lib") void Receive(void *param) { string msg; while (1) { //客戶端接受來自服務器的數據 SOCKET clientSocket = *(SOCKET*)(param); char recvbuf[2048] = {}; //接收緩沖區 if (recv(clientSocket, recvbuf, 2048, 0) == SOCKET_ERROR) { cout << "數據接受失敗" << endl; } else { msg = recvbuf; char sendPlayerName[5] = { 0 }; int len = strlen(recvbuf); //消息長度 copy(&recvbuf[1], &recvbuf[5], sendPlayerName); //分離出名字 msg = msg.substr(5, len - 5); cout << sendPlayerName << "對你說:" << msg<<endl; } } } void Send(void *param) { while (1) { //客戶端發送數據給服務器 SOCKET clientSocket = *(SOCKET*)(param); char sendbuf[2048] = {}; //發送緩沖區 cin.getline(sendbuf, 2048); if (send(clientSocket, sendbuf, strlen(sendbuf), 0) == SOCKET_ERROR) { cout << "發送消息失敗!"; } else cout << "發送消息成功" << endl; } } int main() { cout << "-----------個人客戶端-----------" << endl; WSADATA wsa; if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { cout << "套接字初始化失敗!"<<endl; } SOCKET clientSocket; if ((clientSocket = socket(AF_INET, SOCK_STREAM, 0)) == SOCKET_ERROR) { cout << "套接字創建失敗!"<<endl; } Sleep(30); struct sockaddr_in ClientAddress; //一個綁定地址:有IP地址,有端口號,有協議族 memset(&ClientAddress, 0, sizeof(sockaddr_in)); //初始化結構體 ClientAddress.sin_family = AF_INET; ClientAddress.sin_addr.S_un.S_addr = htonl(INADDR_ANY);//填入本機IP地址 //ClientAddress.sin_port = htons(60001);//設定端口號 //綁定套接字 指定綁定的IP地址和端口號 if (bind(clientSocket, (sockaddr*)&ClientAddress, sizeof(ClientAddress)) == SOCKET_ERROR) { cout << "套接字綁定失敗!" << endl; return 0; } struct sockaddr_in SeverAddress; //服務器地址 也就是即將要連接的目標地址 memset(&SeverAddress, 0, sizeof(sockaddr_in)); SeverAddress.sin_family = AF_INET; SeverAddress.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); //127.0.0.1表示本機ip地址 SeverAddress.sin_port = htons(60000);//設定端口號 //開始連接 if (connect(clientSocket, (sockaddr*)&SeverAddress, sizeof(SeverAddress)) == SOCKET_ERROR) { cout << "客戶端:和服務器連接失敗!"<<endl; return 0; } else cout << "與服務器連接成功!" << endl; //創建兩個子線程 _beginthread(Receive, 0, &clientSocket); _beginthread(Send, 0, &clientSocket); Sleep(INFINITE); //這里采用另外一種技術避免主線程執行完退出——使其無限期休眠 // 關閉socket if (clientSocket != INVALID_SOCKET) { closesocket(clientSocket); clientSocket = INVALID_SOCKET; } return 0; }
“怎么用C++實現聊天小程序”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。