C语言实现Windows下的socket编程

一、UDP(数据报)协议

UDP: User Datagram Protocol的简称, 中文名是用户数据报协议,是OSI(Open System Interconnection,开放式系统互联) 参考模型中一种无连接的传输层协议,提供面向事务的简单不可靠信息传送服务。

1.1Server(服务器):接收端

#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include 
#include 
using namespace std;
#pragma comment(lib, "ws2_32.lib")
#define PORT 8233
int main()
{
	WSADATA wsaData;
	WSAStartup(0x0202, &wsaData); //初始化Socket
	SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);//UDP
	if (INVALID_SOCKET == sock)
	{
		cout << "创建socket套接字出错:" << WSAGetLastError() << endl;
		return -1;
	}
	sockaddr_in sa = {AF_INET};
	sa.sin_port = htons(PORT);//绑定的端口
	sa.sin_addr.s_addr = 0;//绑定本机所有IP
	//绑定socket和地址(IP地址和端口)
	int n = bind(sock, (sockaddr*)&sa, sizeof(sa));
	if (SOCKET_ERROR == n)
	{
		cout << "绑定时套接字时出错:" << WSAGetLastError() << endl;
		return -1;
	}
	char str[100];
	sockaddr_in from; //获取(客户端)发送端地址信息
	int lenFrom = sizeof(from);
	//循环接受信息,接收到 "#" 退出循环
	while (true)
	{
		n = recvfrom(sock, str, sizeof(str), 0, (sockaddr*)&from, &lenFrom);
		if (strcmp(str, "#") == 0)
			break;
		cout << "IP:" << inet_ntoa(from.sin_addr) << "PORT:" << htons(from.sin_port) << endl;;
		str[n] = 0;
		cout << str << endl;;
	}
	closesocket(sock);
	WSACleanup();
	return 0;
}

1.2Client(客户端):发送端

#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include 
#include 
using namespace std;
#pragma comment(lib, "ws2_32.lib")
#define PORT 3473

int main()
{
	WSADATA wsaData;
	WSAStartup(0x0202, &wsaData);
	SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (INVALID_SOCKET == sock)
	{
		cout << "创建socket套接字出错:" << WSAGetLastError() << endl;
		return -1;
	}
	sockaddr_in sa = {AF_INET};
	sa.sin_port = htons(PORT);
	sa.sin_addr.s_addr = 0;
	bind(sock, (sockaddr*)&sa, sizeof(sa));
	//设置接收端地址(IP和端口信息)
	sockaddr_in to = { AF_INET };
	to.sin_port = htons(8233);
	to.sin_addr.s_addr = inet_addr("127.0.0.1");

	char str[100];
	while (true)
	{
		cin >> str; //输入要发送的信息
		sendto(sock, str, strlen(str), 0, (sockaddr*)&to, sizeof(to));
		if (strcmp(str, "#") == 0)
			break;
	}
	closesocket(sock);
	WSACleanup();
	return 0;
}

二、TCP(传输控制)协议

TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的、可靠的、基于字节流的传输层通信协议。

2.1Server(服务器端):

#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include 
#include 
using namespace std;
#pragma comment(lib, "ws2_32.lib")
#define PORT 8233
int main()
{
	WSADATA wsaData;
	WSAStartup(0x0202, &wsaData); //初始化Socket
	SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);//UDP
	if (INVALID_SOCKET == sock)
	{
		cout << "创建socket套接字出错:" << WSAGetLastError() << endl;
		return -1;
	}
	sockaddr_in sa = {2};
	sa.sin_port = htons(PORT);//绑定的端口
	sa.sin_addr.S_un.S_addr = 0;//绑定本机所有IP

	int n = bind(sock, (sockaddr*)&sa, sizeof(sa));
	if (SOCKET_ERROR == n)
	{
		cout << "绑定时套接字时出错:" << WSAGetLastError() << endl;
		return -1;
	}
	//监听
	listen(sock, 5);

	SOCKET socka = INVALID_SOCKET;
	char str[2048];
	while (true)
	{
		socka = accept(sock, NULL, NULL);
		const char* p = "Welcome to connect";
		send(socka, p, strlen(p), 0);
		while (true)
		{
			int n = recv(socka, str, sizeof(str), 0);
			if (n <= 0)
				break;
			str[n] = 0;
			cout << str << endl;
		}
	}
	closesocket(socka);
	closesocket(sock);
	WSACleanup();
	return 0;
}

2.2Client(客户端):

#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include 
#include 
using namespace std;
#pragma comment(lib, "ws2_32.lib")
#define PORT 3473

int main()
{
	WSADATA wsaData;
	WSAStartup(0x0202, &wsaData);
	SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
	if (INVALID_SOCKET == sock)
	{
		cout << "创建socket套接字出错:" << WSAGetLastError() << endl;
		return -1;
	}
	sockaddr_in sa = {AF_INET};
	bind(sock, (sockaddr*)&sa, sizeof(sa));

	//服务器地址
	sockaddr_in to = { AF_INET};
	to.sin_port = htons(8233);
	to.sin_addr.s_addr = inet_addr("127.0.0.1");

	//连接
	int n = connect(sock, (sockaddr*)&to, sizeof(to));
	if (n == SOCKET_ERROR)
	{
		cout << "连接服务器时失败:" << GetLastError() << endl;
		return -1;
	}
	char str[2048];
	n = recv(sock, str, sizeof(str), 0);
	str[n] = 0;
	cout << str << endl;
	
	while (true)
	{
		cout << "请输入要发送的信息:";
		cin >> str;
		if (strcmp(str, "#") == 0)
			break;
		send(sock, str, strlen(str), 0);
	}
	closesocket(sock);
	WSACleanup();
	return 0;
}

你可能感兴趣的:(C语言实现Windows下的socket编程)