1

我正在尝试基于 C 中的套接字编写简单的客户端和服务器。客户端发送字符数组的大小(包括 '\0' 的最后一个单元格),然后发送字符数组。服务器获取大小并尝试为来自客户端的字符数组分配内存。之后,服务器查找空格并将索引 0 中的字符复制到空格,将其粘贴到数组的末尾并将其发送回客户端(如果没有空格,则服务器发送双倍数组)。

我注意到有时我会得到-- |00| |02| -- 从服务器接收到的数组末尾的字符。我已经使用 valgrind 运行了服务器和客户端,它表明问题出在服务器上。

代码如下。

客户:

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>



int
main ()
{
    int sockfd;
    socklen_t len;
    struct sockaddr_in address;
    int result;

    char ch;
    char *string;
    int i;

    sockfd = socket (AF_INET, SOCK_STREAM, 0);

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr ("127.0.0.1"); 
    address.sin_port = htons (9734);
    len = sizeof (address);

    result = connect (sockfd, (struct sockaddr *) &address, len);

    if (result == -1)
    {
        perror ("oops: netclient");
        exit (1);
    }

    string = (char*)malloc(sizeof(char));

    for (i =0; ch = getchar(); i++)
    {
        string = (char*)realloc(string, (i+1)*sizeof(char));

        if(ch != '\n')
            string[i] = ch;
        else if (ch == '\n')
        {
            string[i]='\0';
            break;
        }
    }

    printf("%s\n", string);
    printf("%d\n", i);
    i=i+1;

    write (sockfd, &i, 4);

    write (sockfd, string, i);


    read (sockfd, &i, 4);


    string = (char*)realloc(string, i*sizeof(char));

    read (sockfd, string, i); 

    printf("String recieved: %s\n", string);

    close (sockfd);

    free(string);

    exit (0);
}

和服务器:

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>

int
main ()
{
    int server_sockfd, client_sockfd; 
    socklen_t server_len, client_len; 
    struct sockaddr_in server_address; 
    struct sockaddr_in client_address; 

    server_sockfd = socket (AF_INET, SOCK_STREAM, 0); 
    server_address.sin_family = AF_INET; /*ipv4*/
    server_address.sin_addr.s_addr = htonl (INADDR_ANY); 
    server_address.sin_port = htons (9734);
    server_len = sizeof (server_address);
    bind (server_sockfd, (struct sockaddr *) &server_address, server_len); 


    listen (server_sockfd, 5); 
    signal (SIGCHLD, SIG_IGN);

    while (1)
    {
        char *string;
        int i, j, k, l=0; /*variables to iteration*/
        printf ("server waiting\n");


        client_len = sizeof (client_address);
        client_sockfd = accept (server_sockfd,
                (struct sockaddr *) &client_address,
                &client_len); 


        if (fork () == 0)
        {


            read (client_sockfd, &i, 4);
            printf("recieved int %d\n", i);
            string = (char*)malloc(sizeof(char) * (i));
            read (client_sockfd, string, i);
            printf("\nSTRING recieved: %s \n", string);

            for(j=0; string[j]!='\0'; j++)
            {
                if(string[j] == ' ')
                {
                    i = i+j; /*size to enlarge*/
                    break;
                }
            }

            /*sending new size*/
            write(client_sockfd, &i, 4);

            /*enlarge the string*/
            string = (char*)realloc(string, i*sizeof(char));

            for (k = i-j-1; k < i-1 ; k ++)
            {
                string[k] = string[l];
                l++;
            }
            string[k+1] = '\0';

            write(client_sockfd, string, i);


            close (client_sockfd);

            free(string);

            exit (0);
        }



        else
        {
            close (client_sockfd);
        }
    }
}
4

1 回答 1

2

您的代码忽略以下返回值read()

        read (client_sockfd, &i, 4);

不能忽略此值。套接字接口不保证仅仅因为您请求了 4 个字节,您实际上会得到 4 个字节。套接字接口仅保证您将至少获得一个字节。您必须继续调用read(),直到获得所需的所有字节。

当您在几行之后读取字符串时也是如此。

即使您的程序现在看起来可以工作,但这种问题会在不可预知的情况下突然出现,例如繁忙的机器或网络,您将无法重现它,并且您的程序将变得不可靠。

于 2012-04-24T01:36:43.160 回答