发送结构化消息 网络数据报文的格式定义: 报文有两部分:包头和包体,是网络消息的基本单元 包头:描述本次消息的大小,描述数据的作用 包体:数据
server
#include "stdafx.h"
#include<WinSock2.h>
#include<Windows.h>
enum CMD
{
CMD_LOGIN,
CMD_LOGIN_RESULT,
CMD_LOGOUT,
CMD_LOGOUTRESULT,
CMD_ERROR
};
struct DataHeader
{
short dataLength;
short cmd;
};
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;
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_LOGOUTRESULT;
result = 0;
}
int result;
};
int main()
{
WORD ver = MAKEWORD(2, 2);
WSADATA dat;
WSAStartup(ver, &dat);
SOCKET _sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sockaddr_in _sin = {};
_sin.sin_family = AF_INET;
_sin.sin_port = htons(4567);
_sin.sin_addr.S_un.S_addr = INADDR_ANY;
if (SOCKET_ERROR == bind(_sock, (sockaddr*)&_sin, sizeof(_sin)))
{
printf("错误,绑定失败\n");
}
else
{
printf("绑定成功\n");
}
if (SOCKET_ERROR == listen(_sock, 5))
{
printf("错误,监听失败\n");
}
else
{
printf("监听成功\n");
}
sockaddr_in clientAddr = {};
int nDddrLen = sizeof(sockaddr_in);
SOCKET _cSock = INVALID_SOCKET;
_cSock = accept(_sock, (sockaddr*)&clientAddr, &nDddrLen);
if (_cSock == INVALID_SOCKET)
{
printf("错误,接受到无效客户端socke..\n");
}
printf("新客户端加入:socket = %d, IP = %s\n", _cSock, inet_ntoa(clientAddr.sin_addr));
while (true)
{
char szRecv[1024] = {};
int nLen = recv(_cSock, szRecv, sizeof(DataHeader), 0);
DataHeader *header = (DataHeader*)szRecv;
if (nLen <= 0)
{
printf("客户端已退出,任务结束\n");
break;
}
switch (header->cmd)
{
case CMD_LOGIN:
{
recv(_cSock, szRecv +sizeof(DataHeader), header->dataLength - sizeof(DataHeader), 0);
Login *login = (Login*)szRecv;
printf("收到命令:CMD_LOGIN 数据长度:%d ,userName:%s , passWord = %s\n", login->dataLength,login->userName,login->passWord);
LoginResult ret;
send(_cSock, (char*)&ret, sizeof(LoginResult), 0);
}
break;
case CMD_LOGOUT:
{
recv(_cSock, szRecv + sizeof(DataHeader), header->dataLength - sizeof(DataHeader), 0);
Logout *logout= (Logout*)szRecv;
printf("收到命令:CMD_LOGIN 数据长度:%d ,userName:%s\n", logout->dataLength, logout->userName);
LogoutResult ret;
send(_cSock, (char*)&ret, sizeof(LogoutResult), 0);
}
break;
default:
header->cmd = CMD_ERROR;
header->dataLength = 0;
send(_cSock, (char*)&header, sizeof(DataHeader), 0);
break;
}
}
closesocket(_cSock);
WSACleanup();
printf("已退出,任务结束\n");
getchar();
return 0;
}
client
#include "stdafx.h"
#include<WinSock2.h>
#include<Windows.h>
#include<iostream>
using namespace std;
enum CMD
{
CMD_LOGIN,
CMD_LOGIN_RESULT,
CMD_LOGOUT,
CMD_LOGOUTRESULT,
CMD_ERROR
};
struct DataHeader
{
short dataLength;
short cmd;
};
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;
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_LOGOUTRESULT;
result = 0;
}
int result;
};
int main()
{
WORD ver = MAKEWORD(2, 2);
WSADATA dat;
WSAStartup(ver, &dat);
SOCKET _sock = socket(AF_INET, SOCK_STREAM, 0);
if (INVALID_SOCKET == _sock)
{
printf("错误,建立client socket失败。。\n");
}
else
{
printf("建立client socket成功\n");
}
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("错误,连接socket失败。。\n");
}
else
{
printf("连接socket成功\n");
}
while (true)
{
char cmdBuf[256] = {};
scanf("%s", cmdBuf);
if (0 == strcmp(cmdBuf, "exit"))
{
printf("收到退出命令,任务结束\n");
break;
}
else if(0 == strcmp(cmdBuf, "login"))
{
Login login;
strcpy(login.userName,"gk1314");
strcpy(login.passWord,"10203453");
send(_sock, (const char*)&login, sizeof(login), 0);
LoginResult loginRet = {};
recv(_sock, (char*)&loginRet, sizeof(loginRet), 0);
printf("LoginResult = %d\n", loginRet.result);
}
else if (0 == strcmp(cmdBuf, "logout"))
{
Logout logout;
strcpy(logout.userName, "gk1314");
send(_sock, (const char*)&logout, sizeof(logout), 0);
LogoutResult logoutRet = {};
recv(_sock, (char*)&logoutRet, sizeof(logoutRet), 0);
printf("LoginResult = %d\n", logoutRet.result);
}
else
{
printf("收到不支持命令,重新输入\n");
}
}
closesocket(_sock);
WSACleanup();
printf("退出命令,任务结束\n");
getchar();
return 0;
}
|