Client/Server C program to make client send the name of a file

Using TCP or IP sockets write a client/server C program to make client send the name of a file and server to send back the contents of the requested file if present.


Solution:

Client Side:

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
int main(int argc,char *argv[])
{
int sockfd,newsockfd,portno,len,n;
char buffer[256],c[20000];
struct sockaddr_in serv,cli;
FILE *fd;
if(argc<2)
{
printf("Err:no port no.\nusage:\n./client portno\n ex:./client 7777\n");
exit(1);
}
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero((char *)&serv,sizeof(serv));
portno=atoi(argv[1]);
serv.sin_family=AF_INET;
serv.sin_port=htons(portno);
if(connect(sockfd,(struct sockaddr *)&serv,sizeof(serv))<0)
{
printf("server not responding..\n\n\n\ti am to terminate\n");
exit(1);
}
printf("Enter the file with complete path\n");
scanf("%s",&buffer);
if(write(sockfd,buffer,strlen(buffer))<0)
printf("Err writing to socket..\n");
bzero(c,2000);
printf("Reading..\n..\n");
if(read(sockfd,c,1999)<0)
printf("error: read error\n");
printf("client: display content of %s\n..\n",buffer);
fputs(c,stdout);
printf("\n..\n");
return 0;
}

Server Side:

#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<netdb.h>
int main(int argc,char *argv[])
{
int sockfd,newsockfd,portno,len,n;
char buffer[256],c[2000],cc[20000];
struct sockaddr_in serv,cli;
FILE *fd;
if(argc<2)
{
printf("erroe:no port no\n usage:\n/server port no\n");
exit(1);
}
sockfd=socket(AF_INET,SOCK_STREAM,0);
portno=atoi(argv[1]);
serv.sin_family=AF_INET;
serv.sin_addr.s_addr=INADDR_ANY;
serv.sin_port=htons(portno);
bind(sockfd,(struct sockaddr *)&serv,sizeof(serv));
listen(sockfd,10);
len=sizeof(cli);
printf("serve:\nwaiting for connection\n");
newsockfd=accept(sockfd,(struct sockaddr *)&cli,&len);
bzero(buffer,255);
n=read(newsockfd,buffer,255);
printf("\nserver recv:%s\n",buffer);
if((fd=fopen(buffer,"r"))!=NULL)
{
printf("server:%s found\n opening and reading..\n",buffer);
printf("reading..\n..reading complete");
fgets(cc,2000,fd);
while(!feof(fd))
{
fgets(c,2000,fd);
strcat(cc,c);
}
n=write(newsockfd,cc,strlen(cc));
if(n<0)
printf("error writing to socket");
printf("\ntransfer complete\n");
}
else
{
printf("server:file not found\n");
n=write(newsockfd,"file not foung",15);
if(n<0)
printf("error: writing to socket..\n");
}
return 0;
}


Output Using TCP or IP sockets write a client/server C program to make client send the name of a file and server to send back the contents of the requested file if present.:

Server part output


[root@localhost ~]# ./a.out 4455
server online
waiting for request...
server:(null) found!
transfering the contentserver:transfer completed
Segmentation fault
[root@localhost ~]#

client part output

[root@localhost ~]# ./a.out 4455
waiting for serverserver is online
client enter the path2.c
File Recieved:Display Contents#include<stdio.h>
struct frame{
int fslno;
char data[5];
}arr[10];
sort(int n)
{
struct frame temp;
int i,j,ex;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
if(arr[j].fslno>arr[j+1].fslno)
{
temp=arr[j];
ar


Learn More :