I am programming Cli/Serv app in c that the cli enter the file name of file then the server check up if the file exist @ server then the client can download it so this is part of code for server Assuming the Connection established after Accept();
** the complete code for server and client and header found in attacments.client..cmyHeader.hserver.cclient..cmyHeader.hserver.cabc.txtabc.txt
part of Server code:
Code:
strncpy(fileName,"abc.txt",128);
for(;;)
{
bzero(&buffer,sizeof(buffer));
//
n=read(connfd,&buffer,sizeof(buffer));
printf("you want to download File : %s ",buffer);
CmpValue=strncmp(buffer,fileName,7);
if(CmpValue==0)
{
bzero(&buffer,sizeof(buffer));
sprintf(buffer,"File exist in server.\n");
//n=write(connfd,&buffer,sizeof(buffer));
//state=1;
//buffer[0]=state;
fp= fopen("abc.txt",O_RDONLY);
//error handilng in open file
if(fp<0)
{
//bzero(&buffer,sizeof(buffer));
snprintf(buffer,sizeof(buffer),"%d",fd);
n=write(connfd,&buffer,sizeof(buffer));
continue;
}
while ((n= read(fd,buffer,sizeof(buffer))))
{
buffer[n] = '\0';
//printf("%s",buffer);
}
n=write(connfd,&buffer,sizeof(buffer));
fclose(fp);
}
else
{
bzero(&buffer,sizeof(buffer));
sprintf(buffer,"ERROR : File not exist in server.\n");
state=0;
//buffer[0]=state;
n=write(connfd,&buffer,sizeof(buffer));
}
if(n<0)
err_msg("Error,Writing to socket \n");
} // end of for
}
and this part of Client code :
Code:
for(;;)
{
bzero(&buffer,sizeof(buffer));
printf("Please Enter the file name you want to download : \n");
fgets(buffer,sizeof(buffer),stdin);
str=buffer;
n=write(sockfd,&buffer,sizeof(buffer)); // Data request from server that send string to server
if(n<0)
err_msg("ERROR WRITING TO SOCKET");
n=read(sockfd,&buffer,sizeof(buffer)); //Data replay from server that the client read the length and so on..
if(n<0)
err_msg("ERROR Reading FROM SOCKET");
// To check the buffer if its contains fd=-1 or not
if (buffer[0]=='-'&& buffer[1]=='1')
{
printf("\n");
err_msg("ERROR: opening file");
}
else
{
printf("\n");
printf("%s\n",buffer);
}
//TO check on buffer that include"--" coz this terminate the socket
if(buffer[0]=='-'&& buffer[1]=='-')
{
printf("You pressed \"--\" which terminate the client. \n");
return 0;
}
//else
//printf("Are you want to download File : %s \n",buffer);
} // end of for
close(sockfd);
return 0;
So in this code i made the read the content of file from buffer ,but what i need it is how to make client save the file to Client directory????