Hello people,
i'm new here, and i hope that you can give me a hand with a little client-server software that i'm doing that it's driving me nuts.
The essence of the program is very simple, you have this client (in my case cliente.c, as i'm Spanish) with this syntax once compiled:
#./cliente -r remote_file -h ip_address -l local_file
This client sends the name of the remote file to the server, and the server sends the contents of said remote file to the client. The client then stores the information in "local_file". As you can see, it isn't a big deal. We're assuming both files are text files.
So, i'm basically using a write loop to send the data to the client, and a read loop to receive it. And that's where my problem is: The read loop generates an endless loop which causes my computer to freeze.
I've been trying various combinations and loops, to no avail. The only thing obtained was that, in the case that i totally erased the loop leaving just what was inside it, i would receive the info of the first iteration of the server loop, but then it would logically crash.
My question is, how can i manage to make the file transfer well? I'm going totally bananas with this case.
So as not to make this post huge, i'm only copying the interesting part of both the client and server.
Client:
if( connect(sockfd, (struct sockaddr *)&servidor, sizeof(servidor))<0)
{
printf("ERROR al conectar \n");
exit(1);
}
write(sockfd, nom_remoto, 16); //nom_remoto contains the name of the remote file
while(aux=read(sockfd, buffer, 16)!=0){ //buffer[16]
if(aux==-1){
printf("Error en el read");
}
else fputs(buffer,fp);//fp=fopen(nom_local,"w");
}
printf("Everything's ok");
fclose(fp);
close(sockfd);
Server:
while(1)
{
connfd=accept(sockfd,(struct sockaddr *)&cliente,&clilen);
if((pid==fork())==0)
{
close(sockfd);
read(connfd, nom_docu,16);
fp = fopen(nom_docu,"rt");
while(fgets(archivo,16,fp)!=NULL){//archivo[16]
write(connfd, archivo, 16);
}
close(connfd);
fclose(fp);
}
else close(sockfd);
}
Just in case you're wondering, the rest of both works perfectly, i'm 99% sure that the problem of my software lies here.
Thank you so much for the help.