Hi,
I'm trying to create a comunication between a client and an echo server;
when the client start the connection the server respond well (I seem),
but when I try to send a message from the client, the client turn out 
unconnected,
and I don't understand where is the error.

this is the source of my client:

#include <stdio.h>
#include <conio.h>

#include "lwip/debug.h"

#include "lwip/mem.h"
#include "lwip/memp.h"
#include "lwip/sys.h"

#include "lwip/stats.h"

#include "lwip/ip.h"
#include "lwip/ip_frag.h"
#include "lwip/udp.h"
#include "lwip/tcp.h"

#include "clientif.h"

#include <stdio.h>
#include <conio.h>
#include <string.h>

void cli_init(struct tcp_pcb *pcb);
void srv_Idle(int* T);
void cli_send(struct tcp_pcb *pcb, struct pbuf *P);
void cli_close_conn(struct tcp_pcb *pcb);


err_t cli_sent(void *arg, struct tcp_pcb *pcb, u16_t len);  
err_t cli_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err);
err_t cli_connected(void *arg, struct tcp_pcb *newpcb, err_t err);
err_t cli_poll(void *arg, struct tcp_pcb *pcb);
void cli_err(void *arg, err_t err);

int flag;

int main(int argc, char **argv)
{
 struct ip_addr IP, NetMask, GateWay;
 struct netif Net;
 struct tcp_pcb *pcb;
 struct pbuf *P;
 char S[10];

 int Timer = 250;

 mem_init();
 memp_init();
 pbuf_init(); 
 netif_init();
 ip_init();
 tcp_init();
 printf("stack TCP/IP initializzato.\n");
  
 IP4_ADDR(&GateWay, 192,168,0,2);
 IP4_ADDR(&IP, 192,168,0,1);
 IP4_ADDR(&NetMask, 255,255,255,0);

 netif_add(&Net, &IP, &NetMask, &GateWay, NULL, clientif_init, ip_input);
  
 netif_set_default(&Net);

 cli_init(pcb);

 printf("Client partito.\n");
 
 do {
   flag = 0;
   gets(S);
   
   P = pbuf_alloc(PBUF_TRANSPORT, sizeof(S) + PBUF_TRANSPORT_HLEN, PBUF_RAM);
   memcpy(P->payload, S, sizeof(S));
       
   cli_send(pcb, P);

     clientif_input(&Net);
    } while (strcmp(S, "exit"));

 return 0;
}

void cli_init(struct tcp_pcb *pcb)
{
 struct ip_addr IPDest;
 
 IP4_ADDR(&IPDest, 192, 168, 0, 2);

 pcb = tcp_new();
 tcp_connect(pcb, &IPDest, 5000, &cli_connected);
}
void cli_send(struct tcp_pcb *pcb, struct pbuf *P)
{
 struct pbuf *B;
 int E;
  
 do {
     B = P;
     P = pbuf_dechain(B);
  printf("Parametri di tcp_write: %i %i %s", B, B->len, B->payload);
     if (E = tcp_write(pcb, B->payload, B->len, 1) != ERR_OK)
        {
         pbuf_chain(B, P);
         P = B;
   printf("invio non riuscito\nerrore :%d", E);
         return;
        }

    } while (P != NULL);   
}

void cli_close_conn(struct tcp_pcb *pcb)
{
 tcp_close(pcb);
}

err_t cli_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
{
 printf("Pacchetto di lunghezza %i inviato con successo\n", len);
 return ERR_OK;
}

err_t cli_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
 printf("Pacchetto ricevuto\n %s", (char*)p);

 /**/
 tcp_recved(pcb, p->len);
 pbuf_free(p);
 /**/

 flag = 1;
}

err_t cli_connected(void *arg, struct tcp_pcb *newpcb, err_t err)
{
 tcp_sent(newpcb, cli_sent);
 tcp_recv(newpcb, cli_recv);
 tcp_poll(newpcb, cli_poll, 2);
 tcp_err(newpcb, cli_err);
 printf("Connessione con il server riuscita\n");
}

err_t cli_poll(void *arg, struct tcp_pcb *pcb)
{
 return ERR_OK;
}

void cli_err(void *arg, err_t err)
{
 printf("Errore = %i\n", err);
}

Thanks
Luigi
_______________________________________________
lwip-users mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/lwip-users

Reply via email to