我的编程方式出现“总线错误”。我正在学习如何使具有链表的客户端/服务器。错误总线仅在我返回parse_message (...) = NULL
时出现。
listen_client函数在线程中,我不知道它是否会影响错误。如果您需要更多详细信息/其他代码,请问我。
谢谢你,晚上好。
static t_message *parse_message(char *buffer)
{
int id;
id = atoi(buffer);
printf("ID: %d\n", id);
return (NULL); // for sample
}
static char *read_message(char *data)
{
static char *buffer = NULL;
char *message = NULL;
char *ptr;
size_t size;
size = strlen(data);
if (buffer)
size += strlen(buffer);
if (!(buffer = realloc(buffer, size)))
return (NULL);
strcat(buffer, data);
if ((ptr = strstr(buffer, "\r\n")))
{
size = (ptr - buffer);
if (!(message = (char*)malloc(sizeof(char) * (size + 1))))
return (NULL);
strncpy(message, buffer, size);
ptr += 2;
if (!(*ptr))
buffer = NULL;
else if (!(buffer = strdup(ptr)))
return (NULL);
}
return (message);
}
void listen_client(t_args *args)
{
ssize_t count;
char buffer[BUFF_SIZE + 1] = { 0 };
char *msg_str;
t_message *msg;
while ((count = recv(args->client->s, buffer, BUFF_SIZE, 0)))
{
printf("===> %s\n", buffer);
if (!(msg_str = read_message(buffer)))
{
continue ;
}
if (!(msg = parse_message(msg_str)))
{
// bus error after continue ;
continue ;
}
handle_message();
}