我正在尝试用 c 和套接字制作一个简单的聊天程序。当我在做了几件事后运行它时,它说程序收到信号 EXC_BAD_ACCESS

发布于 2024-12-02 13:16:48 字数 387 浏览 1 评论 0原文

printf("what is your name?");
    gets(send_name);

    strcpy(send_name2, strcat("You are connected to ", send_name));
    send(connected, send_name2, strlen(send_name2), 0);

另一个可执行文件没有接收我发送的内容...

nbytes_recieved = recv(sock, recv_name, 50 ,0);
recv_name[nbytes_recieved] = '\0';

这是我在客户端代码中使用的代码,让它接收字符串。

谢谢, 西德

printf("what is your name?");
    gets(send_name);

    strcpy(send_name2, strcat("You are connected to ", send_name));
    send(connected, send_name2, strlen(send_name2), 0);

The other executable is not receiving what i sent...

nbytes_recieved = recv(sock, recv_name, 50 ,0);
recv_name[nbytes_recieved] = '\0';

This is the code I used in the client code to let it receive the string.

Thanks,
Sidd

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹉夏雨初晴づ 2024-12-09 13:16:48

strcat 期望将可写缓冲区作为其第一个参数。您提供给它的是一个常量字符串,可能存储在进程的只读区域中的某个位置。该函数尝试在该常量字符串之后写入,这会导致内存访问冲突。

strcat expects as its first argument a writeable buffer. What you give to it is a constant string, probably stored somewhere in a read-only area of the process. The function attempts to write right after this constant string, which results in a memory access violation.

纸短情长 2024-12-09 13:16:48

EXC_BAD_ACCESS 相当于分段错误,通常是 NULL 指针取消引用,或访问未分配给进程的内存。如果 recv_name 太小而无法容纳接收到的所有字节加上终止的“\0”,则可能会导致这种情况。

要开始调试,请使用调试符号进行编译,并开始在该代码中的各个点检查 recv_name 的内容。

EXC_BAD_ACCESS is equivalent of a segmentation fault, usually a NULL pointer dereference, or accessing memory not allocated to the process. This could be caused if recv_name is too small to hold all the bytes received plus the terminating '\0'.

To get started debugging, compile with debugging symbols, and start examining the contents of recv_name at various points in that code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文