Socket Server Client 1.1 结构化网络消息数据
Posted __悠然
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Socket Server Client 1.1 结构化网络消息数据相关的知识,希望对你有一定的参考价值。
//Server
struct DataPackage
{
int age;
char name[32];
};
int main()
{
WORD ver = MAKEWORD(2, 2);
WSADATA wsadata;
WSAStartup(ver, &wsadata);
//----------------socket 创建套接字
SOCKET Sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (INVALID_SOCKET == Sock)
{
printf("创建套接字 失败");
}
//----------------socket 绑定;
sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_port = htons(8888);
addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
if (SOCKET_ERROR == bind(Sock, (sockaddr *)&addr, sizeof(sockaddr)))
{
printf("Error , 绑定网络端口失败... \\r\\n");
}
else
{
printf("绑定网络端口成功... \\r\\n");
}
// 3 --------------- listen
if (SOCKET_ERROR == listen(Sock, 5))
{
printf("Error , 监听网络端口失败... \\r\\n");
}
else
{
printf("监听网络端口成功... \\r\\n");
}
// 4 ------------- 等待客户端连接 单用户 多用户要引用多线程 处理....
sockaddr_in ClientInfo = {};
int ClientSock = INVALID_SOCKET;
int listenLen = sizeof(sockaddr);
char rep[] = { "hello I\'m Server" };
ClientSock = accept(Sock, (sockaddr *)&ClientInfo, &listenLen);
if (INVALID_SOCKET == ClientSock)
{
printf(" 客户端连接的套接字有误");
}
else
{
printf("ClientSock = %d , ClinetIp = %s ,ClientPort = %d", ClientSock, inet_ntoa(ClientInfo.sin_addr), ntohs(ClientInfo.sin_port));
//send(ClientSock, rep, strlen(rep) + 1, 0);
}
char RecvBuf[1024] = { 0 };
char sendError[] = { "错误的请求" };
while (true)
{
//5 接收客户端的请求数据
int nlen = recv(ClientSock, RecvBuf, sizeof(RecvBuf), 0);
if (nlen <= 0)
{
printf("客户端已退出,任务结束");
break;
}
printf("收到的命令 : %s", RecvBuf);
//6 处理请求;
if (0 == strcmp(RecvBuf,"GetInfo"))
{
// 7 向客户端返回请求数据;
//利用结构一次性 完成 年龄 与 名称的返回数据;
DataPackage dp = { 80, "Xiao Qiang" };
send(ClientSock, (const char*)&dp, sizeof(DataPackage), 0);
}
else
{
// 7 向客户端返回请求数据;
char MsgBuf[] = "????...";
send(ClientSock, MsgBuf, strlen(MsgBuf) + 1, 0);
}
}
// 8 关闭Socket //套接字 ;
closesocket(Sock);
WSACleanup();
return 0;
}
//Client
struct DataPackage
{
int age;
char name[32];
};
int main()
{
WORD ver = MAKEWORD(2, 2);
WSADATA wsadata = {};
WSAStartup(ver, &wsadata);
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (INVALID_SOCKET == sock)
{
printf("无效的套接字.... \\r\\n");
}
sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_port = htons(8888);
addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
if (SOCKET_ERROR == connect(sock, (sockaddr *)&addr, sizeof(sockaddr)))
{
printf("错误 ,连接服务器失败.....\\r\\n");
return 0;
}
else
{
printf("连接服务器成功.....\\r\\n");
}
while (true)
{
//3 输入请求命令
char cmdBuf[128] = {};
scanf_s("%s",cmdBuf,sizeof(cmdBuf));
// 4 处理请求
if (0 == strcmp(cmdBuf,"exit"))
{
break;
}
else
{
//5 向服务端 发送请求命令
send(sock, cmdBuf, strlen(cmdBuf) + 1, 0);
}
//6 接收服务端信息 recv
char RecvBuf[128] = {};
int nLen = recv(sock, RecvBuf, sizeof(RecvBuf), 0);
if (nLen == sizeof(DataPackage))
{
//由于结构的原因 这里需要进行强制转换;
DataPackage *dp = (DataPackage *)RecvBuf;
printf("接收到的数据 Age:%d name :%s\\r\\n", dp->age, dp->name);
}
else
{
//数据长度 导致 有待优化;
printf("接收到的数据 : %s\\r\\n",RecvBuf);
}
}
//7 关闭套接字 ;
closesocket(sock);
//清除 windows Socket环境;
WSACleanup();
return 0;
}
以上是关于Socket Server Client 1.1 结构化网络消息数据的主要内容,如果未能解决你的问题,请参考以下文章
python,socket网络编程,最简单的server端和client端代码
python使用asyncore实现socket server client