2

再会!

我想创建一个客户端并将其连接到 netcat 简单服务器。

1) 使用默认设置和#define 编译 lib(唯一的东西 - 我设置了一些调试信息) 2) 将 lib 链接到项目(工作正常) 3) 使用 ubuntu 设置虚拟机并让 netcat 使用

~$ sudo netcat -l -v 7 

这意味着我们正在监听端口 7(七)。

Now I use ifconfig and get
inet addr:172.17.9.71 
Bcast:172.17.11.255 
Mask:255.255.252.0 

现在还好。

4) ping 和 telnet 工作正常 - 我看到他们可以访问 netcat。

我的 ifconfig 统计数据是

inet addr:172.17.9.165 
Bcast:172.17.11.255 
Mask:255.255.252.0 

5)现在我正在尝试使用我的客户端连接到 netcat 服务器,使用 lwip 编写输出是:

LWIP_HAVE_LOOPIF = 0 
LWIP_HAVE_LOOPIF = 0 
tcp_bind: bind to port 55555 
tcp_connect to port 7 
netif_default =  -780756800 
netif_is_up(netif_default) =  0 
ip_route: No route to 172.17.9.71 
connect err =  -4 
netif_default =  -780756800 
netif_is_up(netif_default) =  0 
ip_route: No route to 172.17.9.71 
ip_output: No route to 172.17.9.71 
Assertion "mem_free: mem->used" failed at line 339 in ../../../../../lwip/src/core/mem.c 

完整清单:

const char *helloworld = "hello world\n"; 



void hello_end(struct tcp_pcb *pcb, u8_t *state) 
{ 
 tcp_err(pcb, NULL); 
 tcp_recv(pcb, NULL); 
 tcp_sent(pcb, NULL); 
 tcp_poll(pcb, NULL, 0); 
 mem_free(state); 
} 
err_t hello_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) 
{ 
 u8_t *state = (u8_t *)arg; 
 u16_t len; 

 if (p == NULL) 
 if (*state == 255) /* close send */ 
 hello_end(pcb, state); 
 else /* close not yet send */ 
 *state |= 2; 
 else 
  { 
 len = p->tot_len; 
 pbuf_free(p); 
 tcp_recved(pcb, len); 
 } 

 return ERR_OK; 
} 

void hello_err(void *arg, err_t err) 
{ 
 mem_free(arg); 
} 

err_t hello_poll_close(void *arg, struct tcp_pcb *pcb) 
{ 
 u8_t *state = (u8_t *)arg; 

 if (tcp_close(pcb) == ERR_OK) 
 { 
 if ((*state & 2) == 2) /* close received */ 
 hello_end(pcb, state); 
 else /* close not yet received */ 
 *state = 255; 
 } 

 return ERR_OK; 
} 

err_t hello_connected(void *arg, struct tcp_pcb *pcb, err_t err) 
{ 
tcp_write(pcb, helloworld, 12, 0); 
 return ERR_OK; 
} 

err_t hello_connect() { 

    lwip_init(); 
    u8_t *state; 
    err_t err; 
    struct tcp_pcb *pcb; 
    ip_addr_t ipaddr; 

      IP4_ADDR(&ipaddr, 172,17,9,71); 
      if ((state = mem_malloc(1)) == NULL)  { 
         return ERR_MEM; 
      } 

       *state = 1; 
       if ((pcb = tcp_new()) == NULL)  { 
       mem_free(state); 
       return ERR_MEM; 
      } 

      tcp_arg(pcb, state); 
      tcp_err(pcb, hello_err); 
      tcp_recv(pcb, hello_recv); 
      tcp_sent(pcb, NULL); 
      tcp_poll(pcb, hello_poll_close, 10); 





          tcp_bind(pcb,IPADDR_ANY, 55555); //Bind ourselvs to the port 55555 and my own adress 


      err = tcp_connect(pcb, &ipaddr, 7, hello_connected); 
      if (err != ERR_OK)  { 
         std::cout << "connect err =  " << (int)err << std::endl; 
        mem_free(state); 
         tcp_abort(pcb); 
    } 

      getchar(); 
      return ERR_OK; 
} 

int main(int argc, char** argv) { 

     err_t err =  hello_connect(); 
       if (err != ERR_OK)  { 
       std::cout << "2err =  " << err << std::endl; 
       } 
        std::cout << "End of Main" << std::endl; 
        return 0; 


    return 0; 
} 

从这里我开始认为问题在于我没有设置NETIF。

但我不知道如何。

我想我不希望 lwip 默认创建 tap0

(它使

tap0      Link encap:Ethernet  HWaddr 86:97:2c:64:b7:78   
          inet addr:192.168.1.1  Bcast:192.168.1.255  Mask:255.255.255.0 
          inet6 addr: fe80::8497:2cff:fe64:b778/64 Scope:Link 
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1 
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:33 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:500 
          RX bytes:0 (0.0 B)  TX bytes:6555 (6.4 KiB) 

)

我想将我的应用程序绑定到 localhost 或 eth1 - 无论如何......我该怎么做?我在申请中做错了什么?

顺便说一句 - 当我这样做的时候

char *eth = "eht1"; 
netif_set_default(netif_find(eth)); 

在我的初始化函数中,我得到了这个输出

WIP_HAVE_LOOPIF = 0 
LWIP_HAVE_LOOPIF = 0 
tcpip_thread: PACKET 0x7f72acaa1988 
ip_input: packet not for us. 
tcp_bind: bind to port 55555 
tcp_connect to port 7 
netif_default =  0 

它落在

 err = tcp_connect(pcb, &ipaddr, 7, hello_connected); 

(不通过它...)

1 可以帮忙吗?

4

1 回答 1

0

它是您用来与服务器通信的以太网接口吗?在代码的 hello_connect() 函数中,初始化 lwIP 后,需要为以太网接口分配和初始化一个“struct netif”对象,并使用 netif_add() 将其注册到 lwIP。您还需要使用 netif_set_default() 将其设置为默认接口。我不确定其余的代码。为什么不只使用套接字 API?

于 2014-07-03T13:56:33.280 回答