本文小编为大家详细介绍“如何用C++实现聊天小程序”,内容详细,步骤清晰,细节处理妥当,希望这篇“如何用C++实现聊天小程序”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
以一个结构体的形式存储客户端,用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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。