如果输入字符串长于 19 字节,LwIP 套接字上的 UDP 服务器将陷入硬故障
UDP server on LwIP socket falls into hard fault if input string longer than 19 bytes
我认为是因为空指针异常。我的嵌入式系统由STM32F4、FreeRTOS和LwIP组成。核心取自 ST 示例。下面是简单UDP服务器的线程代码。
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include "lwip/sockets.h"
#include "./udpCLI.h"
//#include "Sledge.h" //for assert_amsg(), which print what and where fails before stop execution
static void udpCLI_thread(void *arg)
{
int sockfd; // socket file descriptor
const uint16_t serverport = UDP_PORT_NUMBER_CLI;
struct sockaddr_in serveraddr = {0}, clientaddr = {0};
socklen_t addrlen;// = sizeof(struct sockaddr_in);
const uint16_t bufin_SIZE = 200, bufout_SIZE = 1024;
char *bufin = malloc(bufin_SIZE); assert_amsg(bufin != NULL); // stops execution with printing message "ASSERT FAILED "bufin != NULL" in file udp_cli.c:42" in stdout/stderr
char *bufout = malloc(bufout_SIZE); assert_amsg(bufout != NULL);
LWIP_UNUSED_ARG(arg); //(void)arg;
debugf2( "udpCLI_thread"NEWLINE );
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
assert_msg("udpCLI_thread: ERROR opening socket", sockfd>=0);
/*
* build the server's Internet address
*/
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(serverport);
/*
* bind: associate the socket with a port
*/
if( bind(sockfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0 )
error("udpCLI_thread: ERROR on binding");
// main loop: wait for a datagram, then echo it
while(1)
{
addrlen = sizeof(struct sockaddr_in); // THIS DID NOT HELP
// recvfrom: receive a UDP datagram from a client
int n = recvfrom(sockfd, bufin, bufin_SIZE, /*flags*/0, (struct sockaddr*)&clientaddr, &addrlen);
// handle rusult
//if (n < 0)
// future process and response
//....
}
如果输入消息长度超过 19 个字节,程序控制永远不会从 recvfrom()
函数中 returns。并且从不 returns from function sys_arch_mbox_fetch(&conn->recvmbox, &buf, 0);
in function netconn_recv_data()
in file api_lib.c
at line 371
.
请帮助解决这个问题。
那是我的错误。问题是在另一个任务中错误的内存分配和释放。
谢谢所有同情的人
我认为是因为空指针异常。我的嵌入式系统由STM32F4、FreeRTOS和LwIP组成。核心取自 ST 示例。下面是简单UDP服务器的线程代码。
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include "lwip/sockets.h"
#include "./udpCLI.h"
//#include "Sledge.h" //for assert_amsg(), which print what and where fails before stop execution
static void udpCLI_thread(void *arg)
{
int sockfd; // socket file descriptor
const uint16_t serverport = UDP_PORT_NUMBER_CLI;
struct sockaddr_in serveraddr = {0}, clientaddr = {0};
socklen_t addrlen;// = sizeof(struct sockaddr_in);
const uint16_t bufin_SIZE = 200, bufout_SIZE = 1024;
char *bufin = malloc(bufin_SIZE); assert_amsg(bufin != NULL); // stops execution with printing message "ASSERT FAILED "bufin != NULL" in file udp_cli.c:42" in stdout/stderr
char *bufout = malloc(bufout_SIZE); assert_amsg(bufout != NULL);
LWIP_UNUSED_ARG(arg); //(void)arg;
debugf2( "udpCLI_thread"NEWLINE );
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
assert_msg("udpCLI_thread: ERROR opening socket", sockfd>=0);
/*
* build the server's Internet address
*/
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(serverport);
/*
* bind: associate the socket with a port
*/
if( bind(sockfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0 )
error("udpCLI_thread: ERROR on binding");
// main loop: wait for a datagram, then echo it
while(1)
{
addrlen = sizeof(struct sockaddr_in); // THIS DID NOT HELP
// recvfrom: receive a UDP datagram from a client
int n = recvfrom(sockfd, bufin, bufin_SIZE, /*flags*/0, (struct sockaddr*)&clientaddr, &addrlen);
// handle rusult
//if (n < 0)
// future process and response
//....
}
如果输入消息长度超过 19 个字节,程序控制永远不会从 recvfrom()
函数中 returns。并且从不 returns from function sys_arch_mbox_fetch(&conn->recvmbox, &buf, 0);
in function netconn_recv_data()
in file api_lib.c
at line 371
.
请帮助解决这个问题。
那是我的错误。问题是在另一个任务中错误的内存分配和释放。
谢谢所有同情的人