Home Operation and Maintenance Safety How to analyze UDP protocol

How to analyze UDP protocol

May 12, 2023 pm 02:49 PM
udp

一、套接字(socket)

    套接字socket: ip地址 + port端口号。在TCP/IP协议中,它唯一标识网络通讯中的一个进程。套接字用来描述网络连接的 一对一关系。

    TCP/IP协议规定,网络数据流应采用 大端字节序,即 (内存)低地址高字节(数据)。

二、UDP_SOCKET 相关

    UDP 协议  ----  用户数据报协议(面向非连接)   ---  SOCK_DGRAM 

    h表示host,n表示network,l表示32位长整数,s表示16位短整数。

    IPv4地址格式定义在netinet/in.h中,IPv4地址: sockaddr_in结构体,包括16位端口号和32位IP地址

struct sockaddr_in
 {
    uint8_t sin_len;
    sa_family_t sin_family;
    in_port_t sin_port;
    struct in_addr sin_addr;
    char sin_zero[8];
};
Copy after login

三、UDP socket 实例:

//udp_server.c

#include <stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<unistd.h>
#include<string.h>
#include<errno.h>


void usage(const char *proc)
{
	printf("%s:[ip][port]\n",proc);
}

int main(int argc,char *argv[])
{
	if(argc != 3)
	{
		usage(argv[0]);
		return 1;
	}
	char *_ip=argv[1];
	int _port=atoi(argv[2]);
	int sock=socket(AF_INET,SOCK_DGRAM,0);
	if(sock < 0)
	{
		perror("socket");
		exit(1);
	}

	struct sockaddr_in local;
	local.sin_family=AF_INET;
	local.sin_port=htons(_port);
	local.sin_addr.s_addr=inet_addr(_ip);
	if(bind(sock,(struct sockaddr*)&local,sizeof(local)) < 0)
	{
		perror("bind");
		exit(2);
	}
	int done=0;
	char buf[1024];
	struct sockaddr_in client;
	socklen_t len=sizeof(client);
	while(!done)
	{
		ssize_t _size=recvfrom(sock,buf,sizeof(buf)-1,0,(struct sockaddr*)&client,&len);
		if(_size > 0)
		{
			buf[_size]=&#39;\0&#39;;
			printf("[%s : %d]: %s\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port),buf);
		}
		else if(_size == 0)
		{
			printf("client close...\n");
		}
		else
		{}
	}
	return 0;
}
Copy after login
Copy after login

//udp_client.c

#include <stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<unistd.h>
#include<string.h>
#include<errno.h>


void usage(const char *proc)
{
	printf("%s:[ip][port]\n",proc);
}

int main(int argc,char *argv[])
{
	if(argc != 3)
	{
		usage(argv[0]);
		return 1;
	}
	char *_ip=argv[1];
	int _port=atoi(argv[2]);
	int sock=socket(AF_INET,SOCK_DGRAM,0);
	if(sock < 0)
	{
		perror("socket");
		exit(1);
	}

	struct sockaddr_in local;
	local.sin_family=AF_INET;
	local.sin_port=htons(_port);
	local.sin_addr.s_addr=inet_addr(_ip);
	if(bind(sock,(struct sockaddr*)&local,sizeof(local)) < 0)
	{
		perror("bind");
		exit(2);
	}
	int done=0;
	char buf[1024];
	struct sockaddr_in client;
	socklen_t len=sizeof(client);
	while(!done)
	{
		ssize_t _size=recvfrom(sock,buf,sizeof(buf)-1,0,(struct sockaddr*)&client,&len);
		if(_size > 0)
		{
			buf[_size]=&#39;\0&#39;;
			printf("[%s : %d]: %s\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port),buf);
		}
		else if(_size == 0)
		{
			printf("client close...\n");
		}
		else
		{}
	}
	return 0;
}
Copy after login
Copy after login

运行结果:

How to analyze UDP protocol

The above is the detailed content of How to analyze UDP protocol. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the UDP port numbers? What are the UDP port numbers? Feb 23, 2023 pm 02:00 PM

What are the UDP port numbers?

How to implement asynchronous communication using PHP and UDP protocols How to implement asynchronous communication using PHP and UDP protocols Jul 30, 2023 pm 07:31 PM

How to implement asynchronous communication using PHP and UDP protocols

How does Java network programming use UDP for connectionless communication? How does Java network programming use UDP for connectionless communication? Apr 15, 2024 pm 12:51 PM

How does Java network programming use UDP for connectionless communication?

How to analyze UDP protocol How to analyze UDP protocol May 12, 2023 pm 02:49 PM

How to analyze UDP protocol

Detailed explanation of why DNS uses UDP instead of TCP! Detailed explanation of why DNS uses UDP instead of TCP! Mar 01, 2024 pm 08:16 PM

Detailed explanation of why DNS uses UDP instead of TCP!

What transport services does udp provide? What transport services does udp provide? Feb 27, 2023 am 10:20 AM

What transport services does udp provide?

How to implement UDP programming for network communication based on UDP protocol in Java How to implement UDP programming for network communication based on UDP protocol in Java May 17, 2023 pm 01:13 PM

How to implement UDP programming for network communication based on UDP protocol in Java

What does UDP protocol mean? What does UDP protocol mean? Aug 08, 2023 pm 03:43 PM

What does UDP protocol mean?

See all articles