繁体   English   中英

LWIP如何配置多台不同IP的服务器进行连接?

[英]How LWIP configures multiple servers with different IPs to connect?

#define DEST_IP_ADDR0      192
#define DEST_IP_ADDR1      168
#define DEST_IP_ADDR2      0
#define DEST_IP_ADDR3      200
IP4_ADDR(&server_ip, DEST_IP_ADDR0,DEST_IP_ADDR1,DEST_IP_ADDR2,DEST_IP_ADDR3);

原代码中只有主机的static IP可以成功连接到192.168.0.200。 现在我希望任何具有不同 IP 的主机都能够连接。 How to achieve it, or only open one IP segment, only the static IP is 192.168.0.0- The host of 192.168.0.255 can only connect, how to modify the code to achieve it?

这是TCPclient.c的代码

#include "lwip/netif.h"
#include "lwip/ip.h"
#include "lwip/tcp.h"
#include "lwip/init.h"
#include "netif/etharp.h"
#include "lwip/udp.h"
#include "lwip/pbuf.h"
#include <stdio.h>       
#include <string.h>
#include "main.h"
#include "tcpclient.h"
#define DEST_IP_ADDR0      192
#define DEST_IP_ADDR1      168
#define DEST_IP_ADDR2      0
#define DEST_IP_ADDR3      200
#define DEST_PORT      5001
#define UDP_SERVER_PORT     5002
#define UDP_CLIENT_PORT     5002
#define LOCAL_PORT     5001
char str_rev[100];
static void client_err(void *arg, err_t err)
{
    printf("wrong!!\r\n");
            printf("try again!!\r\n");
            printf("close connect\r\n");
            printf("again init\r\n");
            TCP_Client_Init();
   
}
static err_t client_send(void *arg, struct tcp_pcb *tpcb)
{
    uint8_t send_buf[]= "c";
      tcp_write(tpcb, send_buf, sizeof(send_buf), 1);
      return ERR_OK;
}
static err_t client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
  if (p != NULL)
  {        
tcp_recved(tpcb, p->tot_len);
memset(str_rev, 0 , strlen(str_rev));
memcpy(str_rev, p -> payload, p -> tot_len);  
memset(p->payload, 0 , p->tot_len);
pbuf_free(p);
  }
  else if (err == ERR_OK)
 {
   printf("dis connect!\r\n");
   tcp_close(tpcb);
   TCP_Client_Init();
 }
 return ERR_OK;
  }
static err_t client_connected(void *arg, struct tcp_pcb *pcb, err_t err)
{
  printf("connected ok!\r\n");
  tcp_poll(pcb,client_send,2);
  tcp_recv(pcb,client_recv);
  return ERR_OK;
   }
void TCP_Client_Init(void)
{        
    struct tcp_pcb *client_pcb = NULL;   
  ip4_addr_t server_ip;     
  client_pcb = tcp_new();          
  IP4_ADDR(&server_ip, DEST_IP_ADDR0,DEST_IP_ADDR1,DEST_IP_ADDR2,DEST_IP_ADDR3);
  printf("start connect!\r\n");
  tcp_connect(client_pcb, &server_ip, TCP_CLIENT_PORT, client_connected);
    ip_set_option(client_pcb, SOF_KEEPALIVE);       
    printf("tcp_connect ok\r\n");
  tcp_err(client_pcb, client_err);
    printf("yes err f\r\n");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM