1. 在服务器端加入,如果有新用户连接服务器端则向所有连接的客户端发送消息:
if (FD_ISSET(_sock, &fdRead)) {
FD_CLR(_sock, &fdRead);
//4. 等待客户端连接accept
sockaddr_in clientAddr = {};
socklen_t nAddrLen = sizeof(sockaddr_in);
int _cSock = accept(_sock, (sockaddr*)&clientAddr, &nAddrLen); //套接字,收到客户端socket地址,返回socket地址的大小
if (_cSock == -1) {
perror("client socket error!\n");
} else {
//当新客户端加入时群发给其他客户端
for (int n = (int)g_clients.size() - 1; n >= 0; --n) {
NewUserJoin userJoin;
send(g_clients[n], (const char*)&userJoin, sizeof(NewUserJoin), 0);
}
g_clients.push_back(_cSock);
printf("client socket success! socket = %d, IP = %s \n", _cSock, inet_ntoa(clientAddr.sin_addr)); //打印客户端socket和IP地址
}
}
2. 改造客户端,向其中加入select网络模型:
具体操作与服务器端类似,加入select网络模型,将客户端的消息处理封装到processor函数中。
//
// client.cpp
// SocketStepByStep
//
// Created by 刘君妍 on 2019/7/19.
// Copyright © 2019 tower. All rights reserved.
//
#include
#include
#include
#include
#include
#include
#include
#define SOCKET_ERROR -1
//三个命令:登入、登出和错误信息
enum CMD {
CMD_LOGIN,
CMD_LOGIN_RESULT,
CMD_LOGOUT,
CMD_LOGOUT_RESULT,
CMD_NEW_USER_JOIN,
CMD_ERROR
};
//包头
struct DataHeader {
short dataLength; //数据长度
short cmd; //命令
};
//包体 DataPackage
struct Login : public DataHeader
{
Login() {
dataLength = sizeof(Login);
cmd = CMD_LOGIN;
}
char UserName[32];
char PassWord[32];
};
struct LoginResult : public DataHeader
{
LoginResult() {
dataLength = sizeof(LoginResult);
cmd = CMD_LOGIN;
result = 0;
}
int result;
};
struct LogOut : public DataHeader
{
LogOut() {
dataLength = sizeof(LogOut);
cmd = CMD_LOGOUT;
}
char UserName[32];
};
struct LogoutResult: public DataHeader
{
LogoutResult() {
dataLength = sizeof(LogoutResult);
cmd = CMD_LOGOUT;
result = 0;
}
int result;
};
struct NewUserJoin: public DataHeader
{
NewUserJoin() {
dataLength = sizeof(NewUserJoin);
cmd = CMD_NEW_USER_JOIN;
sock = 0;
}
int sock;
};
//处理进程
int processor(int _sock) {
char szRecv[4096] = {}; //缓冲,用于接收数据
//5. 接收客户端的请求数据
int nLen = recv(_sock, szRecv, sizeof(DataHeader), 0);
DataHeader* header = (DataHeader*)szRecv;
if (nLen <= 0) {
printf("nLen <= 0, connection with server has quit!\n");
return -1;
}
//6. 处理请求
switch (header->cmd) {
//接收服务器返回的数据并存储在szRecv中
case CMD_LOGIN_RESULT:
{
recv(_sock, szRecv + sizeof(DataHeader), header->dataLength - sizeof(DataHeader), 0);
LoginResult* login = (LoginResult*)szRecv;
printf("receive fron server: CMD_LOGIN_RESULT, datalength: %d\n", login->dataLength);
}
break;
case CMD_LOGOUT_RESULT:
{
recv(_sock, szRecv + sizeof(DataHeader), header->dataLength - sizeof(DataHeader), 0);
LogoutResult* logout = (LogoutResult*)szRecv;
printf("receive fron server: CMD_LOGOUT_RESULT, datalength: %d\n", logout->dataLength);
}
break;
case CMD_NEW_USER_JOIN:
{
recv(_sock, szRecv + sizeof(DataHeader), header->dataLength - sizeof(DataHeader), 0);
NewUserJoin* userJoin = (NewUserJoin*)szRecv;
printf("receive fron server: NEW_USER_JOIN, datalength: %d\n", userJoin->dataLength);
}
break;
}
return 0;
}
int main() {
//1. 建立一个socket
int _sock = socket(AF_INET, SOCK_STREAM, 0); //与服务器端不同,第三个参数无需声明使用TCP连接
if (_sock == SOCKET_ERROR) {
printf("socket build error!\n");
} else {
printf("socket build success!\n");
}
//2. 连接服务器
sockaddr_in _sin = {};
_sin.sin_family = AF_INET;
_sin.sin_port = htons(4567);
_sin.sin_addr.s_addr = inet_addr("127.0.0.1");
int ret = connect(_sock, (sockaddr*)&_sin, sizeof(sockaddr_in));
if (ret == SOCKET_ERROR) {
printf("connect error!\n");
} else {
printf("connect success!\n");
}
while (true) {
fd_set fdReads; //用于在文件描述符集合中增加一个新的文件描述符。
FD_ZERO(&fdReads);
FD_SET(_sock, &fdReads);
timeval t = {1, 0};
int ret = select(_sock, &fdReads, 0, 0, &t);
if (ret < 0) {
printf("1select task over!\n");
break;
}
if(FD_ISSET(_sock, &fdReads)) { //用于测试指定的文件描述符是否在该集合中。
FD_CLR(_sock, &fdReads); //用于在文件描述符集合中删除一个文件描述符。
if(-1 == processor(_sock)) { //processor封装了客户端对服务器端传递而来的数据的处理
printf("2select task over!\n");
break;
}
}
//客户端主动向服务器端发送数据
printf("空闲时间处理其他业务……\n");
}
//7. 关闭套接字close socket
close(_sock);
printf("client has quit!");
getchar();
return 0;
}