开发者

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

开发者 https://www.devze.com 2023-04-02 01:07 出处:网络
printf(\"what is your name?\"); gets(send_name); strcpy(send_name2, strcat(\"You are connected to \", send_name));
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 th开发者_如何学运维e client code to let it receive the string.

Thanks, Sidd


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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号