Hi,
I am making a program that receives TCP packets from ethernet using the lwip library. My code is based off the echo example (nortos). The function is to recieve data from TCP and send it through UART serial and vice versa. I have managed to use the echo example and tweak it to recieve packets, process it and send it through UART however I am having trouble Sending data to the same TCP port using the tcp write function. What would be the simplest way to send a string of data to the same tcp port. The only adjustments I made is to the echo_recv function where I took the incoming packet, processes it and then cleared the buffer.
err_t
echo_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
struct echo_state *es;
err_t ret_err;
LWIP_ASSERT("arg != NULL",arg != NULL);
es = (struct echo_state *)arg;
if (p == NULL)
{
/* remote host closed connection */
es->state = ES_CLOSING;
if(es->p == NULL)
{
/* we're done sending, close it */
echo_close(tpcb, es);
}
else
{
/* we're not done yet */
tcp_sent(tpcb, echo_sent);
echo_send(tpcb, es);
}
ret_err = ERR_OK;
}
else if(err != ERR_OK)
{
/* cleanup, for unkown reason */
if (p != NULL)
{
es->p = NULL;
pbuf_free(p);
}
ret_err = err;
}
else if(es->state == ES_ACCEPTED)
{
/* first data chunk in p->payload */
es->state = ES_RECEIVED;
char string1[10];
memcpy(string1,p->payload, p->len);
int i = 0;
for(i = 0; i <= 10 ; i++){
coordinator.modemManager.buffer.push(string1[i]);
}
tcp_recved(tpcb, p->tot_len);
es->p = NULL;
pbuf_free(p);
ret_err = ERR_OK;
/* store reference to incoming pbuf (chain) */
// es->p = p;
/* install send completion notifier */
// tcp_sent(tpcb, echo_sent);
// echo_send(tpcb, es);
// ret_err = ERR_OK;
}
else if (es->state == ES_RECEIVED)
{
/* read some more data */
if(es->p == NULL)
{
es->p = p;
// tcp_sent(tpcb, echo_sent);
// echo_send(tpcb, es);
char string2[10];
memcpy(string2,p->payload, p->len);
int i = 0;
for(i = 0; i <= 10 ; i++){
coordinator.modemManager.buffer.push(string2[i]);
}
tcp_recved(tpcb, p->tot_len);
es->p = NULL;
pbuf_free(p);
ret_err = ERR_OK;
}
else
{
struct pbuf *ptr;
/* chain pbufs to the end of what we recv'ed previously */
ptr = es->p;
pbuf_chain(ptr,p);
}
ret_err = ERR_OK;
}
else if(es->state == ES_CLOSING)
{
/* odd case, remote side closing twice, trash data */
tcp_recved(tpcb, p->tot_len);
es->p = NULL;
pbuf_free(p);
ret_err = ERR_OK;
}
else
{
/* unkown es->state, trash data */
tcp_recved(tpcb, p->tot_len);
es->p = NULL;
pbuf_free(p);
ret_err = ERR_OK;
}
return ret_err;
}