我有一个TCP Concurent服务器,我的客户端有问题。我正在尝试从服务器向客户端发送文件但客户端不能保存到文件而我猜测{{ 1}}召唤是罪魁祸首。
服务器代码:
fwrite()
客户代码:
while (1)
{
/* pregatirea structurii client */
len = sizeof (from);
bzero (&from, sizeof (from));
/* a venit un client, acceptam conexiunea */
client = accept (sd, (struct sockaddr *) &from, &len);
/* eroare la acceptarea conexiunii de la un client */
if (client < 0)
{
perror ("[server] Eroare la accept().\n");
continue;
}
if (nr>30)
{
char maxMsg[100]="Numarul maxim de participanti a fost atins.\n";
write(client,maxMsg,50);
close(client);
nr--;
}
else
{
printf("[server] S-a conectat clientul cu descriptorul %d, de la adresa %s.\n",client, conv_addr (from));
nr++;
}
fflush (stdout);
pid=fork();
if(pid<0)
{
printf ("[server] Eroare la fork().\n");
}
if(pid==0)
{
/* copil/client */
if (welcome(client))
{
sendTest(client);
printf ("[server] S-a deconectat clientul cu descriptorul %d.\n",client);
fflush (stdout);
close (client); /* inchidem conexiunea cu clientul */
nr--;
close(sd);
exit(0);
}
}
} /* while */
} /* main */
int welcome(int client)
{
char buffer[100]; /* mesajul */
int bytes; /* numarul de octeti cititi/scrisi */
char name[100]; //mesajul primit de la client
char msgrasp[100]=" "; //mesaj de raspuns pentru client
bytes = read (client, name, sizeof (buffer));
if (bytes < 0)
{
perror ("Eroare la read() de la client.\n");
return 0;
}
printf ("[server] Numele participantului este %s\n", name);
/*pregatim mesajul de raspuns */
bzero(msgrasp,100);
strcat(msgrasp,"Hello ");
int ln = strlen(name) - 1;
if (name[ln] == '\n')
name[ln] = '\0';
strcat(msgrasp,name);
strcat(msgrasp,". Please wait for your test.\n");
printf("[server]Trimitem mesajul inapoi...%s\n",msgrasp);
if (bytes && write (client, msgrasp, bytes) < 0)
{
perror ("[server] Eroare la write() catre client.\n");
return 0;
}
return bytes;
}
int sendTest(int client)
{
FILE *fd;
int rand;
char filename[100];
char buffer[256];
int nread;
/* pregatim fisierul/testul pentru trimitere */
bzero(filename,100);
rand = randInt();
sprintf(filename,"%d",rand);
strcat(filename,".c");
fd = fopen(filename, "r");
if (fd == NULL)
{
perror("[server] Eroare la open().\n");
exit(EXIT_FAILURE);
}
/* trimitem numele fisierului */
write(client,filename,sizeof(filename));
printf("name=%s\n",filename);
/* Sending file data */
nread=fread(buffer,1,256,fd);
if(nread>0)
{
printf("[server] Sending file.\n");
write(client,buffer,nread);
}
if (nread < 256)
{
if (feof(fd))
printf("End of file\n");
if (ferror(fd))
printf("Error reading\n");
}
fclose(fd);
return 0;
}
文件服务器发送:
if (connect (sd, (struct sockaddr *) &server,sizeof (struct sockaddr)) == -1)
{
perror ("[client]Eroare la connect().\n");
return errno;
}
/* citirea mesajului */
bzero (name, 100);
printf ("[client]Introduceti un nume: ");
fflush (stdout);
read (0, name, 100);
/* trimiterea mesajului la server */
if (write (sd, name, 100) <= 0)
{
perror ("[client]Eroare la write() spre server.\n");
return errno;
}
/* citirea raspunsului dat de server
(apel blocant pina cind serverul raspunde) */
if (read (sd, name, 100) < 0)
{
perror ("[client]Eroare la read() de la server.\n");
return errno;
}
/* afisam mesajul primit */
printf ("[client]Mesajul primit este: %s\n", name);
fflush(stdout);
/* primim numele fisierului */
read(sd,filename,sizeof(filename));
fflush(stdout);
char filename2[50]="test2.c";
received_file = fopen(filename2, "w+");
if (received_file == NULL)
{
printf("Eroare la fopen().\n");
exit(EXIT_FAILURE);
}
bzero(recvBuff,256);
while((bytesReceived = read(sd, recvBuff, 256)) > 0)
{
printf("Bytes received %d\n",bytesReceived);
//fwrite(recvBuff, 8,bytesReceived,received_file);
fwrite("asdf",4,1,received_file);
printf("writing");
}
printf("wrote");
fclose(received_file);
printf("closed");
if(bytesReceived < 0)
{
printf("Eroare la read.\n");
}
/* inchidem conexiunea, am terminat */
close (sd);
此外, /*
Welcome. Here you will resolve your problem. Please use <insert comand here>
to send back the file when you are done.
Your task is to do <insert problem description here>.
Time allocated: 20 minutes;
May the force be with you.
*/
包含服务器发送的所有文本。
提前谢谢你帮助我。
修改
这是输出的图像。客户端在fwrite调用之前的recvBuff
之后停留。
output
答案 0 :(得分:1)
您忘记关闭服务器代码中的realloc
描述符:您在子代中分叉和关闭,但不在父代中,因此连接保持打开状态,客户端在其{{1}中等待阻塞对于永远不会来的数据。
这应该纠正它(你的代码编写得太糟糕,看看是否还有其他错误):
client