上次我们的代码使用了结构化的网络消息,但是只能发送指定的数据格式的数据,无法完成交互。因此,我们需要定义网络报文的数据格式。
网络数据报文的格式定义:
server.cpp:
#define WIN32_LEAN_AND_MEAN
#include
#include
#include
#pragma comment(lib,"ws2_32.lib")
enum CMD
{
CMD_LOGIN,
CMD_LOGOUT,
CMD_ERROR
};
struct DataHeader
{
short dataLength;
short cmd;
};
//DataPackage
struct Login
{
char userName[32];
char PassWord[32];
};
struct LoginResult
{
int result;
};
struct Logout
{
char userName[32];
};
struct LogoutResult
{
int result;
};
int main()
{
//启动Windows socket 2.x环境
WORD ver = MAKEWORD(2, 2);
WSADATA dat;
WSAStartup(ver, &dat);
//------------
//-- 用Socket API建立简易TCP客户端
// 1 建立一个socket
SOCKET _sock = socket(AF_INET, SOCK_STREAM, 0);
if (INVALID_SOCKET == _sock)
{
printf("错误,建立Socket失败...\n");
}
else {
printf("建立Socket成功...\n");
}
// 2 连接服务器 connect
sockaddr_in _sin = {};
_sin.sin_family = AF_INET;
_sin.sin_port = htons(4567);
_sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
int ret = connect(_sock, (sockaddr*)&_sin, sizeof(sockaddr_in));
if (SOCKET_ERROR == ret)
{
printf("错误,连接服务器失败...\n");
}
else {
printf("连接服务器成功...\n");
}
while (true)
{
//3输入请求命令
char cmdBuf[128] = {};
scanf("%s",cmdBuf);
//4 处理请求命令
if (0 == strcmp(cmdBuf, "exit"))
{
printf("收到exit命令,任务结束。\n");
break;
}else if (0 == strcmp(cmdBuf, "login")) {
Login login = {"lyd","lydmm"};
DataHeader dh = { sizeof(login),CMD_LOGIN};
//5 向服务器发送请求命令
send(_sock, (const char *)&dh, sizeof(dh), 0);
send(_sock, (const char *)&login, sizeof(login), 0);
// 接收服务器返回的数据
DataHeader retHeader = {};
LoginResult loginRet = {};
recv(_sock, (char*)&retHeader, sizeof(retHeader), 0);
recv(_sock, (char*)&loginRet, sizeof(loginRet), 0);
printf("LoginResult: %d \n", loginRet.result);
}
else if (0 == strcmp(cmdBuf, "logout")) {
Logout logout = { "lyd" };
DataHeader dh = { sizeof(logout), CMD_LOGOUT };
//5 向服务器发送请求命令
send(_sock, (const char *)&dh, sizeof(dh), 0);
send(_sock, (const char *)&logout, sizeof(logout), 0);
// 接收服务器返回的数据
DataHeader retHeader = {};
LogoutResult logoutRet = {};
recv(_sock, (char*)&retHeader, sizeof(retHeader), 0);
recv(_sock, (char*)&logoutRet, sizeof(logoutRet), 0);
printf("LogoutResult: %d \n", logoutRet.result);
}
else {
printf("不支持的命令,请重新输入。\n");
}
}
// 7 关闭套节字closesocket
closesocket(_sock);
//清除Windows socket环境
WSACleanup();
printf("已退出。");
getchar();
return 0;
}
client.cpp:
#define WIN32_LEAN_AND_MEAN
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include
#include
#include
#pragma comment(lib,"ws2_32.lib")
enum CMD
{
CMD_LOGIN,
CMD_LOGOUT,
CMD_ERROR
};
struct DataHeader
{
short dataLength;
short cmd;
};
//DataPackage
struct Login
{
char userName[32];
char PassWord[32];
};
struct LoginResult
{
int result;
};
struct Logout
{
char userName[32];
};
struct LogoutResult
{
int result;
};
int main()
{
//启动Windows socket 2.x环境
WORD ver = MAKEWORD(2, 2);
WSADATA dat;
WSAStartup(ver, &dat);
//------------
//-- 用Socket API建立简易TCP服务端
// 1 建立一个socket 套接字
SOCKET _sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// 2 bind 绑定用于接受客户端连接的网络端口
sockaddr_in _sin = {};
_sin.sin_family = AF_INET;
_sin.sin_port = htons(4567);//host to net unsigned short
_sin.sin_addr.S_un.S_addr = INADDR_ANY;//inet_addr("127.0.0.1");
if (SOCKET_ERROR == bind(_sock, (sockaddr*)&_sin, sizeof(_sin)))
{
printf("错误,绑定网络端口失败...\n");
}
else {
printf("绑定网络端口成功...\n");
}
// 3 listen 监听网络端口
if (SOCKET_ERROR == listen(_sock, 5))
{
printf("错误,监听网络端口失败...\n");
}
else {
printf("监听网络端口成功...\n");
}
// 4 accept 等待接受客户端连接
sockaddr_in clientAddr = {};
int nAddrLen = sizeof(sockaddr_in);
SOCKET _cSock = INVALID_SOCKET;
_cSock = accept(_sock, (sockaddr*)&clientAddr, &nAddrLen);
if (INVALID_SOCKET == _cSock)
{
printf("错误,接受到无效客户端SOCKET...\n");
}
printf("新客户端加入:socket = %d,IP = %s \n", (int)_cSock, inet_ntoa(clientAddr.sin_addr));
while (true)
{
DataHeader header = {};
// 5 接收客户端数据
int nLen = recv(_cSock, (char*)&header, sizeof(DataHeader), 0);
if (nLen <= 0)
{
printf("客户端已退出,任务结束。");
break;
}
printf("收到命令:%d 数据长度:%d\n", header.cmd, header.dataLength);
switch (header.cmd)
{
case CMD_LOGIN:
{
Login login = {};
recv(_cSock, (char*)&login, sizeof(Login), 0);
//忽略判断用户密码是否正确的过程
LoginResult ret = {1};
send(_cSock, (char*)&header, sizeof(DataHeader), 0);
send(_cSock, (char*)&ret, sizeof(LoginResult), 0);
}
break;
case CMD_LOGOUT:
{
Logout logout = {};
recv(_cSock, (char*)&logout, sizeof(logout), 0);
//忽略判断用户密码是否正确的过程
LogoutResult ret = { 1 };
send(_cSock, (char*)&header, sizeof(header), 0);
send(_cSock, (char*)&ret, sizeof(ret), 0);
}
break;
default:
header.cmd = CMD_ERROR;
header.dataLength = 0;
send(_cSock, (char*)&header, sizeof(header), 0);
break;
}
}
// 8 关闭套节字closesocket
closesocket(_sock);
//------------
//清除Windows socket环境
WSACleanup();
printf("已退出。");
getchar();
return 0;
}
参考资料: