本帖最后由 aifei7320 于 2014-3-10 13:55 编辑
我使用以下两个函数,作为ucos中的任务,想要建立两个tcp连接,但是使用调试工具发现,链接建立瞬间即关闭。而如果只建立任意一个任务,则连接可以使用,http可以登录,tcp也可以发送接收数据。想问问大侠们知不知道是什么原因导致的。
tcpecho_thread(void *arg)
{
struct netconn *conn, *newconn;
err_t err;
LWIP_UNUSED_ARG(arg);
/* Create a new connection identifier. */
conn = netconn_new(NETCONN_TCP);
/* Bind connection to well known port number 7. */
netconn_bind(conn, NULL, 7);
/* Tell connection to go into listening mode. */
netconn_listen(conn);
while (1) {
/* Grab new connection. */
err = netconn_accept(conn, &newconn);
/*printf("accepted new connection %p\n", newconn);*/
/* Process the new connection. */
if (err == ERR_OK) {
struct netbuf *buf;
void *data;
u16_t len;
while ((err = netconn_recv(newconn, &buf)) == ERR_OK) {
/*printf("Recved\n");*/
do {
netbuf_data(buf, &data, &len);
err = netconn_write(newconn, data, len, NETCONN_COPY);
#if 0
if (err != ERR_OK) {
printf("tcpecho: netconn_write: error \"%s\"\n", lwip_strerr(err));
}
#endif
} while (netbuf_next(buf) >= 0);
netbuf_delete(buf);
}
/*printf("Got EOF, looping\n");*/
/* Close connection and discard connection identifier. */
netconn_close(newconn);
netconn_delete(newconn);
}
}
}
static void
http_server_netconn_thread(void *arg)
{
struct netconn *conn, *newconn;
err_t err;
LWIP_UNUSED_ARG(arg);
/* Create a new TCP connection handle */
conn = netconn_new(NETCONN_TCP);
LWIP_ERROR("http_server: invalid conn", (conn != NULL), return;);
/* Bind to port 80 (HTTP) with default IP address */
netconn_bind(conn, NULL, 80);
/* Put the connection into LISTEN state */
netconn_listen(conn);
do {
err = netconn_accept(conn, &newconn);
if (err == ERR_OK) {
http_server_netconn_serve(newconn);
}
} while(err == ERR_OK);
LWIP_DEBUGF(HTTPD_DEBUG,
("http_server_netconn_thread: netconn_accept received error %d, shutting down",
err));
netconn_close(conn);
netconn_delete(conn);
}
|