Trapti Gupta

C Program for File Transfer Using UDP

UDP is an unreliable protocol. It does not create a dedicated path for transmitting packets. So it is a connectionless protocol. Its header size is very small i.e. 8 bytes only.

What is UDP?

User Datagram Protocol is abbreviated as UDP. UDP works on the transport layer. UDP is a connectionless and unreliable protocol. There is no need to create a dedicated path for the transmission of data. UDP also sets some rules that decide how data needs to be transferred to the network using the internet. It transfers the packet in encapsulated form i.e. combining all the data in a packet and also provides its header details to the particular packet in the network.

UDP Header

UDP Header
The size of the UDP header is very small i.e. 8 bytes only. These 8 bytes have four fields, and the length of each byte is 2 bytes.

Given below there is a detailed explanation of the function and purpose of each field of the UDP header.

Source port: Source port is useful for the identification of the application that is transferring the data.

Destination port: Destination port is useful for the identification of the application that is receiving the data.

Length: For finding out the length of the header itself, the length field is used.

Checksum: There may be the chance that corrupted data will be delivered in the network so for checking whether the data transfer is corrupted or not, checksum is used.

UDP File Transfer Tools and Projects

  • Quick UDP Internet Connections abbreviated as QUIC. It is designed by Google and it is an experimental network protocol. It replaced http/2 for providing internet browsing in proper time and also even faster is the main objective of using QUIC.
  • The extended form of UDP transfer protocol is UDT. UDT is used whenever there is a need to send data in large amounts. As it transfers data very fast as compared to TCP. Configurable is one of the main features of UDT and it allows the usage of different algorithms that control congestion.
  • TCP and UDP blend in a tsunami. For achieving a faster speed it prefers to use UPD to transfer the data and TCP for control purposes.
  • For transferring and receiving the TCP and the UDP packets, packetsender is useful. It can also be used for many purposes such as malware analysis, test automation, quality assurance, etc. It comes along with GUI.
  • UFTP is based on FTP with multicast. It is an encryption form of UDP. Files can be transferred securely, efficiently, and reliably using UFTP.

Algorithm

  1. Server waits for the filename after starting
  2. Filename is sent by the client.
  3. Filename is received by the server which is sent by the client.
    The server starts reading the content of the file if the file is present and
    until the server reaches the End Of File, it continues sending the buffer which is filled with the encrypted file content.
  4. EOF represents End Of File.
  5. File is received at the client side in the form of a buffer until EOF(End Of File) is reached. Then the decryption is done.
  6. Server sends a file not found to the client if the file is not present.

UDP File Transfer Server

Code

// UDP socket programming server code
#include <stdio.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

#define IP_PROTOCOL 0
#define cipherKey 'S'
#define PORT_NO 15050
#define sendrecvflag 0
#define NET_BUF_SIZE 32
#define nofile "File Not Found!"

// defining function for clearing the buffer
void bufferClear(char* buf)
{
    int i;
    for (i = 0; i < NET_BUF_SIZE; i++)
        buf[i] = '\0';
}

// function for the encryption of the content
char Encrypt(char a)
{
    return a ^ cipherKey;
}

// function for transmitting file to the client
int transmitFile(FILE* fp, char* buf, int s)
{
    int i, len;
    if (fp == NULL) {
        strcpy(buf, nofile);
        len = strlen(nofile);
        buf[len] = EOF;
        for (i = 0; i <= len; i++)
            buf[i] = Encrypt(buf[i]);
        return 1;
    }

    char a, a2;
    for (i = 0; i < s; i++) {
        a = fgetc(fp);
        a2 = Encrypt(a);
        buf[i] = a2;
        if (a == EOF)
            return 1;
    }
    return 0;
}

// driver code
int main()
{
    int nBytes, sockfd;
    struct sockaddr_in con_addr;
    int addrlen = sizeof(con_addr);
    con_addr.sin_family = AF_INET;
    con_addr.sin_port = htons(PORT_NO);
    con_addr.sin_addr.s_addr = INADDR_ANY;
    char netbuf[NET_BUF_SIZE];
    FILE* fp;

    // socket()
    sockfd = socket(AF_INET, SOCK_DGRAM, IP_PROTOCOL);

    if (sockfd < 0)
        printf("\nNot received File descriptor!!\n");
    else
        printf("\nReceived file descriptor %d \n", sockfd);

    // bind()
    if (bind(sockfd, (struct sockaddr*)&con_addr, sizeof(con_addr)) == 0)
        printf("\nBinding Completed Successfuly!\n");
    else
        printf("\nFailed Binding!\n");

    while (1) {
        printf("\nWaiting for the name of the file...\n");

        // receiving the name of the file
        bufferClear(netbuf);

        nBytes = recvfrom(sockfd, netbuf,
                          NET_BUF_SIZE, sendrecvflag,
                          (struct sockaddr*)&con_addr, &addrlen);

        fp = fopen(netbuf, "r");
        printf("\nReceived the Name of the File: %s\n", netbuf);
        if (fp == NULL)
            printf("\nFailed File opening!\n");
        else
            printf("\nSuccessfully opened the File !\n");

        while (1) {

            // process
            if (sendFile(fp, netbuf, NET_BUF_SIZE)) {
                sendto(sockfd, netbuf, NET_BUF_SIZE,
                       sendrecvflag, 
                    (struct sockaddr*)&con_addr, addrlen);
                break;
            }

            // send
            sendto(sockfd, netbuf, NET_BUF_SIZE,
                   sendrecvflag,
                (struct sockaddr*)&con_addr, addrlen);
            bufferClear(netbuf);
        }
        if (fp != NULL)
            fclose(fp);
    }
    return 0;
}

Output

Received file descriptor 3

Binding Completed Successfuly!

Waiting for the name of the file...

Received the Name of the File: abc.txt

Successfully opened the File !

Waiting for the name of the file...

Received the Name of the File: /pc/documents/abc.txt

Successfully opened the File !

UDP File Transfer Client

Code

// socket programming client code 
#include <stdio.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

#define IP_PROTOCOL 0
#define PORT_NO 15050
#define NET_BUF_SIZE 32
#define IP_ADDRESS "127.0.0.1" // localhost
#define cipherKey 'S'
#define sendrecvflag 0

// defining function for clearing the buffer
void bufferClear(char* buf)
{
    int i;
    for (i = 0; i < NET_BUF_SIZE; i++)
        buf[i] = '\0';
}

// defiining function for  the data decryption
char Decrpyt(char a)
{
    return a ^ cipherKey;
}

// defining function for receiving the file
int fileRec(char* buf, int s)
{
    int i;
    char a;
    for (i = 0; i < s; i++) {
        a = buf[i];
        a = Decrpyt(a);
        if (a == EOF)
            return 1;
        else
            printf("%c", a);
    }
    return 0;
}

// driver code
int main()
{
    int sockfd, nBytes;
    struct sockaddr_in con_addr;
    int addrlen = sizeof(con_addr);
    con_addr.sin_family = AF_INET;
    con_addr.sin_port = htons(PORT_NO);
    con_addr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
    char netbuf[NET_BUF_SIZE];
    FILE* fp;

    // socket()
    sockfd = socket(AF_INET, SOCK_DGRAM,
                    IP_PROTOCOL);

    if (sockfd >= 0)
        printf("\nReceived file descriptor %d \n", sockfd);
    else 
        printf("\nNot received File descriptor!!\n");

    while (1) {
        printf("\nEnter name of the file to be received:\n");
        scanf("%s", netbuf);
        sendto(sockfd, netbuf, NET_BUF_SIZE,
               sendrecvflag, (struct sockaddr*)&con_addr,
               addrlen);

        printf("\n*********Data is Received*********\n");

        while (1) {
            // receiving the file
            bufferClear(netbuf);
            nBytes = recvfrom(sockfd, netbuf, NET_BUF_SIZE,
                              sendrecvflag, (struct sockaddr*)&con_addr,
                              &addrlen);

            // processing
            if (fileRec(netbuf, NET_BUF_SIZE)) {
                break;
            }
        }
        printf("\n**********************************\n");
    }
    return 0;
}

Output

Received file descriptor 3

Enter name of the file to be received:
abc.txt

*********Data is Received*********
30
**********************************

Enter name of the file to be received:
/pc/documents/abc.txt

*********Data is Received*********
30
**********************************

UDP vs TCP

FEATURESUDPTCP
ReliabilityIt is unreliable as there is no assurance of receiving all the packets.It is very reliable as it acknowledges each packet and tracks each packet till it reaches its destination.
Data flowFlow control functionality is not available in UDP.It provides flow control methods like sliding windows, congestion avoidance, etc.
Transmission speedData transfers speedily in UDP.It is not so fast as compared to TCP.
HeaderSize of the header is very small i.e. 8 bytes.Size of the header is small but as small as compared to UDP. Its header size is 20 bytes.
ConnectionIt is absolutely connectionless. It does not need to create a dedicated connection for the transmission of data.It is connection-oriented as it requires a proper and dedicated connection for the transmission of data.
AcknowledgementThere is no need for acknowledgment of the packet.It is mandatory to acknowledge the packet as further transmission is possible only after the acknowledgment of the previous packet.
ProtocolsDNS, VoIP, DHCP are some protocols that use UDP.HTTP, HTTPS, and FTP are some protocols that use TCP.

Conclusion

  • UDP is a connectionless and unreliable protocol.
  • UDP transfers messages very fast.
  • The size of the UDP header is 8 bytes.
  • The UDP header has four fields, each of 2 bytes.
  • Fields of UDP header are source port, destination port, length, and checksum.
  • Some UDP file transfer tools and projects are UDT, QUIC, tsunami, Packet sender, and UFTP.

Author