[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lwip-users] Failed to send TCP data
From: |
Gavin |
Subject: |
[lwip-users] Failed to send TCP data |
Date: |
Mon, 15 Sep 2014 19:46:21 -0700 (MST) |
Hi,
I have one question about TCP sending.
I refer to tcpecho sample and rewrite to a client application for TCP
sending.
In my case, it only can send data twice successful and always failed at the
third time.
Here is my test code.
void test_package(void)
{
struct lan_state *ls;
struct pbuf *cc;
char *str = "ggg";
if ((ls = (struct lan_state *)mem_malloc(sizeof(struct lan_state))) ==
NULL)
return;
if ((cc = (struct pbuf *)mem_malloc(sizeof(struct pbuf))) == NULL)
return;
cc->payload = str;
cc->tot_len = 3;
cc->len = 3;
ls->state = LS_ACCEPTED;
ls->pcb = g_pcb;
ls->retries = 0;
ls->p = cc;
tcp_sent(g_pcb, LAN_sent);
LAN_send(g_pcb, ls);
}
void LAN_send(struct tcp_pcb *pcb, struct lan_state *ls)
{
struct pbuf *ptr;
err_t wr_err = ERR_OK;
UARTprintf("LAN_send++\n");
while ((wr_err == ERR_OK) && (ls->p != NULL) && (ls->p->len <=
tcp_sndbuf(pcb)))
{
ptr = ls->p;
/* enqueue data for transmission */
wr_err = tcp_write(pcb, ptr->payload, ptr->len, 1);
if (wr_err == ERR_OK)
{
u16_t plen;
u8_t freed;
plen = ptr->len;
/* continue with next pbuf in chain (if any) */
ls->p = ptr->next;
if(ls->p != NULL)
{
/* new reference! */
pbuf_ref(ls->p);
}
/* chop first pbuf from chain */
do
{
/* try hard to free pbuf */
freed = pbuf_free(ptr);
}
while(freed == 0);
/* we can read more data now */
//tcp_recved(pcb, plen);
}
else if(wr_err == ERR_MEM)
{
/* we are low on memory, try later / harder, defer to
poll */
ls->p = ptr;
}
else
{
/* other problem ?? */
}
}
UARTprintf("LAN_send--\n");
}
err_t LAN_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
{
struct lan_state *ls;
LWIP_UNUSED_ARG(len);
UARTprintf("LAN_sent++\n");
ls = (struct lan_state *)arg;
ls->retries = 0;
if(ls->p != NULL)
{
/* still got pbufs to send */
tcp_sent(pcb, LAN_sent);
LAN_send(pcb, ls);
}
else
{
/* no more pbufs to send */
if(ls->state == LS_CLOSING)
{
LAN_end(pcb, ls);
}
}
UARTprintf("LAN_sent--\n");
return ERR_OK;
}
err_t LAN_Init(void)
{
err_t err;
struct lan_state *ls;
struct ip_addr ipaddr;
struct ip_addr *resolvedIP;
//connect to server
IP4_ADDR(&ipaddr, uiServerIP[0], uiServerIP[1], uiServerIP[2],
uiServerIP[3]);
if ((ls = (struct lan_state *)mem_malloc(sizeof(struct lan_state))) ==
NULL)
return ERR_MEM;
if ((g_pcb = tcp_new()) == NULL)
{
mem_free(ls);
return ERR_MEM;
}
ls->state = LS_ACCEPTED;
ls->pcb = g_pcb;
ls->retries = 0;
ls->p = NULL;
tcp_arg(g_pcb, ls);
tcp_err(g_pcb, LAN_err);
tcp_recv(g_pcb, LAN_recv);
tcp_sent(g_pcb, LAN_sent);
tcp_poll(g_pcb, LAN_poll, 10);
err = tcp_connect(g_pcb, &ipaddr, SERVER_PORT, NULL);
if (err != ERR_OK)
{
mem_free(ls);
tcp_abort(g_pcb);
}
return err;
}
Is it related with mem allocation or else?
Does anyone have suggestion?
thanks
Gavin
--
View this message in context:
http://lwip.100.n7.nabble.com/Failed-to-send-TCP-data-tp23259.html
Sent from the lwip-users mailing list archive at Nabble.com.
- [lwip-users] Failed to send TCP data,
Gavin <=