Three LwIP Application Development Notes of: LwIP no UDP client operating system

  Before we achieve a RAW API of UDP-based server, the next, we achieve further use of RAW API UDP client.

. 1 , the UDP protocol outlined

  UDP stands for User Datagram Protocol protocol in the network as it is for processing and TCP protocol packet is a connectionless protocol. In the OSI model, in the transport layer, the upper layer protocol is IP. There does not provide UDP packet grouping, assembly and disadvantages can not sort the data packet, that is, when the message is sent, it is not known whether the complete security arrived.

  The main role of the protocol is UDP traffic network data packets into a compressed form. A typical data packet is a transmission unit of binary data. The first 8 bytes of each packet is used to contain header information, the remaining bytes to contain a specific data transfer.

  UDP header consists of 4 domains, wherein each domain occupies 2 bytes each, as follows: source port number, destination port number, datagram length, checksum value. The following data structure:

 

  UDP protocol uses port numbers to different applications retain their respective data transmission channel. UDP and TCP protocol is the use of this mechanism implementation supports simultaneous transmission and reception of data to multiple applications within the same time. One of the data transmission (may be a client or server side) the UDP data packet sent by the source port, the destination port is one of data received by the data receiver. Some network applications only use a static port in advance to reserve or registered; while other network applications, you can use the dynamic port is not registered. Because the two-byte UDP header using the stored port number, the effective range of port numbers from 0 to 65535. Generally, the port number is greater than 49,151 are dynamic port.

  Length is the number of data packets includes a header and a data portion including the total bytes. Because the header length is fixed, so the field is primarily used to calculate the variable length data portion. The maximum length of the datagram and vary depending on the operating environment. Theoretically, the maximum length of the datagram, including the header comprises 65535 bytes. However, some practical applications tend to limit the size of the data reported, sometimes reduced to 8192 bytes.

  UDP protocol header checksum value to ensure data security. First check value obtained through a special calculation algorithm at the data transmission side, after transmission to the receiver, still need to be recalculated. If a datagram is a third party during transmission or tampered with since the damage due to line noise, etc., and transmits the checksum calculated value will not match the recipient, whereby the UDP protocol whether the error can be detected.

2 , UDP client design

  We briefly introduced the UDP datagram protocol and its front, then we will consider how to achieve client UDP-based protocol.

  First, let's take a look at the API function associated with the UDP, and they make a preliminary introduction, should we need to use them to implement our application. And function as follows:

 

  We have learned a UDP server implementation steps, then we explain the implementation steps UDP client.

  First of all, it is still to create a new UDP control block.

  Next, establish a connection with the server, the configuration information of address and port comprises a server.

  Next, if the connection is no problem, then register the client callback function. And implementation of server-side as it related to the complexity of the functions to be achieved. We just implement a simple UDP client, so we send fixed information to the server, continue to send corresponding information after receiving the reply.

  Finally, since the client is the initiator of dialogue, so after you register a callback function, the client wants to initiate a dialogue for the first time.

3 , UDP client implementation

  UDP server to achieve, we will still is divided into two aspects: First, part of the initial configuration UDP client; Second, UDP clients achieve specific content, the content of which is the callback function.

  First part of the initial configuration to achieve UDP client. UDP define a new control block, connected to the specified address and port of the server, the same as our loop we use a relatively simple validation server port. Then register a callback function, initiating client for the first time communications. Specific code as follows:

. 1  / * the UDP client initialization configuration * / 
2  void UDP_Client_Initialization ( void )
 . 3  {
 . 4    ip_addr_t DestIPaddr;
 . 5    err_t ERR;
 . 6    struct udp_pcb * UPCB;
 . 7    char Data [] = " This IS A Client. " ;
 . 8   
. 9    / * set the IP address of the server * / 
10    IP4_ADDR (& DestIPaddr, udpServerIP [ 0 ], udpServerIP [ . 1 ], udpServerIP [ 2 ], udpServerIP [ . 3 ]);
 . 11   
12 is    / *Create a new UDP control block * / 
13 is    UPCB = udp_new ();
 14   
15    IF (! UPCB = NULL)
 16    {
 . 17      / * server address, port configuration * / 
18 is      ERR = udp_connect (UPCB, & DestIPaddr, UDP_ECHO_SERVER_PORT) ;
 . 19   
20 is      IF (ERR == ERR_OK)
 21 is      {
 22 is        / * registered callback function * / 
23 is        udp_recv (UPCB, UDPClientCallback, NULL);
 24        / * * data transmission, the first client sends the data connection to the server, send function will traverse to find the source IP address configuration, if the source IP address is configured, the data transmission fails. There arise problems mentioned later in the summary * * / 
25       UdpClientSendPacket(upcb,data);  
26     }
27   }
28 }

  Second, implement UDP client's specific implementation of the content. Due to the simple response to our client to achieve, so we just return to the same server content.

 1 /* 定义UDP客户端数据处理回调函数 */
 2 static void UDPClientCallback(void *arg,struct udp_pcb *upcb,struct pbuf *p,const ip_addr_t *addr,u16_t port)
 3 {
 4   udp_send(upcb, p);     //数据回显
 5  
 6   pbuf_free(p);
 7 }
 8  
 9 /* 客户端数据发送函数 */
10 void UdpClientSendPacket(struct udp_pcb *upcb,char* data)
11 {
12   struct pbuf *p;
13  
14   /* 分配内存空间 */
15   p = pbuf_alloc(PBUF_TRANSPORT,strlen((char*)data), PBUF_POOL);
16  
17   if (p != NULL)
18   {
19  
20     /* 复制数据到pbuf */
21     pbuf_take(p, (char*)data, strlen((char*)data));
22  
23     /* 发送数据 */
24     udp_send(upcb, p);     //发送数据
25  
26     /* 释放pbuf */
27     pbuf_free(p);
28   }
29 }

  当然,如果我们不想人云亦云的回复服务器,则可以编辑我们自己的数据包然后发送回去。所以我们想要实现复杂的应用时,只需要重新编写合适的回调函数就可以了!

4、结论

  我们完成了简单的,基于RAW API的UDP客户端,其本身并不复杂。同样的我们使用网络软件测试其功能,我们在电脑上建立一个服务器端,然后通过我们这个客户端去连接它。能够进行连接并发送接受数据,说明我们这个客户端的设计是符合要求的。

  至此我们完成了UDP客户端及服务器的实现,后续我们将在次基础上实现更为复杂的应用。

欢迎关注:

Guess you like

Origin www.cnblogs.com/foxclever/p/11831214.html