您好,登錄后才能下訂單哦!
這篇文章主要介紹了C++基于socket多線程怎么實(shí)現(xiàn)網(wǎng)絡(luò)聊天室的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇C++基于socket多線程怎么實(shí)現(xiàn)網(wǎng)絡(luò)聊天室文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。
1. 實(shí)現(xiàn)圖解
2. 聊天室服務(wù)端:TCP_Server_Chat.cpp
#include <winsock2.h> // winsock2的頭文件 #include <iostream> #pragma comment(lib, "ws2_32.lib") using namespace std; // stdcall的線程處理函數(shù) DWORD WINAPI ThreadFun(LPVOID lpThreadParameter); int main() { WSADATA wd; if(WSAStartup(MAKEWORD(2,2), &wd) != 0) { cout << "WSAStartup Error:" << WSAGetLastError() << endl; return 0; } // 1. 創(chuàng)建流式套接字 SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(s == INVALID_SOCKET) { cout << "socket error:" << WSAGetLastError() << endl; return 0; } // 2. 綁定端口和ip sockaddr_in addr; memset(&addr, 0, sizeof(sockaddr_in)); addr.sin_family = AF_INET; addr.sin_port = htons(8000); addr.sin_addr.s_addr = inet_addr("127.0.0.1"); int len = sizeof(sockaddr_in); if(bind(s, (SOCKADDR*)&addr, len) == SOCKET_ERROR) { cout << "bind Error:" << WSAGetLastError() << endl; return 0; } // 3. 監(jiān)聽(tīng) listen(s,5); // 主線程循環(huán)接收客戶端的連接 while(true) { sockaddr_in addrClient; len = sizeof(sockaddr_in); // 4.接受成功返回與client通訊的Socket SOCKET c = accept(s, (SOCKADDR*)&addrClient, &len); if(c != INVALID_SOCKET) { // 創(chuàng)建線程,并且傳入與client通訊的套接字 HANDLE hThread = CreateThread(NULL, 0, ThreadFun, (LPVOID)c, 0, NULL); CloseHandle(hThread); // 關(guān)閉對(duì)線程的引用 } } // 6.關(guān)閉監(jiān)聽(tīng)套接字 closesocket(s); // 清理winsock2的環(huán)境 WSACleanup(); return 0; } DWORD WINAPI ThreadFun(LPVOID lpThreadParameter) { // 5.與客戶端通訊,發(fā)送或者接受數(shù)據(jù) SOCKET c= (SOCKET)lpThreadParameter; cout << "歡迎" << c << "進(jìn)入聊天室!" << endl; // 發(fā)送數(shù)據(jù) char buf[100] = {0}; sprintf(buf,"歡迎 %d 進(jìn)入聊天室!",c); send(c,buf, 100, 0); // 循環(huán)接收客戶端數(shù)據(jù) int ret=0; do { char buf2[100] = {0}; ret = recv(c, buf2, 100, 0); cout << c << " 說(shuō):" << buf2 << endl; } while (ret!=SOCKET_ERROR && ret!=0); cout << c << "離開了聊天室!"; return 0; }
啟動(dòng)聊天室服務(wù)端:
3. 聊天室客戶端:TCP_Client_Chat.cpp
#include<winsock2.h>//winsock2的頭文件 #include<iostream> using namespace std; //勿忘,鏈接dll的lib #pragma comment(lib, "ws2_32.lib") int main() { //加載winsock2的環(huán)境 WSADATA wd; if (WSAStartup(MAKEWORD(2, 2), &wd) != 0) { cout << "WSAStartup error:" << GetLastError() << endl; return 0; } //1.創(chuàng)建流式套接字 SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (s == INVALID_SOCKET) { cout << "socket error:" << GetLastError() << endl; return 0; } //2.鏈接服務(wù)器 sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(8000); addr.sin_addr.s_addr = inet_addr("127.0.0.1"); int len = sizeof(sockaddr_in); if (connect(s, (SOCKADDR*)&addr, len) == SOCKET_ERROR) { cout << "connect error:" << GetLastError() << endl; return 0; } //3接收服務(wù)端的消息 char buf[100] = { 0 }; recv(s, buf, 100, 0); cout << buf << endl; //3隨時(shí)給服務(wù)端發(fā)消息 int ret = 0; do { char buf[100] = {0}; cout << "請(qǐng)輸入聊天內(nèi)容:"; cin >> buf; ret = send(s, buf, 100, 0); } while (ret != SOCKET_ERROR&& ret != 0); //4.關(guān)閉監(jiān)聽(tīng)套接字 closesocket(s); //清理winsock2的環(huán)境 WSACleanup(); return 0; }
啟動(dòng)2個(gè)客戶端
關(guān)于“C++基于socket多線程怎么實(shí)現(xiàn)網(wǎng)絡(luò)聊天室”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“C++基于socket多線程怎么實(shí)現(xiàn)網(wǎng)絡(luò)聊天室”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。