C++实现聊天小程序
C++写一个游戏聊天服务器,供大家参考,具体内容如下
最近学习网络编程写了个聊天程序" title="小程序">小程序,写个博客记录下自己的代码
涉及的技术:
- 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++实现聊天小程序 的全部内容, 来源链接: utcz.com/p/246357.html