ETH004 example with TCP

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Not applicable
Hi there,

In the last few weeks i`ve tried to understand the procedure of the ETH04 App and to communicate via UDP. I`m still having problems to understand the difference between UDP und TCP. It would be useful to have the same APP realized in TCP.
Perhaps someone can show me how this realisation looks like?

Best regards
J_o_e

Here`s the code from the ETH04 APP:

#include //Declarations from DAVE3 Code Generation (includes SFR declaration)

/*Local port waiting for packets*/
#define LOCAL_PORT_RECEIVE (45555)
/*Local port used to echo back the packet*/
#define LOCAL_PORT_TRANSMIT (45174)
/*Destination port of echoed packet*/
#define TRANSMIT_PORT_DESTINATION (45175)


struct udp_pcb * udp_init_comm(unsigned int port) {
/* start the UDP server */
struct udp_pcb *pcb;
/* create new UDP PCB structure */
pcb = udp_new();
udp_bind(pcb, IP_ADDR_ANY, port);
return pcb;

}



void udp_receive_handler (void * arg, struct udp_pcb * upcb, struct pbuf * p, struct ip_addr * addr, u16_t port)
{
struct pbuf * p_out;
//As handler argument is expected the pcb that shall be used for send back the data
// Allocate a buffer to store data to be transmitted
p_out = pbuf_alloc(PBUF_TRANSPORT,p->tot_len,PBUF_RAM);
//Copy received data to transmit buffer
pbuf_copy(p_out,p);
//Send back data to same ip that sent the data but on different port
udp_sendto((struct udp_pcb *)arg,p_out,addr,TRANSMIT_PORT_DESTINATION);
//Free allocated buffer including the buffer of received data
pbuf_free(p_out);
pbuf_free(p);

}



int main(void)
{
struct udp_pcb *pcb_tr; //lwip pcb struct binded to transmit port
struct udp_pcb *pcb_rec; //lwip pcb struct binded to receive port

DAVE_Init(); // Initialization of DAVE Apps
lwIPStack_init(); //Start Lwip stack

//Create
pcb_tr=udp_init_comm(LOCAL_PORT_TRANSMIT);
pcb_rec=udp_init_comm(LOCAL_PORT_RECEIVE);

//Register an handler on receive packet event
udp_recv(pcb_rec,udp_receive_handler,pcb_tr);


while(1)
{

}
return 0;
}
0 Likes
0 Replies