lwIP Socket Quest Bind

Source: Internet
Author: User

a basic socket establishment sequence isServer side:
    • Socket ()
    • Bind ()
    • Listen ()
    • Accept ()
    • Recv ()
Client side:
    • Socket ()
    • Connect ()
    • Send ()
 This article focuses on the server-side bind () process. when the user calls bind (), the actual call is Lwip_bind (), which we look at from this function:
1 int2Lwip_bind (intSConst structSOCKADDR *name, socklen_t Namelen)3 {4 ..................5Sock = Get_socket (s);//get the full structure of the socket in LWIP based on the socket number returned by the socket () function6   if(!sock)7     return-1;8 .................9Local_addr.addr = ((Const structsockaddr_in *) name)sin_addr.s_addr;TenLocal_port = ((Const structsockaddr_in *) name)Sin_port; One ................. AErr =Netconn_bind(Sock->conn, &local_addr, Ntohs (Local_port));//The main work is in this function. - ................. -   return 0; the}
Lwip_bind () requires the user to pass in the IP address and port number to be bound.  next look at the function netconn_bind ()
1 err_t2Netconn_bind (structNetconn *conn,structIP_ADDR *addr, u16_t port)3 {4   structapi_msg msg;5 6Lwip_error ("Netconn_bind:invalid Conn", (conn! = NULL),returnerr_arg;);7 8Msg.function =Do_bind;//The use of api_msg has been described in the socket creation article, which we have actually called into the Do_bind function .9Msg.msg.conn =Conn;TenMSG.MSG.MSG.BC.IPADDR =addr; OneMsg.msg.msg.bc.port =Port; ATcpip_apimsg (&msg); -   returnConn->err; -}
Do_bind calls Raw_bind (), Tcp_bind (), or raw_bind (), respectively, based on the type of user passed in. take Tcp_bind () as an example:
1 err_t2Tcp_bind (structTCP_PCB *PCB,structIP_ADDR *ipaddr, u16_t Port)3 {4 .............5   if(!ip_addr_isany (IPADDR)) {//if the user's incoming IP address is not 0, the user's incoming IP address is recorded in the PCB6PCB->LOCAL_IP = *ipaddr;7   }8Pcb->local_port = port;//put the user's incoming port number in the PCB9 ..............Ten}
at this point, the bind () function puts the user's incoming IP address and port number into the PCB. This is also the meaning of bind ().  But here we see, if the user incoming IP address is not 0, only the IP address to the PCB, then do not miss any doubt we will ask, if the user is 0, then pcb->local_ip is how much? The answer is, if the user passes a 0 IP address to come in, here will not set the PCB->LOCAL_IP first. When the TCP layer is contracted, the tcp_output_segment () function is called, and the function is:
1 Static void2Tcp_output_segment (structTcp_seg *seg,structTCP_PCB *PCB)3 {4 ..........5   / * If we don ' t have a local IP address, we get the one by6 calling Ip_route (). */7   if(Ip_addr_isany (& (pcb->LOCAL_IP)))  {8Netif = Ip_route (& (pcb->remote_ip));9     if(Netif = =NULL) {Ten       return; One     } AIp_addr_set (& (PCB->LOCAL_IP), & (NETIF->IP_ADDR));//set the IP address of the PCB -   } - .......... the}
Note the red Note above. This code shows that when the TCP layer is contracted, if it is found that pcb->local_ip is 0, it will get a native address from the IP layer and fill this native address into the pcb->local_ip. so we can also know that if the user in the call bind (), want to directly use the local address as the binding IP address, you can directly in this parameter to pass a 0. This does not set the IP address of the PCB in Bind (), and the IP address of the PCB is set when the TCP layer is contracted.

lwIP Socket Quest Bind

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.