2

良い一日!

クライアントを作成し、netcat シンプル サーバーに接続したいと考えています。

1)デフォルト設定と#defineでコンパイルされたlib(唯一のもの-いくつかのデバッグ情報を設定しました)2)libをプロジェクトにリンクしました(正常に動作します)3)ubuntuで仮想マシンをセットアップし、netcatを使用して動作させました

~$ sudo netcat -l -v 7 

これは、ポート 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; 
} 

ここから、問題は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)); 

私のInit関数では、この出力が得られます

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 に答える