সকেট প্রোগ্রামিং ইমপ্লিমেন্ট করে দুটি কম্পিউটারের মধ্যে ডেটা ট্রান্সফার করা যায় C-তে।
একই ক্ষেত্রে, ইউজার ডেটাগ্রাম প্রোটোকল (UDP) এবং একটি সিম্পল ক্লায়েন্ট/সার্ভার ব্যবহার করে ফাইল সহজেই পাঠানো যায়।
সিকিউরিটি − এনক্রিপশন দ্বারা হ্যান্ডল করা হয়।
প্রোটোকল − UDP
এনক্রিপশন − XOR এনক্রিপশন
এলগরিদম
সার্ভার শুরু হয় এবং ফাইলনেমের জন্য অপেক্ষা করে।
ক্লায়েন্ট ফাইলনেম পাঠায়।
সার্ভার ফাইলনেম রিসিভ করে। যদি ফাইলটি থাকে, সার্ভার ফাইল পড়া শুরু করে এবং ফাইল কন্টেন্টস দিয়ে বাফার পূরণ করে এনক্রিপ্টেড ফর্মে পাঠাতে থাকে যতক্ষণ না ফাইল-এন্ড পাওয়া যায়।
এন্ড অফ ফাইল EOF দ্বারা মার্ক করা হয়।
ফাইল বাফার হিসেবে রিসিভ হয় যতক্ষণ না EOF রিসিভ হয়। এরপর এটি ডিক্রিপ্ট হয়।
যদি ফাইল না থাকে, "file not found" মেসেজ পাঠানো হয়।
সার্ভার
// UDP সকেট প্রোগ্রামিং-এর জন্য সার্ভার কোড
#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!"
// বাফার ক্লিয়ার করার ফাংশন
void clearBuf(char* b1){
int i;
for (i = 0; i < Net_Buf_Size; i++)
b1[i] = '\0';
}
// এনক্রিপশন মেথডের ফাংশন
char Cipher(char ch1){
return ch1 ^ CipherKey;
}
// ফাইল পাঠানোর ফাংশন
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;
}
// ড্রাইভার কোড
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!!\n");
else
printf("\nfile descriptor %d is received\n", sockfd1);
// bind()
if (bind(sockfd1, (struct sockaddr*)&addr_con,
sizeof(addr_con)) == 0)
printf("\nSuccessfully is binded!\n");
else
printf("\nBinding is Failed!\n");
while (1) {
printf("\nWaiting for name of file...\n");
// ফাইলনেম রিসিভ
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 is Received: %s\n", net_buf1);
if (fp1 == NULL)
printf("\nFile open is failed!\n");
else
printf("\nFile Successfully is opened!\n");
while (1) {
// প্রসেস
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);
}
return 0;
}
ক্লায়েন্ট
// UDP সকেট প্রোগ্রামিং-এর জন্য ক্লায়েন্ট কোড
#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
// বাফার ক্লিয়ার করার ফাংশন
void clearBuf(char* b1){
int i;
for (i = 0; i < Net_Buf_Size; i++)
b1[i] = '\0';
}
// ডিক্রিপশন মেথডের ফাংশন
char Cipher(char ch1){
return ch1 ^ CipherKey;
}
// ফাইল রিসিভ করার ফাংশন
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;
}
// ড্রাইভার কোড
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("\nfile descriptor is not received!!\n");
else
printf("\nfile descriptor %d is received\n", sockfd1);
while (1) {
printf("\nPlease enter the name of file to receive:\n");
scanf("%s", net_buf1);
sendto(sockfd1, net_buf1, Net_Buf_Size,
SendRecvFlag, (struct sockaddr*)&addr_con,
addrlen);
printf("\n---------Data is Received---------\n");
while (1) {
// রিসিভ
clearBuf(net_buf1);
nBytes = recvfrom(sockfd1, net_buf1, Net_Buf_Size,
SendRecvFlag, (struct
sockaddr*)&addr_con,
&addrlen);
// প্রসেস
if (recvFile(net_buf1, Net_Buf_Size)) {
break;
}
}
printf("\n-------------------------------\n");
}
return 0;
}