डेटा को C में सॉकेट प्रोग्रामिंग लागू करके दो कंप्यूटर्स के बीच शिफ्ट किया जा सकता है।
इसी मामले में, फाइल्स को यूजर डेटाग्राम प्रोटोकॉल (UDP) और एक साधारण क्लाइंट/सर्वर लागू करके आसानी से भेजा जा सकता है।
सुरक्षा - एन्क्रिप्शन द्वारा संभाली जाती है।
प्रोटोकॉल - UDP
एन्क्रिप्शन - XOR एन्क्रिप्शन
एल्गोरिदम
सर्वर शुरू किया जाता है और फाइलनाम के लिए इंतजार करता है।
क्लाइंट द्वारा एक फाइलनाम भेजा जाता है।
यह फाइलनाम सर्वर द्वारा प्राप्त किया जाता है। यदि फाइल मौजूद है, तो सर्वर फाइल पढ़ना शुरू करता है और एन्क्रिप्टेड फाइल कंटेंट से भरे बफर को तब तक भेजना जारी रखता है जब तक फाइल-एंड नहीं पहुंच जाता।
एंड ऑफ फाइल को EOF द्वारा चिह्नित किया जाता है।
फाइल को बफर्स के रूप में तब तक प्राप्त किया जाता है जब तक EOF प्राप्त नहीं हो जाता। उसके बाद इसे डिक्रिप्ट किया जाता है।
यदि फाइल मौजूद नहीं है, तो "file not found" संदेश भेजा जाता है।
सर्वर
// UDP सॉकेट प्रोग्रामिंग के लिए सर्वर कोड #include#include #include #include #include #include #include #include #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#include #include #include #include #include #include #include #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; }