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

时间:2015-11-22 11:54:36

标签: c sockets recv

我试图在c中创建应用程序客户端/服务器,但在recv()之后我尝试使用缓冲区时收到的程序给出了分段错误(创建了核心转储),我无法工作出来 这是我在服务器端的代码:

// Allocate page
void *pPage = VirtualAllocEx(hProcess, NULL, 256, MEM_RESERVE, PAGE_EXECUTE_READWRITE);

// Commit memory for thread procedure
void *pThread = VirtualAllocEx(hProcess, pPage, 128, MEM_COMMIT, PAGE_EXECUTE_READWRITE);

// Commit memory for thread data
void *pData = VirtualAllocEx(hProcess, (void*)((long long)pPage + 128), 128, MEM_COMMIT, PAGE_EXECUTE_READWRITE);

// WriteProcessMemory, do stuff

// Release memory
VirtualFreeEx(hProcess, pPage, 256, MEM_RELEASE);

[...]

这是客户端代码:

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;
            }

控制台服务器端输出:

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();

控制台客户端输出:

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

PS:这是我第一次使用套接字的应用程序,所以我可能犯了一些愚蠢的错误。我在很多主题中寻找答案,但我找不到任何可以解决的问题。 非常感谢你

1 个答案:

答案 0 :(得分:2)

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

sizeof(buffer)+1本来就是不正确的。它应该是sizeof(buffer)。您正在使用不存在的内存。

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

行。

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

错误。这应该是

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

然后:

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

这也是错误的。无法保证您已收到空终止字符串完整命令。这是一个字节流协议。如果你想要消息,你必须自己实现它们。对于没有读取或接收循环的问题,编写网络代码或任何I / O代码很少是正确的。