简体   繁体   English

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

[英]I am trying to make a simple chat program with c and sockets. When I run it after a couple of things it says program received signal 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);

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 谢谢,Sidd

strcat expects as its first argument a writeable buffer. strcat希望将可写缓冲区作为其第一个参数。 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 is equivalent of a segmentation fault, usually a NULL pointer dereference, or accessing memory not allocated to the process. EXC_BAD_ACCESS等同于分段错误,通常是NULL指针取消引用或访问未分配给进程的内存。 This could be caused if recv_name is too small to hold all the bytes received plus the terminating '\\0'. 如果recv_name太小而无法容纳接收到的所有字节以及终止符'\\ 0',则可能导致这种情况。

To get started debugging, compile with debugging symbols, and start examining the contents of recv_name at various points in that code. 要开始调试,请使用调试符号进行编译,然后在该代码中的各个点开始检查recv_name的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM