lwip-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lwip-devel] Simple data transfer with lwip


From: RedCollarPanda
Subject: [lwip-devel] Simple data transfer with lwip
Date: Thu, 19 Jun 2014 07:04:14 -0700 (PDT)

Good day! I'm new to libiwip and I really need help! 

Got a task - i have to make 2 applications with lwip library's raw (low-lvl)
interfases - a client and a server (both tcp).
OS is desktop Debian x64. I've compilled this lib.
1) Server
Must listen needed port and get messages from connections, printing them on
the screen.

2) Client
Must connect to the servet and only send some message.

For now I have done server's part like this:

char mydata[1024];


static void close_conn(struct tcp_pcb *pcb){
      tcp_arg(pcb, NULL);
      tcp_sent(pcb, NULL);
      tcp_recv(pcb, NULL);
      tcp_close(pcb);
}

static err_t echo_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t
err){
      int i;
      int len;
      char *pc;

      if (err == ERR_OK && p != NULL) {
            /* Inform TCP that we have taken the data. */
            tcp_recved(pcb, p->tot_len);

            //pointer to the pay load
            pc=(char *)p->payload;

            //size of the pay load
            len =p->tot_len;

            //copy to our own buffer
            for (i=0; i<len; i++)mydata[i]= pc[i];

             //Close TCP when receiving &quot;X&quot;
            if (mydata[0]=='X')close_conn(pcb);

           //Free the packet buffer
            pbuf_free(p);

            //check output buffer capacity
            if (len >tcp_sndbuf(pcb)) len= tcp_sndbuf(pcb);
            //Send out the data
            err = tcp_write(pcb, mydata, len, 0);
            tcp_sent(pcb, NULL); /* No need to call back */
      } else {
            pbuf_free(p);
      }

      if (err == ERR_OK && p == NULL) {
            close_conn(pcb);
      }
      return ERR_OK;
}

static err_t echo_accept(void *arg, struct tcp_pcb *pcb, err_t err){
          std::cout << __LINE__ << std::endl;
      LWIP_UNUSED_ARG(arg);
      LWIP_UNUSED_ARG(err);
      tcp_setprio(pcb, TCP_PRIO_MIN);
      tcp_recv(pcb, echo_recv);
      tcp_err(pcb, NULL); //Don't care about error here
      tcp_poll(pcb, NULL, 4); //No polling here
      std::cout << __LINE__ << std::endl;
      return ERR_OK;
}



signed char  connected_callback () {

}

int main() {

          lwip_init();

          struct tcp_pcb *ptel_pcb;
          ptel_pcb = tcp_new();


          std::cout << "ptel_pcb = " << ptel_pcb << std::endl;

          struct ip_addr ipaddr;
          IP4_ADDR(&ipaddr,  178, 17, 9, 42);
          tcp_bind(ptel_pcb, &ipaddr, 6666 );

         while (1){
                 // std::cout << __LINE__ << std::endl;
                  sleep(1);
                  //std::cout << __LINE__ << std::endl;
                  std::cout <<  "ptel_pcb->state = " << ptel_pcb->state << 
std::endl;
                  if(ptel_pcb->state ==LISTEN) {
                          tcp_accept(ptel_pcb, echo_accept);
                  }
                  else {
                          ptel_pcb = tcp_listen(ptel_pcb);
                  }

          }

              std::cout << __LINE__ << std::endl;



        return 0;
}




and want to make client's part like this

                        lwip_init();
                        struct tcp_pcb *pcb;
                        struct tcp_pcb *tcp_pcb;
                        struct ip_addr ipaddr;
                err_t err = ERR_CONN;




                IP4_ADDR(&ipaddr,  127, 0, 0, 1);

                        tcp_pcb = tcp_new();
                        pcb = tcp_new();
                        std::cout << "tcp_pcb =  " << tcp_pcb << std::endl;
                        std::cout << "pcb =  " << pcb << std::endl;
                tcp_bind(pcb,IP_ADDR_ANY, 54783);
                std::cout << __LINE__ << std::endl;

                err = tcp_connect(tcp_pcb, &ipaddr, 54783, connected_callback);
            if (err != ERR_OK)
                   {
                           std::cout << "tcp_connect returned error: "<<  err <<
"\n\r";

                   }



That all not working... Can some 1 tell me why? Or m.b. give me some
examlpes?



--
View this message in context: 
http://lwip.100.n7.nabble.com/Simple-data-transfer-with-lwip-tp22803.html
Sent from the lwip-devel mailing list archive at Nabble.com.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]