简体   繁体   中英

Segmentation fault using the buffer recived from recv() tcpServer c

I'm trying to create an application client/server in c, but after recv() when I try to use the buffer received the program give segmentation fault (core dump created), I can't work out it. This is my code at server side:

int req_socket_id;
int comunication_socket_id;
struct sockaddr_in server_add;
struct sockaddr_in client_add;
socklen_t client_add_size;
char buffer[255];
//char mess[1024];
int i, n;
//int index;
unsigned int num;


// AF_INET = famiglia di indirizzi iPv4
req_socket_id = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if(req_socket_id<0)
{
    printf("Socket initialization failed !!");
    return -1;
}
e
memset(&server_add, 0, sizeof(server_add)); // azzeramento struttura
server_add.sin_family = AF_INET; // dominio indirizzi IP
server_add.sin_addr.s_addr = 0; // indirizzo IP
server_add.sin_port = htons(23165); // numero di porta UDP

if(bind(req_socket_id, (struct sockaddr*) &server_add, sizeof(server_add)) < 0)
{
    perror("\nErrore associazione porta e socket!\n");
    close(req_socket_id);
    return -1;
}

if(listen(req_socket_id, 1)<0)
{
    perror("\nErrore nell'ascolto!\n");
    close(req_socket_id);
    return -1;
}

while(1)
{
    client_add_size = sizeof(client_add);
    comunication_socket_id = accept(req_socket_id, (struct sockaddr*) &client_add, &client_add_size);
    if(comunication_socket_id>=0)
    {
        //index = 0;
        char tmp[100];
        while(1)
        {
            printf("\nOk");
            //n = recv(comunication_socket_id, (char*) buffer, sizeof(buffer)+1, 0);
            //n = recv(comunication_socket_id, (void*) buffer, sizeof(buffer)+1, 0);
            n = recv(comunication_socket_id, buffer, sizeof(buffer)+1, 0);
            printf("\nReceved! n: %d", n);
            printf("\nReceved: %s", buffer);

            if(strcmp(buffer, "end")==0)
            {
                close(comunication_socket_id);
                printf("\n...socket closed");
                return -1;
            }

[...]

and this is client side code :

unsigned long start, now;
 unsigned int *num = (unsigned int*)buffer;
 int i, n;

 TCPclient_send(buffer, strlen(buffer)+1);
 printf("\nSent: |%s|\n", buffer);
 start = clock(); // tempo iniziale attesa
 now = clock(); // tempo attuale
 while ((now - start) < TIMEOUT)
    {
        if ((n = TCPclient_receive(&buffer[i], sizeof(buffer)-i)) > 0)
         {
            i += n;
            if (i >= sizeof(unsigned int))
                {
                 // risposta completa
                 printf("Receved number %u.\r\n", ntohl(*num));
                 TCPclient_disconnect();
                 return 0;
                }
            }
        now = clock(); 
    }
 printf("No answer receved!\r\n");
 TCPclient_disconnect();

Console server side output:

davide@davide-VirtualBox:~/Documenti/RubricaTCP$ ./TCPserver 
Ok
Receved! n: 11
Errore di segmentazione (core dump creato) //-->segmentation fault, end of process

Console client side output:

Insert the command (end to close): SET=A;A;A;
Sent: |SET=A;A;A;|
No answer receved!

PS: this is my first application with sockets, so it's possible that i've done some stupid mistakes. I looked for answers in many topics, but i didn't find anything that can work out it. Thank you very much

n = recv(comunication_socket_id, buffer, sizeof(buffer)+1, 0);

sizeof(buffer)+1 is inherently incorrect. It should be sizeof(buffer) . You are using memory that does not exist.

printf("\nReceved! n: %d", n);

OK.

printf("\nReceved: %s", buffer);

Wrong. This should be

printf("\nReceved: %.*s", n, buffer);

Then:

if(strcmp(buffer, "end")==0)

This is also wrong. There is no guarantee that you've received a null terminated string or a complete command. It's a byte-stream protocol. If you want messages you have to implement them yourself. It is rarely correct to write networking code or any I/O code for that matter that doesn't have read or receive loops.

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