½ÌѧÎÄ¿âÍø - ȨÍþÎĵµ·ÖÏíÔÆÆ½Ì¨
ÄúµÄµ±Ç°Î»ÖãºÊ×Ò³ > ¾«Æ·Îĵµ > ʵÓÃÄ£°å >

LWIPÖ®SOCKETµÄʵÏÖ(5)

À´Ô´£ºÍøÂçÊÕ¼¯ ʱ¼ä£º2026-05-15
µ¼¶Á£º ¹þ¹þ£¬¿ÉνÊÇÒ»Íø´ò¾¡° £¶ÔÓÚÕâ¸öº¯ÊýÖеöµ÷Óã¬ÎÒÃÇ¿´Ò»¸ö¾ÍºÃÁË£¬±ðµÄʵÏÖÒ²²î²»¶à£¬¾ÍÊǸö¸³ÖµµÄ¹ý³Ì Void tcp_recv(struct tcp_pcb *pcb, err_t (* recv)(void *arg, struct tcp_pcb *tpcb, struct pbuf *p

¹þ¹þ£¬¿ÉνÊÇÒ»Íø´ò¾¡°¡¡£¶ÔÓÚÕâ¸öº¯ÊýÖеöµ÷Óã¬ÎÒÃÇ¿´Ò»¸ö¾ÍºÃÁË£¬±ðµÄʵÏÖÒ²²î²»¶à£¬¾ÍÊǸö¸³ÖµµÄ¹ý³Ì

Void tcp_recv(struct tcp_pcb *pcb, err_t (* recv)(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)) {

pcb->recv = recv; }

setup_tcpÉÏÃæÒ²Óн²¹ý£¬ÊÇÔÚnewconnµÄʱºò±»µ÷Óõ쬴´½¨Íêpcb£¬¾ÍÊǵ÷ÓõÄÕâ¸öº¯Êý£¬ÒÔÅäÖÃÁ¬½ÓµÄ¸÷¸ö»Øµ÷º¯Êý¡£

È»¶øµ½ÕâÀïËÆºõ×ßÁËÒ»¸öËÀºúͬÀïÁË£¬Ã²ËÆÃ»ÓÐʲôµØ·½¶Ôpcb->recvÓе÷Óõ쬶øÎ¨Ò»ÓеľÍÊǽÓÊÕTCPʼþTCP_EVENT_RECVµÄºê¶¨ÒåÖС£Í¬Ê±£¬ÆäËûµÄ¼¸¸öº¯ÊýÒ²ÊÇÀàËÆµÄÇé¿ö£¬ÀýÈçsend_tcpº¯Êý¡£

ÕæÊÇ¡°É½ÇîË®¾¡ÒÉÎÞ·£¬Áø°µ»¨Ã÷ÓÖÒ»´å¡±°¡¡£Ô­À´ÉÏÃæµÄÒ²²»Ö»ÊÇËÀºúͬ¡£Õâ×îµ×²ãµÄ£¨ÔÚtcp²ã£©¾ÍÊÇtcp_input£¬ËüÊÇÖ±½Ó±»ip²ãµ÷Óõģ¬¸Ãº¯ÊýµÄ¶¨Òå×¢ÊÍÊÇÕâôдµÄ£º

* The initial input processing of TCP. It verifies the TCP header, demultiplexes * the segment between the PCBs and passes it on to tcp_process(), which implements * the TCP finite state machine. This function is called by the IP layer (in ip_input()).

Àï¾ÍÒª´ÓtcpµÄÈý´ó½ÓÊÕ´¦Àíº¯Êý˵ÆðÁË¡£

Tcp_inputÓÖµ÷ÓÃÁËtcp_processº¯Êý×ö½øÒ»²½µÄ´¦Àí£¬ËüµÄ¶¨Òå×¢ÊÍÈçÏ£º

* Implements the TCP state machine. Called by tcp_input. In some * states tcp_receive() is called to receive data. The tcp_seg * argument will be freed by the caller (tcp_input()) unless the * recv_data pointer in the pcb is set.

Êǵģ¬ÏÂÃæÊÇtcp_receiveº¯Êý£¬±»tcp_processµ÷ÓÃ

* Called by tcp_process. Checks if the given segment is an ACK for outstanding * data, and if so frees the memory of the buffered data. Next, is places the

* segment on any of the receive queues (pcb->recved or pcb->ooseq). If the segment * is buffered, the pbuf is referenced by pbuf_ref so that it will not be freed until * i it has been removed from the buffer.

È»¶ø¹âÓÐÕâЩµ÷ÓÃ˳ÐòÊDz»Ðеģ¬×îÖØÒªµÄÊÇÏÂÃæÁ½¸ö±äÁ¿¡¾¶¼ÔÚtcp_in.cÖС¿ static u8_t recv_flags;

static struct pbuf *recv_data;

ÔÚtcp_receiveÖÐÓÐÒÔÏÂÖ÷Òª¼¸¾ä if (inseg.p->tot_len > 0)

{

recv_data = inseg.p; }

if (cseg->p->tot_len > 0) {

/* Chain this pbuf onto the pbuf that we will pass to the application. */ if (recv_data)

{

pbuf_cat(recv_data, cseg->p);

} else {

recv_data = cseg->p; }

cseg->p = NULL; }

ÏÂÃæµÄÕâ¸öÊÇtcp_inputÖеģ¬ÊÇÔÚtcp_process´¦ÀíÍêÖ®ºóµÄ if (recv_data != NULL) {

if(flags & TCP_PSH)

{

recv_data->flags |= PBUF_FLAG_PUSH; }

/* Notify application that data has been received. */ TCP_EVENT_RECV(pcb, recv_data, ERR_OK, err); }

¿´×îºóÒ»ÌõÓï¾ä¾ÍºÃÁË£¬ºÜÊìϤÊǰɣ¬¶ÔÁË£¬¾ÍÊÇÉÏÃæ´«ËµÖеÄËÀºúͬ£¬µ½´ËÒ²½â¿ªÁË¡£

LWIPÖ®IP²ãʵÏÖ

ÕâÒ»²¿·ÖµÄʵÏÖ¶¼ÊÇÔÚip.cÎļþÖС¾src\\cor\\ipv4¡¿£¬¿ÉÒÔ¿´µ½ÔÚÕâ¸öÎļþÖÐÖ÷ҪʵÏÖÁË3¸öº¯Êý£¬ip_input£»ip_route£»ip_outputÒÔ¼°ip_output_if¡£ÏÂÃæ·Ö±ðÀ´½éÉÜËüÃÇ¡£

ÕâЩº¯Êý¿ÉÒÔ·Ö³ÉÁ½´óÀࣺ½ÓÊպͷ¢ËÍ¡£ÏÂÃæ¾ÍÏÈ´Ó·¢ËÍ¿ªÊ¼£¬Ê×ÏÈҪ˵µÄ¾ÍÊÇip_outputº¯Êý£¬Õâ¸öÒ²ÊÇ·¢Ë͹ý³ÌÖÐ×îÖØÒªµÄÒ»¸ö£¬ËüÊDZ»tcp²ãµ÷Óõģ¬Ïêϸ¿É²Î¼ûÒÔÉÏÕ½ڡ£

* Simple interface to ip_output_if. It finds the outgoing network * interface and calls upon ip_output_if to do the actual work.

err_t ip_output(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest, u8_t ttl, u8_t tos, u8_t proto) {

struct netif *netif;

if ((netif = ip_route(dest)) == NULL) { return ERR_RTE; }

return ip_output_if(p, src, dest, ttl, tos, proto, netif); }

¿ÉÒÔ¿´µ½¸Ãº¯ÊýµÄʵÏÖ¾ÍÏñ×¢ÊÍËù˵µÄÒ»Ñù£¬Ö±½Óµ÷ÓÃÁËip_routeºÍip_outputifÁ½¸öº¯Êý¡£¸ù¾ÝÒÔÍùµÄ¾­Ñ飬ÏÈ¿´ÏÂnetifÕâ¸ö½á¹¹µÄʵÏÖÇé¿ö£º

* Generic data structure used for all lwIP network interfaces. * The following fields should be filled in by the initialization

* function for the device driver: hwaddr_len, hwaddr[], mtu, flags//Õ⼸¸öÊÇÒªÓÃÇý¶¯²ãÌîдµÄ

struct netif {

/** pointer to next in linked list */ struct netif *next;

/** IP address configuration in network byte order */ struct ip_addr ip_addr; struct ip_addr netmask; struct ip_addr gw;

/** This function is called by the network device driver

* to pass a packet up the TCP/IP stack. */

err_t (* input)(struct pbuf *p, struct netif *inp);

/** This function is called by the IP module when it wants

* to send a packet on the interface. This function typically * first resolves the hardware address, then sends the packet. */

err_t (* output)(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr);

/** This function is called by the ARP module when it wants

* to send a packet on the interface. This function outputs * the pbuf as-is on the link medium. */

err_t (* linkoutput)(struct netif *netif, struct pbuf *p);

#if LWIP_NETIF_STATUS_CALLBACK

/** This function is called when the netif state is set to up or down */ void (* status_callback)(struct netif *netif); #endif /* LWIP_NETIF_STATUS_CALLBACK */ ¡­¡­ ´Ë´¦Òþ²Ø£º1976×Ö£¬È«²¿ÎĵµÄÚÈÝÇëÏÂÔØºó²é¿´¡£Ï²»¶¾ÍÏÂÔØ°É ¡­¡­

LWIPÖ®SOCKETµÄʵÏÖ(5).doc ½«±¾ÎĵÄWordÎĵµÏÂÔØµ½µçÄÔ£¬·½±ã¸´ÖÆ¡¢±à¼­¡¢ÊղغʹòÓ¡
±¾ÎÄÁ´½Ó£ºhttps://www.jiaowen.net/wendang/520675.html£¨×ªÔØÇë×¢Ã÷ÎÄÕÂÀ´Ô´£©
Copyright © 2020-2025 ½ÌÎÄÍø °æÈ¨ËùÓÐ
ÉùÃ÷ :±¾ÍøÕ¾×ðÖØ²¢±£»¤ÖªÊ¶²úȨ£¬¸ù¾Ý¡¶ÐÅÏ¢ÍøÂç´«²¥È¨±£»¤ÌõÀý¡·£¬Èç¹ûÎÒÃÇ×ªÔØµÄ×÷Æ·ÇÖ·¸ÁËÄúµÄȨÀû,ÇëÔÚÒ»¸öÔÂÄÚ֪ͨÎÒÃÇ£¬ÎÒÃǻἰʱɾ³ý¡£
¿Í·þQQ£º78024566 ÓÊÏ䣺78024566@qq.com
ËÕICP±¸19068818ºÅ-2
Top
¡Á ÓοͿì½ÝÏÂÔØÍ¨µÀ£¨ÏÂÔØºó¿ÉÒÔ×ÔÓɸ´ÖƺÍÅŰ棩
VIP°üÔÂÏÂÔØ
ÌØ¼Û£º29 Ôª/Ô ԭ¼Û:99Ôª
µÍÖÁ 0.3 Ôª/·Ý ÿÔÂÏÂÔØ150·Ý
ȫվÄÚÈÝÃâ·Ñ×ÔÓɸ´ÖÆ
VIP°üÔÂÏÂÔØ
ÌØ¼Û£º29 Ôª/Ô ԭ¼Û:99Ôª
µÍÖÁ 0.3 Ôª/·Ý ÿÔÂÏÂÔØ150·Ý
ȫվÄÚÈÝÃâ·Ñ×ÔÓɸ´ÖÆ
×¢£ºÏÂÔØÎĵµÓпÉÄܳöÏÖÎÞ·¨ÏÂÔØ»òÄÚÈÝÓÐÎÊÌ⣬ÇëÁªÏµ¿Í·þЭÖúÄú´¦Àí¡£
¡Á ³£¼ûÎÊÌ⣨¿Í·þʱ¼ä£ºÖÜÒ»µ½ÖÜÎå 9:30-18:00£©