this is my c code of the server and the client. /* TCP server */
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
FILE *fp;
int main(int argc, char**argv){
int listenfd,connfd,n;
struct sockaddr_in servaddr,cliaddr;
socklen_t clilen;
char * banner = "1077"; // size of the file
char buffer[1000];
/* one socket is dedicated to listening */
listenfd=socket(AF_INET,SOCK_STREAM,0);
/* initialize a sockaddr_in struct with our own address information for
binding the socket */
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(32000);
/* binding */
bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
listen(listenfd,0); //listen( --,--);
clilen=sizeof(cliaddr);
/* accept the client with a different socket. */
connfd = accept(listenfd,(struct sockaddr *)&cliaddr,&clilen); // the uninitialized cliaddr variable is filled in with the
n = recvfrom(connfd,buffer,1000,0,(struct sockaddr *)&cliaddr,&clilen);//information of the client by recvfrom function
buffer[n] = 0;
sendto(connfd,banner,strlen(banner),0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
printf("Received:%s\n",buffer);
char file_name[] ="severtext.txt";
fp = fopen(file_name,"r"); // read mode
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("The contents of %s file are :\n", file_name);
char sendData[1000];
/*open servertext.txt and coppy it to a char array */
fread(sendData, 1000, 1, fp);
printf("The contents of sending\n");
sendto(connfd,sendData,sizeof(sendData),0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
/*information of the client by recvfrom function */
n = recvfrom(connfd,buffer,1000,0,(struct sockaddr *)&cliaddr,&clilen);
buffer[n] = 0;
printf("Received:%s\n",buffer);
printf("End of connection \n");
return 0;
}
...client code /* Sample TCP client */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main(int argc, char**argv){
int sockfd,n;
FILE *fp;
fp = fopen("file.txt", "w+");
struct sockaddr_in servaddr;
char banner[] = "request serverfile.txt";
char revbuf1[1000];
char buffer[4];
if (argc != 2){
printf("usage: ./%s <IP address>\n",argv[0]);
return -1;
}
/* socket to connect */
sockfd=socket(AF_INET,SOCK_STREAM,0);
/* IP address information of the server to connect to */
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=inet_addr(argv[1]);
servaddr.sin_port=htons(32000);
connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
sendto(sockfd,banner,strlen(banner),0, (struct sockaddr *)&servaddr,sizeof(servaddr));
n=recvfrom(sockfd,buffer,10,0,NULL,NULL);
buffer[n]=0;
printf("Received: %s\n",buffer);
printf("[Client] Receiveing file from Server and saving it as final.txt...");
char * banner1="ok";
printf("\ndata1 set\n");
n=recvfrom(sockfd,revbuf1,1000,0,NULL,NULL);
revbuf1[n]=0;
sendto(sockfd,banner1,strlen(banner1),0, (struct sockaddr *)&servaddr,sizeof(servaddr));
printf("Received: %s\n",revbuf1);
//open file.txt and over write it with the recieved data in the client side.
fwrite(revbuf1, 1000, 1, fp);
fclose(fp);
close (sockfd);
printf("[Client] Connection lost.\n");
return 0;
}
When i run these two programs i get garbage values in the file.txt with the servertext.txt content, Can someone help to fix this problem?
Because you are writing the whole of the rxd string to the file with
fwrite(revbuf1, 1000, 1, fp);
and any unused space in the array will be garbage. You should do this:
fwrite(revbuf1, 1, n, fp);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments