FTP - UDP - File Transfer Protocol - Networks & DBMS Lab - C Program

Program:

// FTP Client Program: ftpudpc.c

#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<stdlib.h>
main()
{
    struct sockaddr_in server,client;
    int s,n,ret;size_t fp;
    char filename[20],downloaded[10],filedata[100],c[25];
    mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
    s=socket(AF_INET,SOCK_DGRAM,0);
    server.sin_family=AF_INET;
    server.sin_port=2000;
    server.sin_addr.s_addr=inet_addr("127.0.0.1");
    n=sizeof(server);
    printf("Enter the name of the file: ");
    scanf("%s",filename);
    printf("\nEnter a name to save: ");
    scanf("%s",downloaded);
    printf("\nDownloading...\n");
    sendto(s,filename,sizeof(filename),0,(struct sockaddr *)&server,n);
    fp = open(downloaded, O_WRONLY | O_CREAT | O_TRUNC, mode);
    if(fp==-1)
    {
        printf("\nError...");
        exit(0);
    }
    recvfrom(s,filedata,sizeof(filedata),0,NULL,NULL);
    printf("\nProcessing Contents...\n");
    while(1)
    {
        if(strcmp(filedata,"end")==0)
            break;
    printf("%s",filedata);
        ret=write(fp,filedata,strlen(filedata));
        bzero(filedata,100);
        recvfrom(s,filedata,sizeof(filedata),0,NULL,NULL);
    }
    printf("\nFile downloaded successfully...\n");
}

// FTP Server Program: ftpudps.c

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<string.h>

main()
{
    struct sockaddr_in server,client;
    int serversock,n,fp,end;
    char filename[20],buffer[100];
    serversock=socket(AF_INET,SOCK_DGRAM,0);
    server.sin_family=AF_INET;
    server.sin_port=2000;
    server.sin_addr.s_addr=inet_addr("127.0.0.1");
    bind(serversock,(struct sockaddr *)&server,sizeof(server));
    n=sizeof(client);
    recvfrom(serversock,filename,sizeof(filename), 0,(struct sockaddr *)&client,&n);
    fp=open(filename,O_RDONLY);
    while(1)
    {
        end=read(fp,buffer,sizeof(buffer));
        if(end==0)
            break;
        sendto(serversock,buffer,sizeof(buffer),0,(struct sockaddr *)&client,n);
        bzero(buffer,100);
    }
    strcpy(buffer,"end");
    sendto(serversock,buffer,sizeof(buffer),0,(struct sockaddr *)&client,n);
}

// File to be transfered: mybtech.txt

~~My Btech~~
A Computer Science & Engineering Portal
www.2k8cs.tk
www.2k8618.blogspot.com
2008-2012

Output:

Terminal 1: (Client)
nn@linuxmint ~ $ gcc ftpudpc.c -o client
nn@linuxmint ~ $ ./client
Enter the name of the file: mybtech.txt

Enter a name to save: test.txt

Downloading...

Processing Contents...
~~My Btech~~
A Computer Science & Engineering Portal
www.2k8cs.tk
www.2k8618.blogspot.com
2008-2012

File downloaded successfully...
nn@linuxmint ~ $

Terminal 2: (Server)
nn@linuxmint ~ $ gcc ftpudps.c -o server
nn@linuxmint ~ $ ./server
nn@linuxmint ~ $

1 comment:

  1. It is showing that the file is downloading but on the client side no text is been displayed

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...