c - Linux 中的 TCP 总线错误

标签 c linux sockets bus-error

我有一个客户端-服务器应用程序,我正在使用 tcp 套接字。根据客户端的 send() 请求,我总是收到总线错误并且程序终止。现在我做了一些维基百科搜索,文章将总线错误归因于(不存在的物理地址、未对齐的内存访问以及访问已被截断的映射文件)。我发送的结构只是三个整数和一个枚举实例,所以我认为对齐不是问题。这是相关的代码片段:

typedef struct _commsg
{
    RequestType requestType;
    int client_pid;
    int request_id;
    int sector;
}ComMsg; 

主要内容:

/* Create a reliable, stream socket using TCP */
if ((sock_id = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    DieWithError("socket() failed\n");

/* Construct the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr));     /* Zero out structure */
echoServAddr.sin_family      = AF_INET;             /* Internet address family */
echoServAddr.sin_addr.s_addr = inet_addr(loopback_addr);   /* Server IP address */
echoServAddr.sin_port        = htons(SERVER_PORT); /* Server port */

/* Establish the connection to the echo server */
if (connect(sock_id, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
    DieWithError("connect() failed");


if (send(sock_id, (void *)&_commsg, sizeof(ComMsg), 0) != sizeof(ComMsg))
    DieWithError("send() sent a different number of bytes than expected");

在服务器端:

recv(clntSocket_fd, &_commsg, sizeof(ComMsg), 0);

提前致谢。

最佳答案

您是否在编译应用程序时出现完整警告(例如,使用 gcc、-Werror)?你应该。总线错误几乎肯定意味着您正在访问错误的指针,在传递给函数的参数中违反了 API,等等。尝试在打开完整警告的情况下进行编译,彻底修复您看到的每个错误,看看是否能解决您的问题.

顺便说一下,对于 TCP 套接字,read(2) 和 write(2) 比 send(2) 和 recv(2) 更常用。

关于c - Linux 中的 TCP 总线错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9416150/

相关文章:

python - 无法在 Ubuntu 中安装 Pytorch

android - Android 4.0.3中如何通过Socket进行通信

c - 反转一个链表递归解-C

java - 创建服务 Java spring ubuntu

linux - 删除除最后两个修改最多的文件夹之外的所有文件夹的脚本?

c# - TCP 连接的可靠性如何?

Java TCP 套接字连接被拒绝

c - 需要有关如何正确使用 strtok() 的建议

使用链表的循环队列

c - 使用多个图像的 GTK2 应用程序内存泄漏