基于UDP的简易通信程序

//客户端
#include
#include
#include
#include
#pragma comment(lib,"ws2_32.lib")
//BOOL InitWinsock();
void kaisa(char *a, char b, int c)//a为待操作数组,b为移动方向,c为移动位数
{
	switch (b)
	{
	case 'r':
		while (*a)
		{
			*a+=c;
 
			a++;
		}
		break;
	case 'l':
		while (*a)
		{
			*a -= c;
 
 
			a++;
		}
		break;
 
 
	}
 
 
}
void main()
{
	SOCKET socket1;
	HWND q;
	q = FindWindow(NULL, "QQ");
	SetWindowText(q, "XX");
	SetWindowPos(q, NULL, 0, 0, 250, 250, NULL);
	MEMORYSTATUS mem;
	char neicun[30];
	mem.dwLength = sizeof(mem);
	GlobalMemoryStatus(&mem);
	wsprintf(neicun, "Total memory:%dM", mem.dwTotalPhys / 1024 / 1024 + 1);
	char nameqz[30] = "computer name:";
	TCHAR name[1024];
	DWORD namelen = sizeof(name);
	GetComputerName(name, &namelen);
	strcat(nameqz, name);
	//InitWinsock();
	WSADATA wsa;
	WSAStartup(MAKEWORD(2, 2), &wsa);
	sockaddr_in server;
	int len = sizeof(server);
	time_t t;
	char tmp[32] = { 0 };
	t = time(0);
	strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S", localtime(&t));
	server.sin_family = AF_INET;
	server.sin_port = htons(1000);//用1000端口通信
	server.sin_addr.s_addr = inet_addr("10.168.142.61");//服务端IP
	socket1 = socket(AF_INET, SOCK_DGRAM, 0);//UDP
	char a[64] = "host active-";
	strcat(a, tmp);
	kaisa(a, 'r', 2);
	kaisa(nameqz, 'r', 2);
	kaisa(neicun, 'r', 2);
	sendto(socket1, a, sizeof(a), 0, (sockaddr*)&server, len);
	sendto(socket1, nameqz, sizeof(nameqz), 0, (sockaddr*)&server, len);
	sendto(socket1, neicun, sizeof(neicun), 0, (sockaddr*)&server, len);
	while (1)
	{
		char buffer[1024] = "\0";
		printf("input message\n");
		gets_s(buffer);
		if (strcmp(buffer, "bye") == 0)
		{
			break;
		}
		kaisa(buffer, 'r', 2);
		if (sendto(socket1, buffer, sizeof(buffer), 0, (sockaddr*)&server, len) != SOCKET_ERROR)
		{
			if (recvfrom(socket1, buffer, sizeof(buffer), 0, (sockaddr*)&server, &len) != SOCKET_ERROR)
			{
				printf("rece from server:%s\n", buffer);
			}
 
		}
 
	}
	closesocket(socket1);
 
 
}
/*BOOL InitWinsock()
{
int Error;
WORD VersionRequested;
WSADATA WsaData;
VersionRequested = MAKEWORD(2, 2);
Error = WSAStartup(VersionRequested, &WsaData);
if (Error != 0)
{
return FALSE;
}
else
{
if (LOBYTE(WsaData.wVersion) != 2 || HIBYTE(WsaData.wHighVersion) != 2)
{
WSACleanup();
return FALSE;
}
}
return TRUE;
}*/
//服务端
#include
#include
#pragma comment(lib,"ws2_32.lib")
BOOL InitWinsock();
using namespace std;
void kaisa(char *a, char b, int c)//a为待操作数组,b为移动方向,c为移动位数
{
	switch (b)
	{
	case 'r':
		while (*a)
		{
			*a += c;
 
 
			a++;
		}
		break;
	case 'l':
		while (*a)
		{
			*a -= c;
 
 
			a++;
		}
		break;
 
 
	}
 
 
}
void setcolor(unsigned short zt = 7, unsigned short bj = 0)
{
	HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(h, zt + bj * 0x10);
 
}
void main()
{
	SOCKET socket1;
	InitWinsock();
	sockaddr_in local, from;
	int fromlen = sizeof(from);
	local.sin_family = AF_INET;
	local.sin_port = htons(1000);//使用1000端口通信
	local.sin_addr.s_addr = INADDR_ANY;
	socket1 = socket(AF_INET, SOCK_DGRAM, 0);//UDP
	bind(socket1, (sockaddr*)&local, sizeof local);//绑定套接字
	while (1)
	{
		char buffer[1024] = "\0";
		printf("waiting for message from others---\n");
		if (recvfrom(socket1, buffer, sizeof(buffer), 0, (sockaddr*)&from, &fromlen) != SOCKET_ERROR)
		{
			kaisa(buffer, 'l', 2);
			setcolor(0x0a, 0x4);//设置字体颜色
			printf("Received datagram from %s--%s\n", inet_ntoa(from.sin_addr), buffer);
			sendto(socket1, "send successful", sizeof("send successful"), 0, (sockaddr*)&from, fromlen);
			setcolor();
		}
 
		Sleep(500);//延迟0.5s
	}
	closesocket(socket1);
 
}
BOOL InitWinsock()
{
	int Error;
	WORD VersionRequested;
	WSADATA WsaData;
	VersionRequested = MAKEWORD(2, 2);
	Error = WSAStartup(VersionRequested, &WsaData);
	if (Error != 0)
	{
		return FALSE;
	}
	else
	{
		if (LOBYTE(WsaData.wHighVersion) != 2 || HIBYTE(WsaData.wHighVersion) != 2)
		{
			WSACleanup();
			return FALSE;
		}
 
	}
 
	return FALSE;
}

 

你可能感兴趣的:(基于UDP的简易通信程序)