Server01
#define WIN32_LEAN_AND_MEAN
#include<windows.h>
#include<WinSock2.h>
#include<iostream>
#pragma comment(lib,"ws2_32.lib")//动态链接库
//可以在属性的链接库中添加
enum CMD{
CMD_LOGIN,
CMD_LOGIN_RESULT,
CMD_LOGOUT,
CMD_LOGOUT_RESULT,
CMD_ERROR
};
struct DataHeader{
short cmd;
short dataLength;
};
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 = 1;
}
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;
result = 1;
}
int result;
};
int main1_4(){
//启动Windows Socket程序
WORD ver = MAKEWORD(2, 2);
WSADATA dat;
WSAStartup(ver, &dat);
//1、建立socket
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// 2、绑定网络端口
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 = inet_addr("127.0.0.1");
if (SOCKET_ERROR == bind(sock, (sockaddr*)&_sin, sizeof(sockaddr_in)))
printf("绑定网络端口失败。。。");
//3、listen 监听端口
if (SOCKET_ERROR == listen(sock, 5))
printf("监听网络端口失败。。。");
//4、accept 等待客户端连接
sockaddr_in clientAddr = {};
int nAddrlen = sizeof(sockaddr_in);
SOCKET _cSocket = INVALID_SOCKET;
char msgBuf[] = "hello, I am server...";
_cSocket = accept(sock, (sockaddr*)&clientAddr, &nAddrlen);
if (INVALID_SOCKET == _cSocket){
printf("错误,接收到无效客户端。。。");
}
printf("新客户端加入:socket=%d,IP=%s\n", (int)_cSocket, inet_ntoa(clientAddr.sin_addr));
while (true){
char szRecv[1024] = {};
//5、接收客户端请求数据
int nLen = recv(_cSocket, szRecv, sizeof(DataHeader), 0);
DataHeader* header = (DataHeader*)szRecv;
if (nLen <= 0){
printf("客户端已退出。。。");
break;
}
printf("收到客户端的信息为:%d ,数据长度为%d\n", header->cmd, header->dataLength);
//6、处理请求
switch (header->cmd){
case CMD_LOGIN: {
recv(_cSocket, 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);
printf("姓名为:%s上线了。。。\n", login->username);
LoginResult ret;
send(_cSocket, (char*)&ret, sizeof(LoginResult), 0);
break;
}
case CMD_LOGOUT:{
recv(_cSocket, szRecv + sizeof(DataHeader), header->dataLength-sizeof(DataHeader), 0);
Logout* logout = (Logout*)szRecv;
printf("收到命令:CMD_LOGOUT,数据长度:%d username:%s \n", logout->dataLength, logout->userName);
printf("姓名为:%s 已下线。。。\n", logout->userName);
LogoutResult ret;
send(_cSocket, (char*)&ret, sizeof(LogoutResult), 0);
break;
}
default:
{ DataHeader header = {CMD_ERROR,0};
send(_cSocket, (char*)&header, sizeof(header), 0);
}
break;
}
printf("发送成功\n");
}
// 6、关闭套接字
closesocket(sock);
WSACleanup();
printf("已退出,任务结束");
system("pause");
return 0;
}
client01
#define WIN32_LEAN_AND_MEAN
#define _CRT_SECURE_NO_WARNINGS
#include<windows.h>
#include<WinSock2.h>
#include<iostream>
#include<stdio.h>
using namespace std;
#pragma comment(lib,"ws2_32.lib")//动态链接库
//可以在属性的链接库中添加
enum CMD{
CMD_LOGIN,
CMD_LOGIN_RESULT,
CMD_LOGOUT,
CMD_LOGOUT_RESULT,
CMD_ERROR
};
struct DataHeader{
short cmd;
short dataLength;
};
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 = 1;
}
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;
result = 1;
}
int result;
};
int main1_4(){
//启动Windows Socket程序
WORD ver = MAKEWORD(2, 2);
WSADATA dat;
WSAStartup(ver, &dat);
//1、建立socket
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// 2、连接服务器
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 = inet_addr("127.0.0.1");
int ret = connect(sock, (sockaddr*)&_sin, sizeof(sockaddr_in));
if (SOCKET_ERROR == ret)
printf("绑定网络端口失败。。。");
while (true){
//3、输入请求命令
char cmdBuf[128] = {};
cout << "发送数据给服务器:" << endl;
scanf("%s", cmdBuf);
// 4、处理请求命令
if (0 == strcmp(cmdBuf, "exit")){
printf("收到exit,任务结束");
break;
}
else if (0 == strcmp(cmdBuf, "login"))
{
//5、向服务器发送请求命令
Login login;
strcpy(login.username, "lyd");
strcpy(login.password, "123");
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")){
//5、向服务器发送请求命令
Logout logout;
strcpy(logout.userName, "lyd");
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");
}
}
// 7、关闭套接字
closesocket(sock);
WSACleanup();
system("pause");
return 0;
}