stm32 LwIP TCP Client

Gast #2518598
Lesenswert?

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
void client_write(void)
2
{
3
  struct tcp_pcb *pcb;
4
  struct ip_addr server;
5
  err_t ret_val;
6
  int bufspace = 0;
7
  struct pbuf *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
Gast #2518656
Lesenswert?

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
static err_t
2
close_con(void *arg, struct tcp_pcb *pcb, u16_t len)
3
{
4
  tcp_arg(pcb, NULL);
5
  tcp_sent(pcb, NULL);
6
  tcp_close(pcb);
7
}
8

9
void client_write(void)
10
{
11
  struct tcp_pcb *pcb;
12
  struct ip_addr server;
13
  err_t ret_val;
14
  int bufspace = 0;
15
  struct pbuf *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
35
     if (bufspace) {
36
       do {
37
          tcp_write(pcb, pb->payload,pb->len, 0);
38
         printf("\r\nRet val: %d", ret_val);
39
        } while (ret_val == ERR_MEM);
40

41
       tcp_output(pcb);
42
       tcp_sent(pcb, close_con);
43
     }
44
  }
45
  pbuf_free(pb);
46

47
}

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren