Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
C program for file Transfer using UDP?
File transfer using UDP in C enables transferring files between client and server using User Datagram Protocol. This implementation includes XOR encryption for basic security and handles file existence checking.
Syntax
int socket(int domain, int type, int protocol); int sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen); int recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen);
Algorithm
- The server starts and waits for a filename from the client.
- The client sends a filename to the server.
- The server receives the filename and checks if the file exists.
- If the file exists, the server reads and encrypts file contents in chunks, sending them until EOF.
- If the file doesn't exist, the server sends "File Not Found!" message.
- The client receives encrypted data chunks, decrypts them, and displays the content.
Note: This code uses Linux-specific socket headers (arpa/inet.h, sys/socket.h) and will not compile on Windows or online compilers. It requires a Unix/Linux environment with network permissions.
The Server
// server code for UDP socket programming
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define IP_Protocol 0
#define Port_No 15050
#define Net_Buf_Size 32
#define CipherKey 'S'
#define SendRecvFlag 0
#define NoFile "File Not Found!"
// function for clearing buffer
void clearBuf(char* b1){
int i;
for (i = 0; i < Net_Buf_Size; i++)
b1[i] = '\0';
}
// function for encryption method
char Cipher(char ch1){
return ch1 ^ CipherKey;
}
// function for sending file
int sendFile(FILE* fp1, char* buf1, int s1){
int i, len;
if (fp1 == NULL) {
strcpy(buf1, NoFile);
len = strlen(NoFile);
buf1[len] = EOF;
for (i = 0; i <= len; i++)
buf1[i] = Cipher(buf1[i]);
return 1;
}
char ch1, ch2;
for (i = 0; i < s1; i++) {
ch1 = fgetc(fp1);
ch2 = Cipher(ch1);
buf1[i] = ch2;
if (ch1 == EOF)
return 1;
}
return 0;
}
// driver code
int main(){
int sockfd1, nBytes;
struct sockaddr_in addr_con;
int addrlen = sizeof(addr_con);
addr_con.sin_family = AF_INET;
addr_con.sin_port = htons(Port_No);
addr_con.sin_addr.s_addr = INADDR_ANY;
char net_buf1[Net_Buf_Size];
FILE* fp1;
// socket()
sockfd1 = socket(AF_INET, SOCK_DGRAM, IP_Protocol);
if (sockfd1 < 0)
printf("\nfile descriptor is not received!!<br>");
else
printf("\nfile descriptor %d is received<br>", sockfd1);
// bind()
if (bind(sockfd1, (struct sockaddr*)&addr_con, sizeof(addr_con)) == 0)
printf("\nSuccessfully binded!<br>");
else
printf("\nBinding Failed!<br>");
while (1) {
printf("\nWaiting for name of file...<br>");
// receive name of file
clearBuf(net_buf1);
nBytes = recvfrom(sockfd1, net_buf1, Net_Buf_Size, SendRecvFlag,
(struct sockaddr*)&addr_con, &addrlen);
fp1 = fopen(net_buf1, "r");
printf("\nFile Name Received: %s<br>", net_buf1);
if (fp1 == NULL)
printf("\nFile open failed!<br>");
else
printf("\nFile opened successfully!<br>");
while (1) {
// process and send file content
if (sendFile(fp1, net_buf1, Net_Buf_Size)) {
sendto(sockfd1, net_buf1, Net_Buf_Size, SendRecvFlag,
(struct sockaddr*)&addr_con, addrlen);
break;
}
sendto(sockfd1, net_buf1, Net_Buf_Size, SendRecvFlag,
(struct sockaddr*)&addr_con, addrlen);
clearBuf(net_buf1);
}
if (fp1 != NULL)
fclose(fp1);
}
close(sockfd1);
return 0;
}
The Client
// client code for UDP socket programming
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define IP_Protocol 0
#define IP_Address "127.0.0.1" // localhost
#define Port_No 15050
#define Net_Buf_Size 32
#define CipherKey 'S'
#define SendRecvFlag 0
// function for clearing buffer
void clearBuf(char* b1){
int i;
for (i = 0; i < Net_Buf_Size; i++)
b1[i] = '\0';
}
// function for decryption method
char Cipher(char ch1){
return ch1 ^ CipherKey;
}
// function for receiving file
int recvFile(char* buf1, int s1) {
int i;
char ch1;
for (i = 0; i < s1; i++) {
ch1 = buf1[i];
ch1 = Cipher(ch1);
if (ch1 == EOF)
return 1;
else
printf("%c", ch1);
}
return 0;
}
// driver code
int main(){
int sockfd1, nBytes;
struct sockaddr_in addr_con;
int addrlen = sizeof(addr_con);
addr_con.sin_family = AF_INET;
addr_con.sin_port = htons(Port_No);
addr_con.sin_addr.s_addr = inet_addr(IP_Address);
char net_buf1[Net_Buf_Size];
// socket()
sockfd1 = socket(AF_INET, SOCK_DGRAM, IP_Protocol);
if (sockfd1 < 0)
printf("\nfile descriptor is not received!!<br>");
else
printf("\nfile descriptor %d is received<br>", sockfd1);
while (1) {
printf("\nPlease enter the name of file to receive:<br>");
scanf("%s", net_buf1);
sendto(sockfd1, net_buf1, Net_Buf_Size, SendRecvFlag,
(struct sockaddr*)&addr_con, addrlen);
printf("<br>---------Data is Received---------<br>");
while (1) {
// receive
clearBuf(net_buf1);
nBytes = recvfrom(sockfd1, net_buf1, Net_Buf_Size, SendRecvFlag,
(struct sockaddr*)&addr_con, &addrlen);
// process
if (recvFile(net_buf1, Net_Buf_Size)) {
break;
}
}
printf("<br>-------------------------------<br>");
}
close(sockfd1);
return 0;
}
Key Points
- UDP is connectionless, making file transfer faster but without guaranteed delivery.
- XOR encryption with key 'S' provides basic security for file contents.
- The server handles multiple file requests in a continuous loop.
- File existence is checked before attempting transfer.
Conclusion
This UDP-based file transfer system demonstrates basic client-server communication with encryption. While simple, it requires proper error handling and network configuration for production use.
Advertisements
