windows 网络编程 tcp和udp

TCP和UDP属于传输层协议。其中TCP提供IP环境下的数据可靠传输,它事先为要发送的数据开辟好连接通道(三次握手),然后再进行数据发送;而UDP则不为IP提供可靠性,一般用于实时的视频流传输,像rtp、rtsp就是建立在udp的基础上的。
本文参考基于TCP/IP的网络编程(C++语言编程)

注意关闭安全开发生命周期(SDL)。

基于TCP
基本思路:

服务端:
1、加载socket库(WSADATA wsdata;)
2、创建socket套接字(socket函数)
3、分配IP地址和端口号(bind函数)
4、监听可接收请求状态(listen函数)
5、接收客户端连接请求(accept函数)
6、开始传输数据(recv/send函数)
7、关闭套接字(closesock函数)

客户端:
1、加载socket库(WSADATA wsdata;)
2、创建socket套接字(socket函数)
3、初始化客户端和服务端的地址包
4、与服务器创建连接(connect函数)
5、开始传输数据(send/recv函数)
6、关闭套接字(closesock函数)

server.cpp

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<cstring>
#include<WinSock2.h>

//链接ws2_32_lib库文件到此项目中
#pragma comment(lib,"ws2_32.lib")
using namespace std;

//===============全局变量===================
//设置缓冲区
const int BUF_SIZE = 2048;
SOCKET sockSer, sockCli;
SOCKADDR_IN addrSer, addrCli;
int naddr = sizeof(SOCKADDR_IN);

char sendbuf[BUF_SIZE];
char recvbuf[BUF_SIZE];

//===============函数声明===================
int main()
{
	cout << "service starting..." << endl;

	//加载socket库
	WSADATA wsadata;
	if (WSAStartup(MAKEWORD(2, 2), &wsadata) != 0)
	{
		cout << "Load socket database faild." << endl;
		system("pause");
		return 0;
	}
	else
	{
		cout << "Load socket database success." << endl;
	}
	//创建socket套接字
	sockSer = socket(AF_INET, SOCK_STREAM, 0);
	//描述协议族,INET属于ipv4
	//SOCK_STREAM创建套接字类型:tcp
	//SOCK_DGRAM创建套接字类型:udp
	//0不指定协议,常用的就是tcp,udp

	//初始化地址包
	addrSer.sin_addr.s_addr = inet_addr("180.201.21.59");
	addrSer.sin_family = AF_INET;
	addrSer.sin_port = htons(1111);

	//分配IP地址与端口号--绑定
	bind(sockSer, (SOCKADDR*)&addrSer, sizeof(SOCKADDR));
	//强制将SOCKADDR_IN转化为SOCKADDR

	//监听可接收请求状态
	while (true)
	{
		cout << "connect starting.." << endl;
		//监听连接请求
		listen(sockSer, 5);
		//等待连接最大数:5

		//接收连接
		sockCli = accept(sockSer, (SOCKADDR*)&addrCli, &naddr);
		if (sockCli != INVALID_SOCKET)
		{
			while (true)
			{
				cout << "connect success!" << endl;
				cout << "sending$:" ;
				cin >> sendbuf;
				//向客户端发送信息
				send(sockCli, sendbuf, sizeof(sendbuf), 0);
				//接收客户端信息
				recv(sockCli, recvbuf, sizeof(recvbuf), 0);

				cout << "receviced: " << recvbuf << endl;

			}
		}
		else
		{
			cout << "connect faild!!" << endl;
		}
	}
	closesocket(sockSer);
	closesocket(sockCli);

	system("pause");
	return 0;
}

client.cpp

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<cstring>
#include<WinSock2.h>

//链接ws2_32_lib库文件到此项目中
#pragma comment(lib,"ws2_32.lib")
using namespace std;

//===============全局变量===================
//设置缓冲区
const int BUF_SIZE = 2048;
SOCKET sockSer, sockCli;
SOCKADDR_IN addrSer, addrCli;
int naddr = sizeof(SOCKADDR_IN);

char sendbuf[BUF_SIZE];
char recvbuf[BUF_SIZE];

//===============函数声明===================
int main()
{
	cout << "client starting..." << endl;

	//加载socket库
	WSADATA wsadata;
	if (WSAStartup(MAKEWORD(2, 2), &wsadata) != 0)
	{
		cout << "Load socket database faild." << endl;
		system("pause");
		return 0;
	}
	else
	{
		cout << "Load socket database success." << endl;
	}
	//创建socket套接字
	sockCli = socket(AF_INET, SOCK_STREAM, 0);
	//描述协议族,INET属于ipv4
	//SOCK_STREAM创建套接字类型:tcp
	//SOCK_DGRAM创建套接字类型:udp
	//0不指定协议,常用的就是tcp,udp

	//初始化客户端地址包
	addrCli.sin_addr.s_addr = inet_addr("127.0.0.1");
	addrCli.sin_family = AF_INET;
	addrCli.sin_port = htons(1111);

	//初始化服务器地址
	addrSer.sin_addr.s_addr = inet_addr("180.201.21.59");
	addrSer.sin_family = AF_INET;
	addrSer.sin_port = htons(1111);

	while (true)
	{
		if (connect(sockCli, (SOCKADDR*)&addrSer, sizeof(addrSer) )!= SOCKET_ERROR);
		{
			while (true)
			{
				cout << "connect success!" << endl;
				recv(sockCli, recvbuf, sizeof(recvbuf), 0);
				cout << "receviced: " << recvbuf << endl;

				cout << "sending$:" ;
				cin >> sendbuf;
				//向客户端发送信息
				send(sockCli, sendbuf, sizeof(sendbuf), 0);
				cout << "send success!" << endl;
			}
		}
		cout << "send faild!" << endl;
		
	}
	closesocket(sockSer);
	closesocket(sockCli);

	system("pause");
	return 0;
}

基于UDP
基本思路:

服务端:
1、加载socket库(WSADATA wsdata;)
2、创建socket套接字(socket函数)
3、分配IP地址和端口号(bind函数)
4、开始传输数据(recvfrom/sendto函数)
5、关闭套接字(closesock函数)

客户端:
1、加载socket库(WSADATA wsdata;)
2、创建socket套接字(socket函数)
3、只初始化服务端的地址包
4、开始传输数据(send/recv函数)
5、关闭套接字(closesock函数)
server.cpp

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<cstring>
#include<WinSock2.h>

//链接ws2_32_lib库文件到此项目中
#pragma comment(lib,"ws2_32.lib")
using namespace std;

//===============全局变量===================
//设置缓冲区
const int BUF_SIZE = 2048;
SOCKET sockSer;
SOCKADDR_IN addrSer,addrCli;
int naddr = sizeof(SOCKADDR_IN);

char sendbuf[BUF_SIZE];
char recvbuf[BUF_SIZE];

//===============函数声明===================
int main()
{
	cout << "service starting..." << endl;

	//加载socket库
	WSADATA wsadata;
	if (WSAStartup(MAKEWORD(2, 2), &wsadata) != 0)
	{
		cout << "Load socket database faild." << endl;
		system("pause");
		return 0;
	}
	else
	{
		cout << "Load socket database success." << endl;
	}
	//创建socket套接字
	sockSer = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	//描述协议族,INET属于ipv4
	//SOCK_STREAM创建套接字类型:tcp
	//SOCK_DGRAM创建套接字类型:udp
	//0不指定协议,常用的就是tcp,udp
	//IPPROTO_UDP,指定UDP

	//初始化地址包
	addrSer.sin_addr.s_addr = INADDR_ANY;
	addrSer.sin_family = AF_INET;
	addrSer.sin_port = htons(1111);

	//分配IP地址与端口号--绑定
	bind(sockSer, (SOCKADDR*)&addrSer, sizeof(SOCKADDR));
	//强制将SOCKADDR_IN转化为SOCKADDR

	while (true)
	{
		cout << "sending$:" ;
		cin >> sendbuf;
		int len = sizeof(SOCKADDR);
		//向客户端发送信息
		sendto(sockSer, sendbuf, sizeof(sendbuf), 0,(SOCKADDR*)&addrCli,sizeof(SOCKADDR));
		//接收客户端信息
				
		recvfrom(sockSer, recvbuf, sizeof(recvbuf), 0,(SOCKADDR*)&addrCli, &len);
		cout << "receviced: " << recvbuf << endl;
	}
	cout << "connect faild!!" << endl;
		
	closesocket(sockSer);
//	closesocket(sockCli);

	system("pause");
	return 0;
}

client.cpp

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<cstring>
#include<WinSock2.h>

//链接ws2_32_lib库文件到此项目中
#pragma comment(lib,"ws2_32.lib")
using namespace std;

//===============全局变量===================
//设置缓冲区
const int BUF_SIZE = 2048;
SOCKET  sockCli;
SOCKADDR_IN addrSer, addrCli;
int naddr = sizeof(SOCKADDR_IN);

char sendbuf[BUF_SIZE];
char recvbuf[BUF_SIZE];

//===============函数声明===================
int main()
{
	cout << "client starting..." << endl;

	//加载socket库
	WSADATA wsadata;
	if (WSAStartup(MAKEWORD(2, 2), &wsadata) != 0)
	{
		cout << "Load socket database faild." << endl;
		system("pause");
		return 0;
	}
	else
	{
		cout << "Load socket database success." << endl;
	}
	//创建socket套接字
	sockCli = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	//描述协议族,INET属于ipv4
	//SOCK_STREAM创建套接字类型:tcp
	//SOCK_DGRAM创建套接字类型:udp
	//0不指定协议,常用的就是tcp,udp
	//IPPROTO_UDP,指定UDP


	//初始化服务器地址
	addrSer.sin_addr.s_addr = inet_addr("127.0.0.1");
	addrSer.sin_family = AF_INET;
	addrSer.sin_port = htons(1111);

	while (true)
	{
		int len = sizeof(SOCKADDR);

		recvfrom(sockCli, recvbuf, sizeof(recvbuf), 0, (SOCKADDR*)&addrSer, &len);
		cout << "receviced: " << recvbuf << endl;

		cout << "sending$:" ;
		cin >> sendbuf;
		//向客户端发送信息
		sendto(sockCli, sendbuf, sizeof(sendbuf), 0, (SOCKADDR*)&addrSer, sizeof(SOCKADDR));
		cout << "send success!" << endl;
	}
		cout << "send faild!" << endl;
		
//	closesocket(sockSer);
	closesocket(sockCli);

	system("pause");
	return 0;
}

windows 网络编程 tcp和udp

上一篇:Oracle Text(全文索引)


下一篇:win10远程桌面设置202008060931