UDP protocol realizes simple communication C++

1. IP address

Computers in the network use IP addresses for unique identification. There are two types of IP addresses: IPv4 and IPv6. It is generally expressed in decimal system, such as ipv6.168.1, but not in decimal system.

How to view IP address related information:

Under Windows system, open cmd, enter the command ipconfig, and press enter to view. Under Linux or Mac system, open the terminal, use ifconfig command and press enter to view.

2. Port number

Port number is an integer number label of applications in the computer, which is used to distinguish different applications.

0 ~ 1024 is the port number used or reserved by the system, and 0 ~ 65535 is the valid port number. That is to say, when we want to define the port number for some programs, we should select an integer number in the range of 1024 ~ 65535.

How to view information about port numbers:

Under Windows system, open cmd, enter the command netstat -an, and press enter to view.

3. Communication protocol

The difference between TCP protocol and UDP protocol.

TCP protocol
English Name: Transmission Control Protocol
Transmission control protocol
Protocol Description: TCP is a connection oriented, reliable and byte stream based transport layer communication protocol.

For example: when making a phone call, both parties need to be connected before a dialogue can be held

Features: low efficiency and safe data transmission

UDP protocol
English Name: User Datagram Protocol
Datagram protocol
Protocol Description: UDP is a connectionless transport layer communication protocol.

For example: when sending text messages, there is no need for both parties to establish a connection, and the size of datagram should be limited to 64k

Features: high efficiency, unsafe data transmission and easy packet loss

programming

send end:

  1. Create socket
    WORD wVersionRequested;
	WSADATA wsaData;
	wVersionRequested = MAKEWORD(1, 1);

	int err = WSAStartup(wVersionRequested, &wsaData);//Load socket library
  1. Create a Socket object and specify the port number of the receiving end application and the IP address of the receiving end host.
    SOCKET sockClient = socket(AF_INET, SOCK_DGRAM, 0);//Create socket
	SOCKADDR_IN addrSend;
	addrSend.sin_addr.S_un.S_addr = inet_addr("192.168.0.195");//IP address
	addrSend.sin_family = AF_INET;
	addrSend.sin_port = htons(6000);//Port number
  1. Call sendto() to send the specified data to the server.
sendto(sockClient, "123", strlen("123") + 1, 0, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
  1. Close the Socket object.

receive end

  1. Create socket
    WORD wVersionRequested;
	WSADATA wsaData;
	wVersionRequested = MAKEWORD(1, 1);

	int err = WSAStartup(wVersionRequested, &wsaData);//Load socket library
	SOCKET sockRec = socket(AF_INET, SOCK_DGRAM, 0);//Create socket

2. Bind the socket to a local address and port

	SOCKADDR_IN addrRec;
	addrRec.sin_addr.S_un.S_addr = htonl(INADDR_ANY);//IP address
	addrRec.sin_family = AF_INET;
	addrRec.sin_port = htons(6000);//Port number
	bind(sockSrv, (SOCKADDR*)&addrRec, sizeof(SOCKADDR));
  1. Call recvfrom() to send the specified data to the server.
SOCKADDR_IN addrSen;//Address of receiving and sending end
recvfrom(sockSrv, recvBuf, 100, 0, (SOCKADDR*)&addrSen, &len);
  1. Close the Socket object.

Send.cpp

#include <Winsock2.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")

void main()
{
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;

	wVersionRequested = MAKEWORD(1, 1);

	err = WSAStartup(wVersionRequested, &wsaData);
	if (err != 0)
	{
		return;
	}

	if (LOBYTE(wsaData.wVersion) != 1 ||
		HIBYTE(wsaData.wVersion) != 1)
	{
		WSACleanup();
		return;
	}
	SOCKET sockSen = socket(AF_INET, SOCK_DGRAM, 0);
	SOCKADDR_IN addrSen;
	addrSen.sin_addr.S_un.S_addr = inet_addr("192.168.0.195");
	addrSen.sin_family = AF_INET;
	addrSen.sin_port = htons(6000);
	
	sendto(sockSen, "123", strlen("123") + 1, 0, (SOCKADDR*)&addrSen, sizeof(SOCKADDR));
	closesocket(sockSen);
	WSACleanup();
}

Receive.cpp

#include <Winsock2.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")

void main()
{
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;

	wVersionRequested = MAKEWORD(1, 1);

	err = WSAStartup(wVersionRequested, &wsaData);
	if (err != 0)
	{
		return;
	}

	if (LOBYTE(wsaData.wVersion) != 1 ||
		HIBYTE(wsaData.wVersion) != 1)
	{
		WSACleanup();
		return;
	}

	SOCKET sockRec = socket(AF_INET, SOCK_DGRAM, 0);
	SOCKADDR_IN addrRec;
	addrRec.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
	addrRec.sin_family = AF_INET;
	addrRec.sin_port = htons(6000);

	bind(sockRec, (SOCKADDR*)&addrRec, sizeof(SOCKADDR));

	SOCKADDR_IN addrSen;
	int len = sizeof(SOCKADDR);
	char recvBuf[100];
	while (1)
	{
		recvfrom(sockRec, recvBuf, 100, 0, (SOCKADDR*)&addrSen, &len);
		printf("%s\n", recvBuf);
	}
	closesocket(sockRec);
	WSACleanup();
}

Keywords: C++ network udp

Added by cooldude832 on Sun, 20 Feb 2022 13:11:56 +0200