[Solved] C Programming Language Trying Send File Socket Client First Sent File Size File Content R Q37159500
In c programming language I am trying to send afile from a socket to the client . I first sent the file size thenthe file content but for some reason it is not writing the contentto the file.
Client.c.Unless is their an easier way to do this and if soplease do not use fopen() or fwrite().
include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <sys/socket.h>
int main(int argc, char *argv[]) {
int sockfd = -1
int portno = -1;
int conn = -1;
int file_size =0;
ssize_t len;
int n = -1;
struct hostent*server;
char buffer[256];
int fd =0;
if(argc < 3){
fprintf(stderr,”usage %s hostname portn”,argv[0]);
exit(0);
}
// getting theportnumber
portno =atoi(argv[2]);
sockfd =socket(AF_INET,SOCK_STREAM,0);
if(sockfd < 0){
error(“Error opening socket”);
}
// getting thehostname
server =gethostbyname(argv[1]);
if(server ==NULL){
fprintf(stderr,”Error no such hostn”);
exit(0);
}
conn =connection(portno,server,sockfd);
if(conn < 0){
printf(“%sn”,strerror(errno));
}
//reciving file size
recv(sockfd,buffer,BUFSIZ,0);
file_size =atoi(buffer);
int path =open(“text.txt”,O_CREAT | O_WRONLY, 0644);
if(path < 0){
printf(“%sn”,strerror(errno));
}
int remain_data =file_size;
printf(“%d”,file_size);
recv(sockfd,buffer,BUFSIZ,0);
char buffer1[200];
while((remain_data>0) && ((len = recv(sockfd,buffer,BUFSIZ,0))>0)){
write(path,buffer,100);
remain_data-=len;
fprintf(stdout,”Recived %d bytes and we hope :- %dbytesn”,len,remain_data);
}
return 0;
}
Server.c
include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <sys/socket.h>
int main(int argc, char const *argv[]){
int sockfd =-1;
int portno = -1;
int clilen = -1;
int remain_data=0;
char buffer[300];
char buffer1[300];
int peer_socket =-1;
struct sockaddr_inserv_addr,cli_addr;
int n = -1;
struct statfile_stat;
charfile_size[256];
ssize_t len;
int sent_bytes =0;
sockfd = socket(AF_INET,SOCK_STREAM, 0);
if(sockfd<0){
error(“Error opening socket”);
}
bzero((char*)&serv_addr,sizeof(serv_addr));
portno=atoi(argv[1]);
serv_addr.sin_family= AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port =htons(portno);
if(bind(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0){
error(“Error on Binding”);
}
listen(sockfd,5);
int fd =open(“project/text.txt”,O_RDONLY);
fstat(fd,&file_stat);
clilen =sizeof(cli_addr);
peer_socket =accept(sockfd,(struct sockaddr *)&cli_addr, &clilen);
sprintf(file_size,”%d”,file_stat.st_size);
//sends file size
len =send(peer_socket,file_size,sizeof(file_size),0);
if(peer_socket<0){
error(“Error on accept”);
}
off_t offset =0;
remain_data =file_stat.st_size;
//send the file
while(((sent_bytes=sendfile(peer_socket,fd,&offset,BUFSIZ))>0)&&( remain_data>0)){
remain_data -=sent_bytes;
}
bzero(buffer,256);
//n =read(newsockfd,buffer,255);
//if(n < 0){
// error(“Error reading from thesocket”);
//}
//n = write(newsockfd,”The direcotry existsn”, 100);
//if(n<0){
// error(“Error writing tosocket”);
//}
return 0;
}
Expert Answer
Answer to In c programming language I am trying to send a file from a socket to the client . I first sent the file size then the f… . . .
OR

