简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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