NetConn LWIP TCP/IP客户端[Freertos]
我使用下面的代码为嵌入式项目创建一个TCP客户端:
NetConn和NetBuf Creation
static struct netconn *conn;
static struct netbuf *buf;
static ip_addr_t *addr, dest_addr;
static unsigned short port, dest_port;
char msgc[100];
char smsgc[200];
int indx = 0;
void tcpinit_thread()
{
err_t connect_error;
/* Create a new connection identifier. */
conn = netconn_new(NETCONN_TCP);
if (conn!=NULL)
{
/* The desination IP adress of the computer */
IP_ADDR4(&dest_addr, 192, 168, 225, 20);
dest_port = 5656; // server port
/* Connect to the TCP Server */
connect_error = netconn_connect(conn, &dest_addr, dest_port);
// If the connection to the server is established, the following will continue, else delete the connection
if (connect_error == ERR_OK)
{
printf("TCP client connected on port 5656 \r\n");
while (1)
{
/* wait until the data is sent by the server */
if (netconn_recv(conn, &buf) == ERR_OK)
{
/* Extract the address and port in case they are required */
addr = netbuf_fromaddr(buf); // get the address of the client
port = netbuf_fromport(buf); // get the Port of the client
/* If there is some data remaining to be sent, the following process will continue */
do
{
strncpy (msgc, buf->p->payload, buf->p->len); // get the message from the server
// Or modify the message received, so that we can send it back to the server
sprintf (smsgc, "\"%s\" was sent by the Server\n", msgc);
printf("data received from server \r\n");
// send the data to the TCP Server
tcpsend (smsgc);
memset (msgc, '\0', 100); // clear the buffer
}
while (netbuf_next(buf) >0);
netbuf_delete(buf);
}
}
}
else
{
/* Close connection and discard connection identifier. */
netconn_close(conn);
netconn_delete(conn);
printf("Client disconnected \r\n");
}
}
}
来发送数据
void tcpsend (char *data)
{
// send the data to the connected connection
netconn_write(conn, data, strlen(data), NETCONN_COPY);
}
INIT inith inith thread
void tcpclient_init (void)
{
sys_thread_new("tcpinit_thread", tcpinit_thread, NULL, 1024,DEFAULT_THREAD_PRIO);
}
问题,即使它在指定的端口上运行,我也无法连接到服务器。 我总是得到错误13:连接中止。
所以我只是想知道我做错了什么?
I used the code below to create a TCP client for an embedded project :
netconn and netbuf creation
static struct netconn *conn;
static struct netbuf *buf;
static ip_addr_t *addr, dest_addr;
static unsigned short port, dest_port;
char msgc[100];
char smsgc[200];
int indx = 0;
void tcpinit_thread()
{
err_t connect_error;
/* Create a new connection identifier. */
conn = netconn_new(NETCONN_TCP);
if (conn!=NULL)
{
/* The desination IP adress of the computer */
IP_ADDR4(&dest_addr, 192, 168, 225, 20);
dest_port = 5656; // server port
/* Connect to the TCP Server */
connect_error = netconn_connect(conn, &dest_addr, dest_port);
// If the connection to the server is established, the following will continue, else delete the connection
if (connect_error == ERR_OK)
{
printf("TCP client connected on port 5656 \r\n");
while (1)
{
/* wait until the data is sent by the server */
if (netconn_recv(conn, &buf) == ERR_OK)
{
/* Extract the address and port in case they are required */
addr = netbuf_fromaddr(buf); // get the address of the client
port = netbuf_fromport(buf); // get the Port of the client
/* If there is some data remaining to be sent, the following process will continue */
do
{
strncpy (msgc, buf->p->payload, buf->p->len); // get the message from the server
// Or modify the message received, so that we can send it back to the server
sprintf (smsgc, "\"%s\" was sent by the Server\n", msgc);
printf("data received from server \r\n");
// send the data to the TCP Server
tcpsend (smsgc);
memset (msgc, '\0', 100); // clear the buffer
}
while (netbuf_next(buf) >0);
netbuf_delete(buf);
}
}
}
else
{
/* Close connection and discard connection identifier. */
netconn_close(conn);
netconn_delete(conn);
printf("Client disconnected \r\n");
}
}
}
to send data
void tcpsend (char *data)
{
// send the data to the connected connection
netconn_write(conn, data, strlen(data), NETCONN_COPY);
}
init the thread
void tcpclient_init (void)
{
sys_thread_new("tcpinit_thread", tcpinit_thread, NULL, 1024,DEFAULT_THREAD_PRIO);
}
problem is that i cannot connect to the server, even if its running on the specified port.
i get always err 13 : connection aborted.
So I'm just wondering if there's anything obvious I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论