Ich versuche einen kleine TCP Client mittels LwIP zu realisieren, alles
vergeblich. Die Daten werden einfach nicht gesendet. Der Server meldet
zwar, dass sich ein Client verbindet und dann trennt, aber die Daten
kommen nicht an. In Wireshark sind auch keine Daten zu sehen.
Ich habe mich an dieses Dokument orientiert:
http://lwip.wikia.com/wiki/Raw/TCP
Active connection
Call tcp_new to create a pcb.
Call tcp_connect.
Sending TCP data
Call tcp_sent() to specify a callback function for acknowledgements.
Call tcp_sndbuf() to find the maximum amount of data that can be sent.
Call tcp_write() to enqueue the data.
Call tcp_output() to force the data to be sent.
1
voidclient_write(void)
2
{
3
structtcp_pcb*pcb;
4
structip_addrserver;
5
err_tret_val;
6
intbufspace=0;
7
structpbuf*pb;
8
char*string="Hello TCP World! Lorem ipsum dolor sit amet, consetetur sadipscing elitr\r\n";
9
10
pb=pbuf_alloc(PBUF_TRANSPORT,0,PBUF_REF);
11
pb->payload=string;
12
pb->len=pb->tot_len=strlen(string);
13
14
15
IP4_ADDR(&server,172,16,20,27);
16
17
pcb=tcp_new();// create tcp pcb
18
tcp_bind(pcb,IP_ADDR_ANY,61110);//bind client port for outgoing connection
19
ret_val=tcp_connect(pcb,&server,61111,NULL);//connect to the server
20
21
if(ret_val!=ERR_OK){
22
printf("\r\n Error: %d\n",ret_val);
23
}
24
else{
25
printf("\r\n Connect ok [%d]",ret_val);
26
bufspace=tcp_sndbuf(pcb);// get space which can be used to sent the data
27
if(bufspace){
28
do{
29
tcp_write(pcb,pb->payload,pb->len,0);
30
printf("\r\nRet val: %d",ret_val);
31
}while(ret_val==ERR_MEM);
32
33
tcp_output(pcb);
34
}
35
tcp_arg(pcb,NULL);
36
tcp_sent(pcb,NULL);
37
tcp_close(pcb);
38
}
39
pbuf_free(pb);
40
}
Habe leider keine brauchbare Beispiele für einen Client gefunden. Der
raw HTTP Server funktioniert dagegen wunderbar. Hat jemand eien Rat oder
ein brauchbares Beispiel?
Mfg
Ahh ich habs, lag an der Windows7 Firewall :(
Nicht desto trotz, hier ist ein funktionierendes Beispiel und zwar wenig
eleganter, die write Funktion habe ich noch mit tcp_sent(pcb,
close_con); Callback erweitert. So wird die Verbindung erst geschlossen,
wenn die Daten von TCP tatsächlich gesendet wurden...
1
staticerr_t
2
close_con(void*arg,structtcp_pcb*pcb,u16_tlen)
3
{
4
tcp_arg(pcb,NULL);
5
tcp_sent(pcb,NULL);
6
tcp_close(pcb);
7
}
8
9
voidclient_write(void)
10
{
11
structtcp_pcb*pcb;
12
structip_addrserver;
13
err_tret_val;
14
intbufspace=0;
15
structpbuf*pb;
16
char*string="Hello TCP World! Lorem ipsum dolor sit amet, consetetur sadipscing elitr\r\n";
17
18
pb=pbuf_alloc(PBUF_TRANSPORT,0,PBUF_REF);
19
pb->payload=string;
20
pb->len=pb->tot_len=strlen(string);
21
22
23
IP4_ADDR(&server,172,16,20,31);
24
25
pcb=tcp_new();// create tcp pcb
26
tcp_bind(pcb,IP_ADDR_ANY,61110);//bind client port for outgoing connection
27
ret_val=tcp_connect(pcb,&server,61111,NULL);//connect to the server
28
29
if(ret_val!=ERR_OK){
30
printf("\r\n Error: %d\n",ret_val);
31
}
32
else{
33
printf("\r\n Connect ok [%d]",ret_val);
34
bufspace=tcp_sndbuf(pcb);// get space which can be used to sent the data