控制台发消息,client–server–client
client程序代码
//
#include "stdafx.h"
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <WinSock2.h>
#include <process.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#pragma comment(lib,"ws2_32.lib")
#define RECV_OVER 1
#define RECV_YET 0
char userName[16] = { 0 };
int iStatus = RECV_YET;
//接受数据
unsigned __stdcall ThreadRecv(void* param)
{
char buf[128] = { 0 };
while (1)
{
int ret = recv(*(SOCKET*)param, buf, sizeof(buf), 0);
if (ret == SOCKET_ERROR)
{
Sleep(500);
continue;
}
if (strlen(buf) != 0)
{
printf("%s\n", buf);
iStatus = RECV_OVER;
}
else
Sleep(100);
}
return 0;
}
//发送数据
unsigned __stdcall ThreadSend(void* param)
{
char buf[128] = { 0 };
int ret = 0;
while (1)
{
int c = getch();
if (c == 72 || c == 0 || c == 68)//为了显示美观,加一个无回显的读取字符函数
continue; //getch返回值我是经过实验得出如果是返回这几个值,则getch就会自动跳过,具体我也不懂。
printf("%s: ", userName);
gets_s(buf);
ret = send(*(SOCKET*)param, buf, sizeof(buf), 0);
if (ret == SOCKET_ERROR)
return 1;
}
return 0;
}
//连接服务器
int ConnectServer()
{
WSADATA wsaData = { 0 };//存放套接字信息
SOCKET ClientSocket = INVALID_SOCKET;//客户端套接字
SOCKADDR_IN ServerAddr = { 0 };//服务端地址
USHORT uPort ;//服务端端口
//初始化套接字
if (WSAStartup(MAKEWORD(2, 2), &wsaData))
{
printf("WSAStartup failed with error code: %d\n", WSAGetLastError());
return -1;
}
//判断套接字版本
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
printf("wVersion was not 2.2\n");
return -1;
}
//创建套接字
ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ClientSocket == INVALID_SOCKET)
{
printf("socket failed with error code: %d\n", WSAGetLastError());
return -1;
}
//输入服务器IP
printf("说明:\n");
printf("1.服务器IP地址是打开《server.exe》的设备的ipv4.\n");
printf("2.用的时候电脑可能比较卡顿(找不到原因).\n");
printf("3.用Visual Studio 2015 运行的,不知道会不会存在链接库版本问题.\n");
printf("\n");
printf("请输入服务器的IP地址:");
char IP[32] = { 0 };
gets_s(IP);
printf("请输入服务端口(请确保端口没被占用!):");
scanf("%d", &uPort);
//printf("%d", uPort);
getchar();
//设置服务器地址
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(uPort);//服务器端口
ServerAddr.sin_addr.S_un.S_addr = inet_addr(IP);//服务器地址
printf("connecting......\n");
//连接服务器
if (SOCKET_ERROR == connect(ClientSocket, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr)))
{
printf("connect failed with error code: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return -1;
}
printf("Connecting server successfully IP:%s Port:%d\n",
IP, htons(ServerAddr.sin_port));
printf("Please input your UserName: ");
gets_s(userName);
send(ClientSocket, userName, sizeof(userName), 0);
printf("\n\n");
_beginthreadex(NULL, 0, ThreadRecv, &ClientSocket, 0, NULL); //启动接收和发送消息线程
_beginthreadex(NULL, 0, ThreadSend, &ClientSocket, 0, NULL);
for (int k = 0; k < 1000; k++)
Sleep(10000000);
closesocket(ClientSocket);
WSACleanup();
return 0;
}
int main()
{
ConnectServer(); //连接服务器
return 0;
}
server程序代码
//
#include "stdafx.h"
#include <WinSock2.h>
#include <process.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib,"ws2_32.lib")
#define SEND_OVER 1 //已经转发消息
#define SEND_YET 0 //还没转发消息
int g_iStatus = SEND_YET;
SOCKET g_ServerSocket = INVALID_SOCKET; //服务端套接字
SOCKADDR_IN g_ClientAddr = { 0 }; //客户端地址
int g_iClientAddrLen = sizeof(g_ClientAddr);
bool g_bCheckConnect = false; //检查连接情况
HANDLE g_hRecv1 = NULL;
HANDLE g_hRecv2 = NULL;
//客户端信息结构体
typedef struct _Client
{
SOCKET sClient; //客户端套接字
char buf[128]; //数据缓冲区
char userName[16]; //客户端用户名
char IP[20]; //客户端IP
UINT_PTR flag; //标记客户端,用来区分不同的客户端
}Client;
Client g_Client[2] = { 0 }; //创建一个客户端结构体
//发送数据线程
unsigned __stdcall ThreadSend(void* param)
{
int ret = 0;
int flag = *(int*)param;
SOCKET client = INVALID_SOCKET; //创建一个临时套接字来存放要转发的客户端套接字
char temp[128] = { 0 }; //创建一个临时的数据缓冲区,用来存放接收到的数据
memcpy(temp, g_Client[!flag].buf, sizeof(temp));
sprintf(g_Client[flag].buf, "%s: %s", g_Client[!flag].userName, temp);//添加一个用户名头
if (strlen(temp) != 0 && g_iStatus == SEND_YET) //如果数据不为空且还没转发则转发
ret = send(g_Client[flag].sClient, g_Client[flag].buf, sizeof(g_Client[flag].buf), 0);
if (ret == SOCKET_ERROR)
return 1;
g_iStatus = SEND_OVER; //转发成功后设置状态为已转发
return 0;
}
//接受数据
unsigned __stdcall ThreadRecv(void* param)
{
SOCKET client = INVALID_SOCKET;
int flag = 0;
if (*(int*)param == g_Client[0].flag) //判断是哪个客户端发来的消息
{
client = g_Client[0].sClient;
flag = 0;
}
else if (*(int*)param == g_Client[1].flag)
{
client = g_Client[1].sClient;
flag = 1;
}
char temp[128] = { 0 }; //临时数据缓冲区
while (1)
{
memset(temp, 0, sizeof(temp));
int ret = recv(client, temp, sizeof(temp), 0); //接收数据
if (ret == SOCKET_ERROR)
continue;
g_iStatus = SEND_YET; //设置转发状态为未转发
flag = client == g_Client[0].sClient ? 1 : 0; //这个要设置,否则会出现自己给自己发消息的BUG
memcpy(g_Client[!flag].buf, temp, sizeof(g_Client[!flag].buf));
_beginthreadex(NULL, 0, ThreadSend, &flag, 0, NULL); //开启一个转发线程,flag标记着要转发给哪个客户端
//这里也可能是导致CPU使用率上升的原因。
}
return 0;
}
//管理连接
unsigned __stdcall ThreadManager(void* param)
{
while (1)
{
if (send(g_Client[0].sClient, "", sizeof(""), 0) == SOCKET_ERROR)
{
if (g_Client[0].sClient != 0)
{
CloseHandle(g_hRecv1); //这里关闭了线程句柄,但是测试结果断开连C/S接后CPU仍然疯涨
CloseHandle(g_hRecv2);
printf("Disconnect from IP: %s,UserName: %s\n", g_Client[0].IP, g_Client[0].userName);
closesocket(g_Client[0].sClient); //这里简单的判断:若发送消息失败,则认为连接中断(其原因有多种),关闭该套接字
g_Client[0] = { 0 };
}
}
if (send(g_Client[1].sClient, "", sizeof(""), 0) == SOCKET_ERROR)
{
if (g_Client[1].sClient != 0)
{
CloseHandle(g_hRecv1);
CloseHandle(g_hRecv2);
printf("Disconnect from IP: %s,UserName: %s\n", g_Client[1].IP, g_Client[1].userName);
closesocket(g_Client[1].sClient);
g_Client[1] = { 0 };
}
}
Sleep(2000); //2s检查一次
}
return 0;
}
//接受请求
unsigned __stdcall ThreadAccept(void* param)
{
int i = 0;
int temp1 = 0, temp2 = 0;
_beginthreadex(NULL, 0, ThreadManager, NULL, 0, NULL);
while (1)
{
while (i < 2)
{
if (g_Client[i].flag != 0)
{
++i;
continue;
}
//如果有客户端申请连接就接受连接
if ((g_Client[i].sClient = accept(g_ServerSocket, (SOCKADDR*)&g_ClientAddr, &g_iClientAddrLen)) == INVALID_SOCKET)
{
printf("accept failed with error code: %d\n", WSAGetLastError());
closesocket(g_ServerSocket);
WSACleanup();
return -1;
}
recv(g_Client[i].sClient, g_Client[i].userName, sizeof(g_Client[i].userName), 0); //接收用户名
printf("Successfuuly got a connection from IP:%s ,UerName: %s\n",
inet_ntoa(g_ClientAddr.sin_addr), g_Client[i].userName);
memcpy(g_Client[i].IP, inet_ntoa(g_ClientAddr.sin_addr), sizeof(g_Client[i].IP)); //记录客户端IP
g_Client[i].flag = g_Client[i].sClient; //不同的socke有不同UINT_PTR类型的数字来标识
i++;
}
i = 0;
if (g_Client[0].flag != 0 && g_Client[1].flag != 0) //当两个用户都连接上服务器后才进行消息转发
{
if (g_Client[0].flag != temp1) //每次断开一个连接后再次连上会新开一个线程,导致cpu使用率上升,所以要关掉旧的
{
if (g_hRecv1) //这里关闭了线程句柄,但是测试结果断开连C/S接后CPU仍然疯涨
CloseHandle(g_hRecv1);
g_hRecv1 = (HANDLE)_beginthreadex(NULL, 0, ThreadRecv, &g_Client[0].flag, 0, NULL); //开启2个接收消息的线程
}
if (g_Client[1].flag != temp2)
{
if (g_hRecv2)
CloseHandle(g_hRecv2);
g_hRecv2 = (HANDLE)_beginthreadex(NULL, 0, ThreadRecv, &g_Client[1].flag, 0, NULL);
}
}
temp1 = g_Client[0].flag; //防止ThreadRecv线程多次开启
temp2 = g_Client[1].flag;
Sleep(2000);
}
return 0;
}
//启动服务器
int StartServer()
{
//存放套接字信息的结构
WSADATA wsaData = { 0 };
SOCKADDR_IN ServerAddr = { 0 }; //服务端地址
USHORT uPort; //服务器监听端口
//printf("%d",uPort); //初始化套接字
if (WSAStartup(MAKEWORD(2, 2), &wsaData))
{
printf("WSAStartup failed with error code: %d\n", WSAGetLastError());
return -1;
}
//判断版本
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
printf("wVersion was not 2.2\n");
return -1;
}
//创建套接字
g_ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (g_ServerSocket == INVALID_SOCKET)
{
printf("socket failed with error code: %d\n", WSAGetLastError());
return -1;
}
printf("请输入服务端口(请确保端口没被占用!):");
scanf("%d", &uPort);
printf("输入的端口为:%d", uPort);
printf("\n");
printf("请打开client!\n");
printf("以下为使用client端的信息:\n");
//设置服务器地址
ServerAddr.sin_family = AF_INET;//连接方式
ServerAddr.sin_port = htons(uPort);//服务器监听端口
ServerAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);//任何客户端都能连接这个服务器
//绑定服务器
if (SOCKET_ERROR == bind(g_ServerSocket, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr)))
{
printf("bind failed with error code: %d\n", WSAGetLastError());
closesocket(g_ServerSocket);
return -1;
}
//设置监听客户端连接数
if (SOCKET_ERROR == listen(g_ServerSocket, 20000))
{
printf("listen failed with error code: %d\n", WSAGetLastError());
closesocket(g_ServerSocket);
WSACleanup();
return -1;
}
_beginthreadex(NULL, 0, ThreadAccept, NULL, 0, 0);
for (int k = 0; k < 100; k++) //让主线程休眠,不让它关闭TCP连接.
Sleep(10000000);
//关闭套接字
for (int j = 0; j < 2; j++)
{
if (g_Client[j].sClient != INVALID_SOCKET)
closesocket(g_Client[j].sClient);
}
closesocket(g_ServerSocket);
WSACleanup();
return 0;
}
int main()
{
StartServer(); //启动服务器
return 0;
}
*
说明:
1.比较卡顿;
2.假如client和server两个程序填写的端口不一致,程序的端口会自己获取一个端口;
3.有bug可能导致即使端口一致,程序也会自动获取端口;