The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Saturday, December 8, 2012

Implement FTP Client-Server


FTP Client.c

#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<time.h>
#define MAX 100;

main(int argc,char**argv)
{
int sockfd,n,connfd;
char str[100],str1[100],s[2];
struct sockaddr_in serv;
if(argc!=5)
{
exit(0);
}
if(sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("\n Error ! Socket not created...\n");
exit (0);
}
bzero(&serv,sizeof(serv));
serv.sin_family=AF_INET;
serv.sin_port=htons(atoi(argv[2]));
if(inet_pton(AF_INET,argv[1],&serv.sin_addr)<=0)
{
printf("\n error in conversion of IP address from string to num\n"),
exit(0);
}
if(connect(sockfd,(struct sockaddr*)&serv,sizeof(serv))<0)
{
printf("\n Error!Conx not established...\n);
exit(0);
}
printf("\n connected...sending file name%s\n",argv[3]);
write(sockfd,argv[],strlen(argv[3]));    read(sockfd,s,3);
write(sockfd,argv[4],strlen(argv[4]));
str1[0]='\0';
while((n=read(sockfd,str,100))>0)
{
str[n]='\0';
printf("%s\n",str);
write(sockfd,str,strlen(str));
}
if(n<0)
printf("\n Read error...\n");
exit (0);
}


FTP Server.c

#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<time.h>
#define MAX 100;

int main ()
{
int listenfd,connfd,n,nl;
struct sockaddr_in serv;
char str1 [100],str2[100],fname[20],fname1[20],s[2],
int port=9999;
FILE*f1*f2;
listenfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&serv,sizeof(serv);
serv.sin_family=AF_INET;
serv.sin_addr.s_addr=htonl(INADDR_ANY);
serv.sin_port=htons(port);
bind(listenfd,(struct sockaddr*)&serv,sizeof(serv));
"serftp4.cpp"54L, 1518C
listen(listenfd,5);
for(;;)
{
connfd=accept(listenfd,(struct sockaddr*)NULL,NULL);
printf("\nClient requesting");
n=read(connfd,fname,20);
fname(n)='\0';
printf("\n Received:%s\n",fname);
f1=fopen(fname)."r");
strcpy(s,"ab");
write(connfd,s,strlen(s));
n1=read(connfd,fname1,20);
fname1[n1]=;\0';
printf("stored in:%s\n"'fname 1);
f2=fopen(fname1,"w");
while(!feof(f1))
{
fgets(str1,50f1);
write(connfd,str,1,strlen(str1));
n=read(connfd,str,2,100);
str2[n]='\0';
fputs(str2,f2);
}
fclose(f1);
fclose(f2);
close(connfd);
}
return 0;
}


OUTPUT SCREEN:-

Server Side:

[cseb17@localhost cseb 17]$ cc FTPserver.c
[cseb17@localhost cseb 17]$.a/out
Client requesting
Received:new.c
Stored in : thanga.c

Client side:

[cseb17@localhost cseb 17]$cc FTPclient.c
[cseb17@localhost cseb 17]$./a.out 127.0.0.1 6142 new.c.thanga.c

connected...sending filename new.c
hai how are you?

1 comment:

  1. Harrah's casino new COVID-19 vaccine and COVID-19 vaccine
    A review 서귀포 출장마사지 of Harrah's casino in Cherokee and Harrah's Cherokee in 제주도 출장마사지 COVID-19 in 경상남도 출장안마 Cherokee County is 성남 출장안마 a health concern 김제 출장안마 for Harrah's

    ReplyDelete