C program for file Transfer using UDP
Data can be transferred between two computers using Socket programming in C.
Similarly, files can easily be sent using UDP protocol and a simple client/server.
Security: Handled by encryption.
Protocol : UDP
Encryption: XOR encryption
Algorithm :
1. The server starts and waits for filename. 2. The client sends a filename. 3. The server receives filename. If file is present, server starts reading file and continues to send a buffer filled with file contents encrypted until file-end is reached. 4. End is marked by EOF. 5. File is received as buffers until EOF is received. Then it is decrypted. 6. If Not present, a file not found is sent.
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 to clear buffer void clearBuf(char* b) { int i; for (i = 0; i < NET_BUF_SIZE; i++) b[i] = '\0'; } // function to encrypt char Cipher(char ch) { return ch ^ cipherKey; } // function sending file int sendFile(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] = Cipher(buf[i]); return 1; } char ch, ch2; for (i = 0; i < s; i++) { ch = fgetc(fp); ch2 = Cipher(ch); buf[i] = ch2; if (ch == EOF) return 1; } return 0; } // driver code int main() { int sockfd, 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_buf[NET_BUF_SIZE]; FILE* fp; // socket() sockfd = socket(AF_INET, SOCK_DGRAM, IP_PROTOCOL); if (sockfd < 0) printf("\nfile descriptor not received!!\n"); else printf("\nfile descriptor %d received\n", sockfd); // bind() if (bind(sockfd, (struct sockaddr*)&addr;_con, sizeof(addr_con)) == 0) printf("\nSuccessfully binded!\n"); else printf("\nBinding Failed!\n"); while (1) { printf("\nWaiting for file name...\n"); // receive file name clearBuf(net_buf); nBytes = recvfrom(sockfd, net_buf, NET_BUF_SIZE, sendrecvflag, (struct sockaddr*)&addr;_con, &addrlen;); fp = fopen(net_buf, "r"); printf("\nFile Name Received: %s\n", net_buf); if (fp == NULL) printf("\nFile open failed!\n"); else printf("\nFile Successfully opened!\n"); while (1) { // process if (sendFile(fp, net_buf, NET_BUF_SIZE)) { sendto(sockfd, net_buf, NET_BUF_SIZE, sendrecvflag, (struct sockaddr*)&addr;_con, addrlen); break; } // send sendto(sockfd, net_buf, NET_BUF_SIZE, sendrecvflag, (struct sockaddr*)&addr;_con, addrlen); clearBuf(net_buf); } if (fp != NULL) fclose(fp); } return 0; } |
chevron_right
filter_none
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 to clear buffer void clearBuf(char* b) { int i; for (i = 0; i < NET_BUF_SIZE; i++) b[i] = '\0'; } // function for decryption char Cipher(char ch) { return ch ^ cipherKey; } // function to receive file int recvFile(char* buf, int s) { int i; char ch; for (i = 0; i < s; i++) { ch = buf[i]; ch = Cipher(ch); if (ch == EOF) return 1; else printf("%c", ch); } return 0; } // driver code int main() { int sockfd, 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_buf[NET_BUF_SIZE]; FILE* fp; // socket() sockfd = socket(AF_INET, SOCK_DGRAM, IP_PROTOCOL); if (sockfd < 0) printf("\nfile descriptor not received!!\n"); else printf("\nfile descriptor %d received\n", sockfd); while (1) { printf("\nPlease enter file name to receive:\n"); scanf("%s", net_buf); sendto(sockfd, net_buf, NET_BUF_SIZE, sendrecvflag, (struct sockaddr*)&addr;_con, addrlen); printf("\n---------Data Received---------\n"); while (1) { // receive clearBuf(net_buf); nBytes = recvfrom(sockfd, net_buf, NET_BUF_SIZE, sendrecvflag, (struct sockaddr*)&addr;_con, &addrlen;); // process if (recvFile(net_buf, NET_BUF_SIZE)) { break; } } printf("\n-------------------------------\n"); } return 0; } |
chevron_right
filter_none
Output :
Server :

Client :

Recommended Posts:
- Difference between File Transfer Protocol (FTP) and Secure File Transfer Protocol (SFTP)
- File Transfer Protocol (FTP) in Application Layer
- Lex program to search a word in a file
- C Program to find size of a File
- C Program for Lower Case to Uppercase and vice-versa in a file
- Lex Program to print the total characters, white spaces, tabs in the given input file
- Asynchronous serial data transfer
- Principle Of Reliable Data Transfer Protocol
- Asynchronous Transfer Mode (ATM) in Computer Network
- Simple Mail Transfer Protocol (SMTP)
- Basics of File Handling in C
- File opening modes(r versus r+)
- time.h header file in C with Examples
- Sorting integer data from file and calculate execution time
- Lex code to replace a word with another word in a file
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh



