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;
}

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

推薦閱讀: