我正在尝试基于C中的套接字编写简单的客户端和服务器。 客户端发送char数组的大小(包括'\ 0'的最后一个单元格),然后发送chars数组。 服务器获取大小并尝试从客户端为字符数组分配内存。 在此之后,服务器查找空间并将字符从索引0复制到空间,将其粘贴到数组的末尾并将其发送回客户端(如果没有空间,则服务器发送加倍数组)。 / p>
我注意到有时候我会得到 - | 00 | | 02 | - 来自服务器的接收数组末尾的char。 我使用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);
}
}
}
答案 0 :(得分:2)
您的代码会忽略read()
的返回值:
read (client_sockfd, &i, 4);
您不得忽略此值。套接字接口不保证只是因为你要求4个字节,你实际上会得到4个字节。套接字接口仅保证您将至少返回一个字节。您必须继续调用read()
,直到获得所需的所有字节为止。
当您稍后读取字符串时也是如此。
即使您的程序现在可能正常运行,但这种问题会突然出现在不可预测的情况下,例如繁忙的机器或网络,您将无法重现它,并且您的程序将变得不可靠。