自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

學(xué)習(xí)筆記:UDP用戶數(shù)據(jù)報協(xié)議

網(wǎng)絡(luò) 網(wǎng)絡(luò)管理
UDP是一個簡單的面向數(shù)據(jù)報的運輸層協(xié)議:進程的每個輸出操作都正好產(chǎn)生一個UDP數(shù)據(jù)報,并組裝成一份待發(fā)送的IP數(shù)據(jù)報。

UDP是一個簡單的面向數(shù)據(jù)報的運輸層協(xié)議:進程的每個輸出操作都正好產(chǎn)生一個UDP數(shù)據(jù)報,并組裝成一份待發(fā)送的IP數(shù)據(jù)報。這與面向流字符的協(xié)議不同,如TCP,應(yīng)用程序產(chǎn)生的全體數(shù)據(jù)與真正發(fā)送的單個IP數(shù)據(jù)報可能沒有什么聯(lián)系。RFC768是UDP的正式規(guī)范

1、UDP報文封裝

 

[[123688]]

 

UDP首部報文格式如下

UDP首部報文格式

端口號表示發(fā)送進程和接收進程,UDP長度字段指的是UDP首部和UDP數(shù)據(jù)的字節(jié)長度。該字段的最小值為8字節(jié)(發(fā)送一份0字節(jié)的UDP數(shù)據(jù)報是OK的)。這個UDP長度是有冗余的。IP數(shù)據(jù)報長度指的是數(shù)據(jù)報全長,因此UDP數(shù)據(jù)報長度是全長減去IP首部的長度。

特點:不可靠、易分片

UDP不提供可靠性:它把應(yīng)用程序傳給IP層的數(shù)據(jù)發(fā)送出去,但是并不保證它們能到達目的地。

分片:應(yīng)用程序必須關(guān)心IP數(shù)據(jù)報的長度。如果它超過網(wǎng)絡(luò)的MTU,那么就要對IP數(shù)據(jù)報進行分片。如果需要,源端到目的端之間的每個網(wǎng)絡(luò)都要進行分片,并不只是發(fā)送端主機連接***個網(wǎng)絡(luò)才這樣做。

2、IP分片

物理網(wǎng)絡(luò)層一般要限制每次發(fā)送數(shù)據(jù)幀的***長度。任何時候IP層接收到一份要發(fā)送的IP數(shù)據(jù)報時,它要判斷向本地哪個接口發(fā)送數(shù)據(jù)(選路),并查詢該接口獲得其MTU。IP把MTU與數(shù)據(jù)報長度進行比較,如果需要則進行分片。分片可以發(fā)生在原始發(fā)送端主機上,也可以發(fā)生在中間路由器上。把一份IP數(shù)據(jù)報分片以后,只有到達目的地才進行重新組裝。

IP首部中部分字段用于報文分片組裝,具體參見tcp/ip協(xié)議學(xué)習(xí)筆記(3)Internet Protocol(IP)

對于發(fā)送端發(fā)送的每份IP數(shù)據(jù)報來說,其標(biāo)識字段都包含一個唯一值。該值在數(shù)據(jù)報分片時被復(fù)制到每個片中。標(biāo)志字段用其中一個比特來表示“更多的片”。除了***一片外,其他每個組成數(shù)據(jù)報的片都要把該比特置1,下面簡單抓一個分片ping包看一下

***片

***片

第二片

第二片

在分片時,除***一片外,其他每一片中的數(shù)據(jù)部分(除IP首部外的其余部分)必須是8字節(jié)的整數(shù)倍。

標(biāo)志字段中有一個比特稱作“不分片”位。如果將這一比特置1,IP將不對數(shù)據(jù)報進行分片。相反把數(shù)據(jù)報丟棄并發(fā)送一個ICMP差錯報文給起始端

盡管IP分片過程看起來是透明的,但有一點讓人不想使用它:即使只丟失一片數(shù)據(jù)也要重傳整個數(shù)據(jù)報。為什么會發(fā)生這種情況呢?因為I P層本身沒有超時重傳的機制——由更高層來負責(zé)超時和重傳(T C P有超時和重傳機制,但U D P沒有)

IP數(shù)據(jù)報:是指IP層端到端的傳輸單元(在分片之前和重新組裝之后)

分組是指在IP層和鏈路層之間傳送的數(shù)據(jù)單元。一個分組可以是一個完整的IP數(shù)據(jù)報,也可以是IP數(shù)據(jù)報的一個分片。

IP數(shù)據(jù)報

3、UDP數(shù)據(jù)報***長度

理論上,IP數(shù)據(jù)報的***長度是65535字節(jié),這是由IP首部16比特總長度字段所限制的。去除20字節(jié)的IP首部和8個字節(jié)的UDP首部, UDP數(shù)據(jù)報中用戶數(shù)據(jù)的最長長度為65507字節(jié)。但是,大多數(shù)實現(xiàn)所提供的長度比這個***值小。其中兩個限制因素:

***,應(yīng)用程序可能會受到其程序接口的限制。socket API提供了一個可供應(yīng)用程序調(diào)用的函數(shù),以設(shè)置接收和發(fā)送緩存的長度。對于UDP socket,這個長度與應(yīng)用程序可以讀寫的***U D P數(shù)據(jù)報的長度直接相關(guān)。現(xiàn)在的大部分系統(tǒng)都默認提供了可讀寫大于8192字節(jié)的UDP數(shù)據(jù)報

第二個限制來自于TCP/IP的內(nèi)核實現(xiàn)。可能存在一些實現(xiàn)特性(或差錯),使IP數(shù)據(jù)報長度小于65535字節(jié)#p#

4、UDP服務(wù)器的設(shè)計與實現(xiàn)

  1. /*********************************************************************************  
  2.   *Author     :  wph  
  3.   *Version    :  1.0  
  4.   *Date       :  2014/03/01    
  5.   *Description:  udp server   
  6.   *Others     :   
  7.   *History    :     
  8. **********************************************************************************/   
  9. #include<stdio.h>     
  10. #include<string.h>     
  11. #include<unistd.h>     
  12. #include<sys/types.h>     
  13. #include<sys/socket.h>     
  14. #include<stdlib.h>     
  15. #include<netinet/in.h>     
  16. #include<arpa/inet.h>   
  17. #include<event2/event.h>   
  18.    
  19. #include "errocode.h"   
  20. #include "basetype.h"   
  21.    
  22. #define INVALID_FD      -1   
  23. #define PORT            1234     
  24. #define MAXDATASIZE     512    
  25.    
  26. STATIC INT g_iudpFd = INVALID_FD;   
  27.    
  28. VOID udp_callback(evutil_socket_t fd, short what, void *arg)   
  29. {   
  30.     struct sockaddr_in client;     
  31.     socklen_t addrlen;     
  32.     int num;     
  33.     char buf[MAXDATASIZE];    
  34.        
  35.     memset(buf, 0, MAXDATASIZE);   
  36.     num = recvfrom(fd, buf, MAXDATASIZE, 0, (struct sockaddr*)&client, &addrlen);                                        
  37.     if (num < 0)     
  38.     {     
  39.         perror("recvfrom() error\n");     
  40.         exit(1);     
  41.     }    
  42.        
  43.     printf("You got a message (%s) from client.\nIt's ip is%s, port is %d.\n",    
  44.             buf, inet_ntoa(client.sin_addr), htons(client.sin_port));    
  45.        
  46.     sendto(fd, buf, num, 0, (struct sockaddr *)&client, addrlen);     
  47. }   
  48.    
  49. ULONG udp_init(VOID)   
  50. {   
  51.     int sockfd;     
  52.     struct sockaddr_in server;     
  53.        
  54.     /* The caller has already set up fd1, fd2 somehow, and make them  
  55.        nonblocking. */   
  56.     sockfd = socket(AF_INET, SOCK_DGRAM, 0);   
  57.     if(INVALID_FD == sockfd)      
  58.     {     
  59.         perror("Creatingsocket failed.");     
  60.         exit(1);     
  61.     }    
  62.        
  63.     bzero(&server, sizeof(server));     
  64.     server.sin_family = AF_INET;     
  65.     server.sin_porthtons(PORT);     
  66.     server.sin_addr.s_addrhtonl(INADDR_ANY);     
  67.     if(-1 == bind(sockfd, (struct sockaddr *)&server, sizeof(server)))     
  68.     {     
  69.         perror("Bind()error.");     
  70.         exit(1);     
  71.     }   
  72.        
  73.     g_iudpFd = sockfd;   
  74.        
  75.     return EROOR_SUCCESS;   
  76. }   
  77.    
  78. VOID udp_fini(VOID)   
  79. {   
  80.     int sockfd = g_iudpFd;     
  81.    
  82.     if (INVALID_FD != sockfd)   
  83.     {      
  84.         close(sockfd);   
  85.     }   
  86. }   
  87. VOID main_loop(VOID)   
  88. {   
  89.     INT ifd = g_iudpFd;   
  90.         struct event *ev1;   
  91.     struct timeval five_seconds = {5,0};   
  92.     struct event_base *base = event_base_new();   
  93.    
  94.     /* upd 服務(wù)器采用libevet進行處理 */   
  95.     ev1 = event_new(base, ifd, EV_TIMEOUT|EV_READ|EV_PERSIST, udp_callback, NULL);   
  96.    
  97.     event_add(ev1, &five_seconds);   
  98.     event_base_dispatch(base);   
  99.        
  100.     return ;   
  101. }   
  102. INT main()   
  103. {   
  104.     if(EROOR_SUCCESS != udp_init())   
  105.     {   
  106.         return -1;   
  107.     }   
  108.        
  109.     main_loop();   
  110.    
  111.     udp_fini();   
  112.        
  113.     return 0;   
  114. }   
  1. /*********************************************************************************  
  2.   *Copyright(C),2010-2011,  
  3.   *Author     :  wph  
  4.   *Version    :  1.0  
  5.   *Date       :  2014/03/01    
  6.   *Description:  udp client   
  7.   *Others     :   
  8.   *History    :     
  9. **********************************************************************************/   
  10. #include <stdio.h>     
  11. #include <stdlib.h>     
  12. #include <unistd.h>     
  13. #include <string.h>     
  14. #include <sys/types.h>     
  15. #include <sys/socket.h>     
  16. #include <netinet/in.h>     
  17. #include <netdb.h>     
  18.    
  19. #include "errocode.h"   
  20. #include "basetype.h"   
  21.    
  22. #define INVALID_FD      -1   
  23. #define PORT            1234     
  24. #define MAXDATASIZE     512    
  25.     
  26. INT main(INT argc, CHAR *argv[])     
  27. {     
  28.     INT  isockfd = INVALID_FD;   
  29.     UINT uinum = 0;     
  30.     char buf[MAXDATASIZE];   
  31.     socklen_t  addrlen;     
  32.     struct hostent *he;     
  33.     struct sockaddr_in server;   
  34.     struct sockaddr_in peer;     
  35.    
  36.     if (3 != argc)     
  37.     {     
  38.         printf("Usage: %s <IP Address><message>\n", argv[0]);     
  39.         exit(1);     
  40.     }     
  41.    
  42.     if (NULL == (he=gethostbyname(argv[1])))     
  43.     {     
  44.         printf("gethostbyname()error\n");     
  45.         exit(1);     
  46.     }     
  47.    
  48.     if (INVALID_FD == (isockfd = socket(AF_INET, SOCK_DGRAM,0)))     
  49.     {     
  50.         printf("socket() error\n");     
  51.         exit(1);     
  52.     }     
  53.    
  54.     bzero(&server,sizeof(server));     
  55.     server.sin_family = AF_INET;     
  56.     server.sin_port = htons(PORT);     
  57.     server.sin_addr= *((struct in_addr *)he->h_addr);     
  58.     sendto(isockfd, argv[2],strlen(argv[2]),0,(struct sockaddr *)&server,sizeof(server));     
  59.     addrlen=sizeof(server);     
  60.     while (1)     
  61.     {     
  62.         if((uinum=recvfrom(isockfd, buf, MAXDATASIZE, 0, (struct sockaddr *)&peer, &addrlen))== -1)     
  63.         {     
  64.             printf("recvfrom() error\n");     
  65.             exit(1);     
  66.         }     
  67.         if (addrlen != sizeof(server) || memcmp((const void *)&server, (const void *)&peer, addrlen) != 0)     
  68.         {     
  69.             printf("Receive message from otherserver.\n");     
  70.             continue;     
  71.         }     
  72.    
  73.         buf[uinum]='\0';     
  74.         printf("Server Message:%s\n", buf);     
  75.         break;     
  76.    }     
  77.    
  78.  close(isockfd);     
  79. }     

演示:

[root@localhost server]# ./udpserver &

[1] 1389

[root@localhost server]# ./udpclient 127.0.0.1 "i like you"

You got a message (i like you) from client.

It's ip is127.0.0.1, port is 34340.

Server Message:i like you

[root@localhost server]# netstat -aun

Active Internet connections (servers and established)

演示

責(zé)任編輯:林琳 來源: CSDN
相關(guān)推薦

2019-06-24 07:34:38

數(shù)據(jù)報協(xié)議UDPIP

2015-01-05 09:56:41

UDPLinux TCP

2022-11-29 16:29:50

設(shè)備開發(fā)UDP

2019-09-16 08:33:50

TCP協(xié)議UDP

2019-09-12 09:56:33

TCPUDPHTTP

2020-01-03 07:57:39

UDPTCP網(wǎng)絡(luò)協(xié)議

2019-06-25 10:32:19

UDP編程通信

2012-01-06 13:58:47

JavaTCP

2010-06-10 11:51:22

Internet協(xié)議數(shù)據(jù)報

2010-06-12 15:27:23

UDP協(xié)議

2011-08-25 09:29:35

udp中文man

2021-01-20 23:40:27

數(shù)據(jù)泄露OpenWRT攻擊

2011-03-25 15:21:43

2014-04-09 15:12:35

2021-01-06 10:01:09

數(shù)據(jù)泄露漏洞信息安全

2022-12-01 12:14:09

2021-05-11 11:05:58

蘋果iOS隱私

2023-10-23 19:18:05

2015-10-19 18:18:44

2023-08-30 23:15:07

點贊
收藏

51CTO技術(shù)棧公眾號