File Transfer Protocol Server TCP in CS1305- NETWORK C Language Programming LInux OS

Step by step Algorithm: create a socket and mention the same as for client.
Bind the socket.
Listen to any clients are waiting
Accept the connection, if any client has required
Receive the source file name to read the contents.
Open the source file name to read the content
Open the destination file name and write the contents.
Send the destination file name and write the contents.
Close the socket.

See ->The Basic FTP SYNTAX command C Language Programing

Source code of programming FTP-File Transfer protocol Server in Network Lap
#include<sys/socket.h>
#include<sys/types.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#define SER_PORT 6100
int main()
{ int a;
int sersock,newsock,n;
char str[25],str1[25];
FILE *f1,*f2;
char buf[100],buf1[100];
struct sockaddr_in seraddr,cliinfo;
socklen_t size=sizeof(cliinfo);
seraddr.sin_family=AF_INET;
seraddr.sin_port=htonl(INADDR_ANY);
if((sersock=socket(AF_INET,SOCK_STREAM,0))<0)
{
error("socket\n");
exit(0);
}
if(bind(sersock,(struct sockaddr*)&seraddr,sizeof(seraddr))<0)
{
error("bind\n");
exit(0);
}
if(listen(sersock,1)<0)
{
error("listen\n");
exit(0);
}
if((newsock=accept(sersock,(struct sockaddr*)&cliinfo,&size))<0)
{
error("accept\n");
exit(0);
} else
printf("\n connection to %s\n",inet_ntoa(cliinfo.sin_addr));
recv(newsock,buf,sizeof(buf),0);
recv(newsock,buf1,sizeof(buf),0);
printf("%s",buf);
printf("%s",buf1);
f2=fopen(buf1,"w");
while(!feof(f1))
{ fscanf(f1,"%s",str1);
fprintf(f2,"%s",str1);
} close(newsock);
close(sersock);
return 0;
}

Output FTP Server
[Linux28@localhost network]$ cc ftpserver.c
[Linux28@localhost network]$./a.out
Connect to 192.168.1.111

[Linux28@localhost network]$cat output.dat
Welcome To Linux Network

Post a Comment

0 Comments