C program for file Transfer using UDP?


Data can be shifted between two computers implementing Socket programming in C.

In same case, files can easily be sent implementing User Datagram Protocol(UDP) and a simple client/server.

Security − Handled by encryption.

Protocol − UDP

Encryption − XOR encryption

Algorithm

  • The server is started and waited for filename.

  • A filename is sent by the client.

  • This filename is received by the server. If file is present, server starts reading file and is continued to send a buffer filled with file contents encrypted until and unless file-end is reached.

  • End of File is marked by EOF.

  • File is received as buffers until and unless EOF is received. After that it is ecrypted.

  • If file is not present, a message "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 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(fp);
      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("
file descriptor is not received!!
");    else       printf("
file descriptor %d is received
", sockfd1);    // bind()    if (bind(sockfd1, (struct sockaddr*)&addr_con,       sizeof(addr_con)) == 0)       printf("
Successfully is binded!
");    else       printf("
Binding is Failed!
");    while (1) {       printf("
Waiting for name of file...
");       // 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("
File Name is Received: %s
", net_buf1);       if (fp1 == NULL)       printf("
File open is failed!
");       else       printf("
File Successfully is opened!
");       while (1) {          // process          if (sendFile(fp1, net_buf1, Net_Buf_Size)) {             sendto(sockfd1, net_buf1, Net_Buf_Size,             SendRecvFlag,             (struct sockaddr*)&addr_con,             addrlen);             break;          }          // send          sendto(sockfd1, net_buf1, Net_Buf_Size,          SendRecvFlag,          (struct sockaddr*)&addr_con, addrlen);          clearBuf(net_buf1);       }       if (fp1 != NULL)       fclose(fp1);    } 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 receiveing 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];
   FILE* fp1;
   // socket()
   sockfd1 = socket(AF_INET, SOCK_DGRAM,
   IP_Protocol);
   if (sockfd1 < 0)
   printf("
file descriptor is not received!!
");    else    printf("
file descriptor %d is received
", sockfd1);    while (1) {       printf("
Please enter the name of file to receive:
");       scanf("%s", net_buf1);       sendto(sockfd1, net_buf1, Net_Buf_Size,       SendRecvFlag, (struct sockaddr*)&addr_con,       addrlen);       printf("
---------Data is Received---------
");       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("
-------------------------------
");    }    return 0; }

Updated on: 29-Jan-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements