2014-07-02 105 views
2

美好的一天!lwip init網絡接口

我想製作一個客戶端並將其連接到netcat簡單服務器。

1)Compilled LIB採用默認設置和#定義的(唯一的事情 - 我設置)鏈接lib添加到項目的一些調試信息) 2(正常工作) 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)現在,我想用我的客戶,使用LWIP 輸出寫入連接到netcat的服務器:

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; 
} 

從這裏我開始認爲問題是我沒有建立NE TIF。

但我不知道如何。

我覺得我不想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) 

我想我的應用程序綁定到本地主機或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); 

(不及格吧.. )

有些可以幫助嗎?

回答

0

它是一個以太網接口,您正在使用與您的服務器交談?在代碼的hello_connect()函數中,在初始化lwIP之後,需要爲以太網接口分配和初始化一個「struct netif」對象,並使用netif_add()將它註冊到lwIP。您還需要將它設置爲netif_set_default()的默認接口。 我不確定其餘的代碼。爲什麼不使用套接字API?