服务端
// TcpCom.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "pch.h"
#include
#include //winsock2必须放在windows上面,因为早期的window已经实现了winsock,会引起冲突
#include
#include
using namespace std;
#pragma comment(lib,"ws2_32")//明确指出要用到的库
enum CMD {
CMD_LOGIN,
CMD_LOGOUT,
CMD_ERROR
};
struct Datahead {
short len;
short cmd;
};
struct Login {
char user_name[32];
char user_pwd[32];
};
struct Log_result {
int result;
};
struct Logout {
char user_name[32];
};
static void log_in(SOCKET socket) {
Login login = {};
Log_result ret = {};
int send_info;
recv(socket, (char*)&login, sizeof(login), 0);
if (strcmp(login.user_pwd, "123") == 0) {
ret.result = 1;
send_info=send(socket, (char*)&ret, sizeof(ret), 0);
}
if (send_info < 0)
cout << "Send fail" << endl;
else
cout << "Send success" << endl;
}
void log_out(SOCKET socket) {
Logout logout = {};
Log_result ret = {2};
recv(socket, (char*)&logout, sizeof(logout), 0);
int send_info=send(socket, (char*)&ret, sizeof(ret), 0);
if (send_info < 0)
cout << "Send fail" << endl;
else
cout << "Send success" << endl;
}
int main()
{
WORD ver = MAKEWORD(2, 2);//版本号
WSADATA data;//wsa数据指针
WSAStartup(ver, &data);
//建立套接字
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//AF_INET为网络版本号,SOCK_STREAM 网络为什么的类型,什么样一种协议的网络
//绑定网络端口
sockaddr_in _sin = {};
_sin.sin_family = AF_INET;//定义绑定地址的类型为ipv4
_sin.sin_port = htons(4567);//主机to网络
_sin.sin_addr.S_un.S_addr = INADDR_ANY;//主机任意ip都可以访问的
if (SOCKET_ERROR == bind(sock, (sockaddr*)&_sin, sizeof(_sin)))
cout << "Bind fail" << endl;
else
cout << "Bind success" << endl;
//监听端口listen
if (SOCKET_ERROR == listen(sock, 5))//最多5人连接
cout << "Listen fail" << endl;
else
cout << "Listen successful" << endl;
//等待接收accept
sockaddr_in new_client = {};
SOCKET csock = INVALID_SOCKET;
int claddr = sizeof(sockaddr_in);
char msgbuf[] = "hello i am server";
char err_buf[] = "???";
char str[100];
csock = accept(sock, (sockaddr*)&new_client, &claddr);//阻塞直到有客户端连接
inet_ntop(AF_INET, &new_client.sin_addr, str, sizeof(str));
if (csock == INVALID_SOCKET) cout << "接收到无效客户端sock" << endl;
else
cout << "new client "<<"socket:"<<csock<<" IP:" << str <<" join"<< endl;
while (true) {
int send_info = 0;
int test_client = 0;
Datahead header = {};
test_client=recv(csock,(char*) &header, sizeof(header), 0);
if (test_client < 0) {
cout << "client is drop out" << endl;
break;
}
switch (header.cmd) {
case CMD_LOGIN:
log_in(csock);
break;
case CMD_LOGOUT:
log_out(csock);
break;
default:
header.cmd = CMD_ERROR;
send(csock, (char*)&header, sizeof(header), 0);
break;
}
}
//关闭自身
closesocket(sock);
WSACleanup();
system("pause");
return 0;
}
客户端
// Client.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include"pch.h"
#include
#include //winsock2必须放在windows上面,因为早期的window已经实现了winsock,会引起冲突
#include
#include
using namespace std;
#pragma comment(lib,"ws2_32")//明确指出要用到的库
enum CMD {
CMD_LOGIN,
CMD_LOGOUT,
CMD_ERROR
};
struct Datahead {
short len;
short cmd;
};
struct Login {
char user_name[32];
char user_pwd[32];
};
struct Log_result {
int result;
};
struct Logout {
char user_name[32];
};
int main()
{
WORD ver = MAKEWORD(2, 2);//版本号
WSADATA data;//wsa数据指针
WSAStartup(ver, &data);
SOCKET c_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
//网络版本,面向数据流,网络协议tcp
sockaddr_in _sin = {};
_sin.sin_family = AF_INET;
_sin.sin_port = htons(4567);
inet_pton(AF_INET, "127.0.0.1", &_sin.sin_addr.S_un.S_addr);
//将127.0.0.1转换为_sin.sin_addr.S_un.S_addr,成功返回1
int ret = connect(c_sock, (sockaddr*)&_sin, sizeof(sockaddr_in));
if (SOCKET_ERROR == ret)
cout << "Connect fail" << endl;
else
cout << "Connect Success" << endl;
char cmd_buf[128];
while (true) {
Datahead head = {};
Login login = {"zhou","123"};
Log_result log_re = {};
Logout logout = { "zhou" };
cout << "imput cmd:" << endl;
cin >> cmd_buf;
if (strcmp(cmd_buf, "login") == 0) {
head.cmd = CMD_LOGIN;
head.len = sizeof(login);
send(c_sock, (char*)&head, sizeof(head), 0);
send(c_sock, (char*)&login, sizeof(login), 0);
recv(c_sock, (char*)&log_re, sizeof(log_re), 0);
cout << "recive " << log_re.result << endl;
}
else if (strcmp(cmd_buf, "logout") == 0) {
head.cmd = CMD_LOGOUT;
head.len = sizeof(logout);
send(c_sock, (char*)&head, sizeof(head), 0);
send(c_sock, (char*)&logout, sizeof(logout), 0);
recv(c_sock, (char*)&log_re, sizeof(log_re), 0);
cout << "receive " << log_re.result << endl;
}
else
{
cout << "error cmd" << endl;
}
}
//关闭
closesocket(c_sock);
WSACleanup();
system("pause");
return 0;
}