UDP File Transfer Program in Unix Using C Programming
🎯 Introduction
This blog post will demonstrate how to create a simple UDP-based file transfer program in C on a Unix system. We will implement both the client and server sides of the file transfer process using UDP sockets. UDP (User Datagram Protocol) is a connectionless and unreliable protocol, making it suitable for scenarios where some data loss is acceptable, and real-time communication is critical.
🎯 Concept
UDP (User Datagram Protocol) is one of the core protocols of the Internet Protocol Suite. Unlike TCP, UDP is a connectionless protocol, meaning it does not establish a direct connection before data transfer. Instead, it sends data packets, known as datagrams, without guaranteeing their delivery. As a result, UDP is faster and more efficient but lacks reliability and error checking.
🎯 Detailed Explanation File Transfer Server
👉 File Transfer Server Code
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <string.h>
int main() {
    int sfd, cfd;
    char buf[1024] = "";
    struct sockaddr_in server, client;
    sfd = socket(AF_INET, SOCK_DGRAM, 0);
    bzero(&server, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(1011);
    inet_aton("172.16.29.67", &server.sin_addr);
    int b = bind(sfd, (struct sockaddr*)&server, sizeof(server));
    printf("BIND VALUE: %d\n", b);
    int l = sizeof(client);
    for (;;) {
        recvfrom(sfd, buf, 1024, 0, (struct sockaddr*)&client, &l);
        int fd = open(buf, O_RDONLY);
        read(fd, buf, 1024);
        sendto(sfd, buf, strlen(buf), 0, (struct sockaddr*)&client, sizeof(client));
        printf("MESSAGE FROM CLIENT:\n%s\n", buf);
    }
    close(sfd);
}
👉 Explanation
The server code listens for incoming file requests from clients. Upon receiving a file name from the client, it opens the requested file, reads its content, and sends it back to the client using the same UDP socket.
🎯 Detailed Explanation File Transfer Cleint
👉 File Transfer Client Code
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
int main() {
    int sfd, cfd;
    char buf[1024] = "";
    struct sockaddr_in server, client;
    sfd = socket(AF_INET, SOCK_DGRAM, 0);
    bzero(&server, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(1011);
    inet_aton("172.16.29.67", &server.sin_addr);
    int b = connect(sfd, (struct sockaddr*)&server, sizeof(server));
    printf("CONNECT VALUE: %d\n", b);
    int l = sizeof(client);
    for (;;) {
        printf("ENTER THE FILE NAME:\n");
        scanf("%s", buf);
        sendto(sfd, buf, strlen(buf), 0, (struct sockaddr*)&server, sizeof(server));
        recvfrom(sfd, buf, 1024, 0, NULL, NULL);
        printf("RECEIVED FROM SERVER:\n%s\n", buf);
    }
    close(sfd);
}
👉 Explanation
The client code takes the input of the file name from the user and sends it to the server using the UDP socket. Upon receiving the file content from the server, it displays the content on the client side.
🎯 Sample Output
👉 Server Output
$cc udps.c -o udpsÂ
$./udps
BIND VALUE:0Â
accept value 4Â
MESSAGE FROM CLIENT:Â
#include<stdio.h>Â
main()Â
{Â
printf("hello");Â
}Â
👉 Client OutputÂ
#cc udps.c -o udpsÂ
#./udpsÂ
ENTER THE MESSAGEÂ
hello.cÂ
RECEIVED FROM SERVER
#include<stdio.h>Â
main()Â
{Â
printf("hello");Â
}Â
🎯 Summary
In this blog post, we learned how to implement a UDP-based file transfer program in C on Unix. We explored the concepts of UDP, its connectionless and unreliable nature, and its suitability for real-time communication. The server listens for file requests, reads the requested file, and sends its content back to the client. The client takes user input for the file name, sends it to the server, and displays the received file content.
🎯 Key Points
UDP (User Datagram Protocol) is a connectionless and unreliable protocol.
UDP is faster and more efficient than TCP but lacks reliability.
UDP is suitable for real-time communication scenarios.
The server listens for file requests and sends the requested file content to the client using UDP sockets.
The client sends file requests to the server and displays the received file content.