UDP File Transfer Program in Unix Using C Programming
On this page (6sections)
UDP File Transfer Program in Unix Using C Programming
👉 File Transfer Server Code
👉 Explanation
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>
```c
int main() {
int sfd, cfd;
```text
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>
```c
int main() {
int sfd, cfd;
```text
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.
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.
👉 More Unix Network Programs
Related Tutorials
Unix Basic Commands
To create a new file and add content to it, you can use the cat command with the output redirection operator ().
Read tutorialFIFO Client/Server Program in Unix Using C Programming
In this blog post, we will explore the implementation of a FIFO (First-In-First-Out) client/server program in the Unix environment using the C programming langu
Read tutorialMessage Queue in Unix Using C Programming
Message queues are a crucial inter-process communication mechanism used in Unix-based systems for sending and receiving messages between processes. They provide
Read tutorial