原程序,通过下位机判断接收上位机数据的长度,选择发送给上位机什么数据,我想实现如果上位机不断开网络连接,下位机一直发送数据,直到上位机断开网络连接。
我之前改动会死循环,
哪位大神来帮我改动一下,谢谢,不胜感激
static void tcpecho_thread(void *arg)
{
struct netconn *conn, *newconn;
err_t err, accept_err;
struct netbuf *buf;
void *data="abcdef";
u16_t len;
err_t recv_err;
int m;
LWIP_UNUSED_ARG(arg);
/* Create a new connection identifier. */
conn = netconn_new(NETCONN_TCP);
if (conn!=NULL)
{
/* Bind connection to well known port number 7. */
err = netconn_bind(conn, NULL, 7);//目标端口号,以太网通讯
if (err == ERR_OK)
{
/* Tell connection to go into listening mode. */
netconn_listen(conn);
while (1)
{
/* Grab new connection. */
accept_err = netconn_accept(conn, &newconn);
/* Process the new connection. */
if (accept_err == ERR_OK)
{
recv_err = netconn_recv(newconn, &buf);
while ( recv_err == ERR_OK)
{
do
{
netbuf_data(buf, &data, &len);
switch(len)
{
case 1:for(m=1;m<=10;m++)
{
netconn_write(newconn, e1, 12, NETCONN_COPY);
}//仅实现发送十个数据
break;
case 2:netconn_write(newconn, e2, 12, NETCONN_COPY);
break;
case 3:netconn_write(newconn, e3, 12, NETCONN_COPY);
break;
case 4:netconn_write(newconn, e4, 12, NETCONN_COPY);
break;
case 5:netconn_write(newconn, e5, 12, NETCONN_COPY);
break;
case 6:for(m=1;m<=10;m++)
{
netconn_write(newconn, e6, 12, NETCONN_COPY);
}
break;
case 7:netconn_write(newconn, b, 66, NETCONN_COPY);
break;
case 8:for(m=1;m<=10;m++)
{
netconn_write(newconn, b, 66, NETCONN_COPY);
}
break;
}
}
while (netbuf_next(buf) >= 0);
netbuf_delete(buf);
recv_err = netconn_recv(newconn, &buf);
}
/* Close connection and discard connection identifier. */
netconn_close(newconn);
netconn_delete(newconn);
}
}
}
else
{
netconn_delete(newconn);
printf(" can not bind TCP netconn");
}
}
else
{
printf("can not create TCP netconn");
}
}
一周热门 更多>