C++基於socket多線程實現網絡聊天室

本文實例為大傢分享瞭C++基於socket多線程實現網絡聊天室的具體代碼,供大傢參考,具體內容如下

1. 實現圖解

2. 聊天室服務端:TCP_Server_Chat.cpp

#include <winsock2.h> // winsock2的頭文件
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
using namespace std;

// stdcall的線程處理函數
DWORD WINAPI ThreadFun(LPVOID lpThreadParameter);

int main()
{
 WSADATA wd;
 if(WSAStartup(MAKEWORD(2,2), &wd) != 0)
 {
  cout << "WSAStartup Error:" << WSAGetLastError() << endl;
  return 0;
 }

 // 1. 創建流式套接字
 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. 監聽
 listen(s,5);

 // 主線程循環接收客戶端的連接
 while(true)
 {
  sockaddr_in addrClient;
  len = sizeof(sockaddr_in);
  // 4.接受成功返回與client通訊的Socket
  SOCKET c = accept(s, (SOCKADDR*)&addrClient, &len);
  if(c != INVALID_SOCKET)
  {
   // 創建線程,並且傳入與client通訊的套接字
   HANDLE hThread = CreateThread(NULL, 0, ThreadFun, (LPVOID)c, 0, NULL);
   CloseHandle(hThread); // 關閉對線程的引用
  }

 }

 // 6.關閉監聽套接字
 closesocket(s);
 
 // 清理winsock2的環境
 WSACleanup();

 return 0;
}

DWORD WINAPI ThreadFun(LPVOID lpThreadParameter)
{
 // 5.與客戶端通訊,發送或者接受數據
 SOCKET c= (SOCKET)lpThreadParameter;

 cout << "歡迎" << c << "進入聊天室!" << endl;

 // 發送數據
 char buf[100] = {0};
 sprintf(buf,"歡迎 %d 進入聊天室!",c);
 send(c,buf, 100, 0);

 // 循環接收客戶端數據
 int ret=0;
 do 
 {
  char buf2[100] = {0};
  ret = recv(c, buf2, 100, 0);

  cout << c << " 說:" << buf2 << endl;

 } while (ret!=SOCKET_ERROR && ret!=0);

 cout << c << "離開瞭聊天室!";

 return 0;
}

啟動聊天室服務端:

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的環境
 WSADATA  wd;
 if (WSAStartup(MAKEWORD(2, 2), &wd) != 0)
 {
  cout << "WSAStartup  error:" << GetLastError() << endl;
  return 0;
 }

 //1.創建流式套接字
 SOCKET  s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 if (s == INVALID_SOCKET)
 {
  cout << "socket  error:" << GetLastError() << endl;
  return 0;
 }

 //2.鏈接服務器
 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接收服務端的消息
 char buf[100] = { 0 };
 recv(s, buf, 100, 0);
 cout <<  buf << endl;

 //3隨時給服務端發消息
 int  ret = 0;
 do
 {
  char buf[100] = {0};
  cout << "請輸入聊天內容:";
  cin >> buf; 
  ret = send(s, buf, 100, 0); 
 } while (ret != SOCKET_ERROR&& ret != 0);


 //4.關閉監聽套接字
 closesocket(s);

 //清理winsock2的環境
 WSACleanup();


 return 0;
}

啟動2個客戶端

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: