我正在尝试用 c 和套接字制作一个简单的聊天程序。当我在做了几件事后运行它时,它说程序收到信号 EXC_BAD_ACCESS
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.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 ifrecv_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.