简体   繁体   English

使用从recv()tcpServer c获取的缓冲区的分段错误

[英]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. 我正在尝试在c中创建应用程序客户端/服务器,但是在recv()之后,当我尝试使用接收到的缓冲区时,程序给出了分段错误(创建了核心转储),我无法解决。 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. PS:这是我第一个使用套接字的应用程序,因此我可能犯了一些愚蠢的错误。 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. sizeof(buffer)+1本质上是不正确的。 It should be sizeof(buffer) . 它应该是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. 对于没有读取或接收循环的问题,编写网络代码或任何I / O代码很少是正确的。

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

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