Guest User

ggwp

a guest
Apr 12th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 79.43 KB | None | 0 0
  1. // Q1. WAP in C/C++ so that a client enters an integer and server returns
  2. // i) Nth Fibonacci number
  3. // ii) Factorial of n
  4.  
  5. Client Side:
  6. #include <stdio.h> #include <sys/socket.h> #include <stdlib.h> #include <netinet/in.h> #include <string.h> #include<math.h> #defne PORT 8080
  7. int main(int argc, char const *argv[]){ struct sockaddr_in address;
  8. int sock = 0, valread;
  9. struct sockaddr_in serv_addr;
  10. if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){ printf("\n Socket creation error \n");
  11. return -1;
  12. }
  13. memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET;
  14. serv_addr.sin_port = htons(PORT);
  15. if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0){
  16. printf("\nInvalid address \n");
  17. return -1; }
  18. If (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){ printf("\nConnection Failed \n");
  19. return -1;
  20. }
  21. int n;
  22. printf("\n Enter the Number n :\n"); scanf("%d",&n);
  23. send(sock , &n , sizeof(int) , 0 );
  24. int res;
  25. valread = read( sock , &res, sizeof(int)); printf("Fibonacci : %d\n",res );
  26. valread = read( sock , &res, sizeof(int)); printf("Factorial : %d\n",res );
  27. return 0;
  28.  
  29. }
  30. Server Side:-
  31. #include <unistd.h> #include <stdio.h> #include <sys/socket.h> #include <stdlib.h> #include <netinet/in.h> #include <string.h> #include<math.h> #defne PORT 8080
  32. int main(int argc, char const *argv[]){
  33. int server_fd, new_socket, valread; struct sockaddr_in address;
  34. int opt = 1;
  35. int addrlen = sizeof(address);
  36. If ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0){
  37. perror("socket failed");
  38. exit(EXIT_FAILURE); }
  39. If (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,&opt, sizeof(opt))){
  40. perror("setsockopt"); exit(EXIT_FAILURE);
  41. }
  42. address.sin_family = AF_INET;
  43. address.sin_addr.s_addr = INADDR_ANY;
  44. address.sin_port = htons( PORT );
  45. If (bind(server_fd, (struct sockaddr *)&address,sizeof(address))<0){
  46. perror("bind failed");
  47. exit(EXIT_FAILURE); }
  48. If (listen(server_fd, 3) < 0){ perror("listen");
  49. exit(EXIT_FAILURE); }
  50. If ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0){
  51. perror("accept"); exit(EXIT_FAILURE);
  52. }
  53. int n;
  54. valread = read( new_socket , &n , sizeof(int)); printf("Received : %d\n",n);
  55. int dp[n];
  56.  
  57. dp[0]=1,dp[1]=1; int i; for(i=2;i<n;i++){
  58. dp[i] = dp[i-1] + dp[i-2]; }
  59. send(new_socket , &dp[n-1] , sizeof(int) , 0 ); printf("Fibonacci sent\n");
  60. int fact = 1;
  61. for(i=1;i<=n;i++){
  62. fact = fact*i; }
  63. send(new_socket , &fact , sizeof(int) , 0 );
  64. return 0; }
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.    
  78. // Q2. WAP so that client sends two numbers and server calculate addition, subtraction, multiplication, division and returns it.
  79. Client Side:
  80. #include <stdio.h>
  81. #include <sys/socket.h>
  82. #include <stdlib.h>
  83. #include <netinet/in.h>
  84. #include <string.h> #include<math.h>
  85. #defne PORT 8080
  86. int main(int argc, char const *argv[]){
  87. struct sockaddr_in address;
  88. int sock = 0, valread;
  89. struct sockaddr_in serv_addr;
  90. If ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
  91. printf("\n Socket creation error \n");
  92. return -1; }
  93. memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET;
  94. serv_addr.sin_port = htons(PORT);
  95. if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0){
  96. printf("\nInvalid address \n");
  97. return -1; }
  98. if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){ printf("\nConnection Failed \n");
  99. return -1;
  100. }
  101. int n1,n2, res;
  102. float sol;
  103. scanf("%d",&n1);
  104. scanf("%d",&n2);
  105. send(sock , &n1 , sizeof(int) , 0 ); send(sock , &n2 , sizeof(int) , 0 ); valread = read( sock , &res, sizeof(int)); printf("Sum : %d\n",res);
  106. valread = read( sock , &res, sizeof(int)); printf("Product : %d\n",res);
  107. valread = read(sock, &res, sizeof(int)); printf("Minus : %d\n", res);
  108. valread = read(sock, &sol, sizeof(float));
  109.  
  110. printf("Divide : %f", sol);
  111. return 0; }
  112. Server Side:
  113. #include <unistd.h> #include <stdio.h> #include <sys/socket.h> #include <stdlib.h> #include <netinet/in.h> #include <string.h> #include<math.h> #defne PORT 8080
  114. int main(int argc, char const *argv[]){
  115. int server_fd, new_socket, valread; struct sockaddr_in address;
  116. int opt = 1;
  117. int addrlen = sizeof(address);
  118. if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0){
  119. perror("socket failed");
  120. exit(EXIT_FAILURE); }
  121. if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,&opt, sizeof(opt))){
  122. perror("setsockopt"); exit(EXIT_FAILURE);
  123. }
  124. address.sin_family = AF_INET;
  125. address.sin_addr.s_addr = INADDR_ANY;
  126. address.sin_port = htons( PORT );
  127. if (bind(server_fd, (struct sockaddr *)&address,sizeof(address))<0){
  128. perror("bind failed");
  129. exit(EXIT_FAILURE); }
  130. If (listen(server_fd, 3) < 0){ perror("listen");
  131. exit(EXIT_FAILURE); }
  132. if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0){
  133. perror("accept"); exit(EXIT_FAILURE);
  134. }
  135. int n1,n2,sum,prod, minus, divide;
  136. valread = read( new_socket , &n1 , sizeof(int));
  137.  
  138. valread = read( new_socket , &n2 , sizeof(int)); sum = n1 + n2;
  139. prod = n1 * n2;
  140. minus = n1 - n2;
  141. divide = n1 / n2;
  142. send(new_socket , &sum , sizeof(int) , 0 ); send(new_socket , &prod , sizeof(int) , 0 ); send(new_socket , &minus , sizeof(int) , 0 ); send(new_socket , &divide , sizeof(float) , 0 ); return 0;
  143. }
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.    
  152. // Q3. server maintains a database of students with roll no. as key client sends a roll no. and server replies with all corressponding info. Connection should not terminate till the client wants?.
  153. // Code:
  154. // Client Side:
  155. #include <stdio.h> #include <sys/socket.h> #include <stdlib.h> #include <netinet/in.h> #include <string.h> #include<math.h> #include <arpa/inet.h> #include<unistd.h> #include<iostream> #include<bits/stdc++.h>
  156. using namespace std;
  157. #defne PORT 8080
  158. int main(int argc, char const *argv[]){
  159. struct sockaddr_in address;
  160. int sock = 0, valread;
  161. struct sockaddr_in serv_addr;
  162. char bufer[2256] = {0};
  163. if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
  164. printf("\n Socket creation error \n");
  165. return -1; }
  166. memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET;
  167. serv_addr.sin_port = htons(PORT);
  168. if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0){
  169. printf("\nInvalid address \n");
  170. return -1; }
  171. if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){ printf("\nConnection Failed \n");
  172. return -1;
  173. } while(true){
  174. string roll_id;
  175. cout<<"\n Enter The Roll_ID (or) 0 for exit:";
  176.  
  177. getline(cin, roll_id);
  178. send(sock, roll_id.data(), roll_id.size(), 0); if(roll_id == "0") break;
  179. string name, branch, year;
  180. int len = recv(sock, bufer, sizeof(bufer), 0); name.append(bufer, bufer + len);
  181. len = recv(sock, bufer, sizeof(bufer), 0); branch.append(bufer, bufer + len);
  182. len = recv(sock, bufer, sizeof(bufer), 0); year.append(bufer, bufer + len);
  183. cout<<" Roll_ID : "<<roll_id;
  184. cout<<"\n Name : "<<name;
  185. cout<<"\n Branch : "<< branch;
  186. cout<<"\n Year : "<< year;
  187. }
  188. return 0;
  189. }
  190. Server Side:
  191. #include <unistd.h> #include <stdio.h> #include <sys/socket.h> #include <stdlib.h> #include <netinet/in.h> #include <string.h> #include<math.h> #include <arpa/inet.h> #include<unistd.h> #include<iostream> #include<bits/stdc++.h>
  192. using namespace std;
  193. #defne PORT 8080
  194. struct stu{ string name; string branch; string year;
  195. };
  196. map<string, struct stu> mp;
  197. int main(int argc, char const *argv[]){
  198. int server_fd, new_socket, valread; struct sockaddr_in address;
  199. int opt = 1;
  200.  
  201. int addrlen = sizeof(address); char bufer[2256] = {0};
  202. mp["12je001221"].name = "Jitendra Kumar"; mp["12je001221"].branch = "CSE"; mp["12je001221"].year = "Final"; mp["12je001222"].name = "Jitendra Singh"; mp["12je001222"].branch = "CSE"; mp["12je001222"].year = "Final";
  203. if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0){ perror("socket failed");
  204. exit(EXIT_FAILURE); }
  205. if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,&opt, sizeof(opt))){
  206. perror("setsockopt"); exit(EXIT_FAILURE);
  207. }
  208. address.sin_family = AF_INET;
  209. address.sin_addr.s_addr = INADDR_ANY;
  210. address.sin_port = htons( PORT );
  211. if (bind(server_fd, (struct sockaddr *)&address,sizeof(address))<0){
  212. perror("bind failed");
  213. exit(EXIT_FAILURE); }
  214. if (listen(server_fd, 3) < 0){ perror("listen");
  215. exit(EXIT_FAILURE); }
  216. if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0){
  217. perror("accept"); exit(EXIT_FAILURE);
  218. } while(true){
  219. string roll_id;
  220. int len = recv(new_socket, bufer, sizeof(bufer), 0); roll_id.append(bufer, bufer + len);
  221. if(roll_id == "0") break;
  222. send(new_socket, (mp[roll_id].name).data(), (mp[roll_id].name).size(), 0); send(new_socket, (mp[roll_id].branch).data(), (mp[roll_id].branch).size(), 0); send(new_socket, (mp[roll_id].year).data(), (mp[roll_id].year).size(), 0);
  223. }
  224.  
  225. return 0;
  226. }
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242. // Q4. Server stores set of questions and their answers. Client sends a question to server and server replies with corresponding answers?.
  243. Client Side:
  244. #include <stdio.h> #include <sys/socket.h> #include <stdlib.h> #include <netinet/in.h> #include <string.h> #include<math.h> #include <arpa/inet.h> #include<unistd.h> #include<iostream> #include<bits/stdc++.h>
  245. using namespace std;
  246. #defne PORT 8080
  247. int main(int argc, char const *argv[]){
  248. struct sockaddr_in address;
  249. int sock = 0, valread;
  250. struct sockaddr_in serv_addr;
  251. char bufer[2256] = {0};
  252. if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
  253. printf("\n Socket creation error \n");
  254. return -1; }
  255. memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET;
  256. serv_addr.sin_port = htons(PORT);
  257. if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0){
  258. printf("\nInvalid address \n");
  259. return -1; }
  260. if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){ printf("\nConnection Failed \n");
  261. return -1;
  262. } while(true){
  263. string ques;
  264. cout<<"Enter The Question (all lowercase) (or) 0 to exit ?\n Q : "; getline(cin, ques);
  265.  
  266. send(sock, ques.data(), ques.size(), 0); if(ques == "0") break;
  267. int len = recv(sock, bufer, sizeof(bufer), 0); string res;
  268. res.append(bufer, bufer + len);
  269. cout<<" A : "<<res<<endl;
  270. }
  271. return 0;
  272. }
  273. Server Side:
  274. #include <unistd.h> #include <stdio.h> #include <sys/socket.h> #include <stdlib.h> #include <netinet/in.h> #include <string.h> #include<math.h> #include <arpa/inet.h> #include<unistd.h> #include<iostream> #include<bits/stdc++.h>
  275. using namespace std;
  276. #defne PORT 8080
  277. map<string, string> mp;
  278. int main(int argc, char const *argv[]){
  279. int server_fd, new_socket, valread; struct sockaddr_in address;
  280. int opt = 1;
  281. int addrlen = sizeof(address);
  282. char bufer[2256] = {0};
  283. mp["who am i"] = "My Boss";
  284. mp["who is your daddy"] = "You daddy";
  285. if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0){
  286. perror("socket failed");
  287. exit(EXIT_FAILURE); }
  288. if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,&opt, sizeof(opt))){
  289. }
  290. perror("setsockopt"); exit(EXIT_FAILURE);
  291.  
  292. address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons( PORT );
  293. if (bind(server_fd, (struct sockaddr *)&address,sizeof(address))<0){ perror("bind failed");
  294. exit(EXIT_FAILURE); }
  295. if (listen(server_fd, 3) < 0){ perror("listen");
  296. exit(EXIT_FAILURE); }
  297. if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0){
  298. perror("accept"); exit(EXIT_FAILURE);
  299. } while(true){
  300. string ques;
  301. int len = recv(new_socket, bufer, sizeof(bufer), 0); ques.append(bufer, bufer + len);
  302. if(ques == "0") break;
  303. send(new_socket, mp[ques].data(), mp[ques].size(), 0);
  304. }
  305. return 0;
  306. }
  307. Server Side:
  308. Client Side:
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323. //Q5. CRC(7,4) bit C++ Program. Client send a dataword with appended CRC. Server checks the dataword for any error.Server replies with “good data”, “bad data” to client ?.
  324. Client Side:
  325. #include <stdio.h> #include <sys/socket.h> #include <stdlib.h> #include <netinet/in.h> #include <string.h> #include<math.h> #include <arpa/inet.h> #include<unistd.h> #include<iostream> #include<bits/stdc++.h>
  326. using namespace std;
  327. #defne PORT 8080
  328. int main(int argc, char const *argv[]){
  329. struct sockaddr_in address;
  330. int sock = 0, valread;
  331. struct sockaddr_in serv_addr;
  332. char bufer[2256] = {0};
  333. if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
  334. printf("\n Socket creation error \n");
  335. return -1; }
  336. memset(&serv_addr, '0', sizeof(serv_addr));
  337. serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(PORT);
  338. if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0){ printf("\nInvalid address \n");
  339. return -1;
  340. }
  341. if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){ printf("\nConnection Failed \n");
  342. return -1;
  343.  
  344. }
  345. while(true){
  346. long long codeword, key;
  347. cout<<" Enter the dataword with appended CRC(7, 4) and key (or) 0 to exit: ";
  348. cin>>codeword>>key;
  349. send(sock, &codeword, sizeof(long long), 0); send(sock, &key, sizeof(long long), 0); if(codeword == 0 || key == 0) break;
  350. int len = recv(sock, bufer, sizeof(bufer), 0); string info;
  351. info.append(bufer, bufer + len);
  352. cout<<" Result : "<<info<<endl;
  353. }
  354. return 0;
  355. }
  356. Server Side:
  357. #include <unistd.h> #include <stdio.h> #include <sys/socket.h> #include <stdlib.h> #include <netinet/in.h> #include <string.h> #include<math.h> #include <arpa/inet.h> #include<unistd.h> #include<iostream> #include<bits/stdc++.h>
  358. using namespace std;
  359. #defne PORT 8080
  360. string Xor(string a, string b){ string res;
  361. for(int i=0; i<b.length(); i++){
  362. if(a[i] == b[i]) res += "0";
  363. else res += "1"; }
  364. return res; }
  365. string DecToBin(long long dec){
  366.  
  367. string res ; while(dec > 0){
  368. res = to_string(dec%2) + res;
  369. dec = dec/2; }
  370. return res; }
  371. long long BinToDec(string bin){ long long dec = 0;
  372. for(int i=0;i<bin.length(); i++){
  373. dec += pow(2, bin.length() - 1 - i)*(bin[i] - '0'); }
  374. return dec; }
  375. long long crc(string divident, string divisor){ int len = divisor.length();
  376. string tmp_res = divident.substr(0, len); while(len < divident.length()){
  377. if(tmp_res[0] == '1'){
  378. tmp_res = Xor(divisor, tmp_res).substr(1,3) + divident[len];
  379. } else{
  380. }
  381. len++; }
  382. if(tmp_res[0] == '1'){
  383. tmp_res = Xor(divisor, tmp_res);
  384. } else{
  385. }
  386. return BinToDec(tmp_res); }
  387. int main(int argc, char const *argv[]){
  388. int server_fd, new_socket, valread; struct sockaddr_in address;
  389. string tmp;
  390. for(int i=0; i< len; i++) tmp += '0';
  391. tmp_res = Xor(tmp, tmp_res).substr(1, 3) + divident[len];
  392. string tmp;
  393. for(int i=0; i< len; i++) tmp += '0';
  394. tmp_res = Xor(tmp, tmp_res).substr(1, 3) + divident[len];
  395.  
  396. int opt = 1;
  397. int addrlen = sizeof(address); char bufer[2256] = {0};
  398. if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0){ perror("socket failed");
  399. exit(EXIT_FAILURE); }
  400. if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,&opt, sizeof(opt))){
  401. perror("setsockopt"); exit(EXIT_FAILURE);
  402. }
  403. address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons( PORT );
  404. if (bind(server_fd, (struct sockaddr *)&address,sizeof(address))<0){ perror("bind failed");
  405. exit(EXIT_FAILURE); }
  406. if (listen(server_fd, 3) < 0){ perror("listen");
  407. exit(EXIT_FAILURE); }
  408. if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0){
  409. perror("accept"); exit(EXIT_FAILURE);
  410. }
  411. while(true){
  412. long long cwd, ky;
  413. valread = read(new_socket, &cwd, sizeof(long long)); valread = read(new_socket, &ky, sizeof(long long)); if(cwd == 0 || ky == 0) break;
  414. string codeword = DecToBin(cwd);
  415. string key = DecToBin(ky);
  416. string res;
  417. crc(codeword, key) == 0 ? res = "good data" : res = "bad data";
  418.  
  419. send(new_socket, res.data(), res.size(), 0); }
  420. return 0;
  421. }
  422. Server Side:
  423. Client Side:
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443. //Q7: WAP to create a two way chat application where both processes can send and receive messages?
  444. Client Side:
  445. #include <stdio.h> #include <sys/socket.h> #include <stdlib.h> #include <netinet/in.h> #include <string.h> #include<math.h> #include <arpa/inet.h> #include<unistd.h> #include<iostream> #include<bits/stdc++.h>
  446. using namespace std;
  447. #defne PORT  s0s0
  448. int main(int argc, char const *argv[]){ struct sockaddr_in address;
  449. int sock = 0, valread;
  450. struct sockaddr_in serv_addr; char bufer[[256] = {0};
  451. if((sock = socket(AF_INET , SOCK_ST REAM, 0)) < 0){ printf("\n Socket creation error \n");
  452. return -1;
  453. }
  454. memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET ;
  455. serv_addr.sin_port = htons(PORT );
  456. if(inet_pton(AF_INET , "1[27.0.0.1", &serv_addr.sin_addr)<=0){
  457. printf("\nInvalid address \n");
  458. return -1; }
  459. if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){ printf("\nConnection Failed \n");
  460. return -1;
  461. }
  462. cout<<"Welcome to Chat Room :\n";
  463. while(true){
  464. string chat, chat_line, reply;
  465. cout<<"\nClient : ";
  466. while(std::getline(std::cin, chat_line) && !chat_line.empty() )
  467. chat += chat_line + "+" ;
  468. send(sock, chat.data(), chat.size(), 0);
  469. int len = recv(sock, bufer, sizeof(bufer), 0); reply.append(bufer, bufer + len); cout<<"\nBot : ";
  470. for(int i=0; i<reply.length(); i++){
  471. if(reply[i] == '+') cout<<endl;
  472. else cout<<reply[i]; }
  473. if(reply == "bye+") break; }
  474. return 0; }
  475. Server Side:
  476. #include <unistd.h> #include <stdio.h> #include <sys/socket.h> #include <stdlib.h> #include <netinet/in.h> #include <string.h> #include<math.h> #include <arpa/inet.h> #include<unistd.h> #include<iostream> #include<bits/stdc++.h>
  477. using namespace std;
  478. #defne PORT  s0s0
  479. int main(int argc, char const *argv[]){ int server_fd, new_socket, valread; struct sockaddr_in address;
  480. int opt = 1;
  481. int addrlen = sizeof(address); char bufer[[256] = {0};
  482. if ((server_fd = socket(AF_INET , SOCK_ST REAM, 0)) == 0){ perror("socket failed");
  483. exit(EXIT _FAILURE); }
  484. if (setsockopt(server_fd, SOL_SOCKET , SO_REUSEADDR | SO_REUSEPORT ,&opt, sizeof(opt))){
  485. perror("setsockopt"); exit(EXIT _FAILURE);
  486. }
  487. address.sin_family = AF_INET ;
  488. address.sin_addr.s_addr = INADDR_ANY;
  489. address.sin_port = htons( PORT  );
  490. if (bind(server_fd, (struct sockaddr *)&address,sizeof(address))<0){
  491. perror("bind failed");
  492. exit(EXIT _FAILURE); }
  493. if (listen(server_fd, 3) < 0){ perror("listen");
  494. exit(EXIT _FAILURE); }
  495.  
  496. if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0){
  497. perror("accept"); exit(EXIT _FAILURE);
  498. }
  499. cout<<"Welcome to Chat Room :\n";
  500. while(true){
  501. string chat, reply, reply_line;
  502. int len = recv(new_socket, bufer, sizeof(bufer), 0); chat.append(bufer, bufer + len);
  503. cout<<"\nClient : ";
  504. for(int i=0; i<chat.size(); i++){
  505. if(chat[i] == '+') cout<<"\n";
  506. else cout<<chat[i]; }
  507. cout<<"\nBot : ";
  508. while(getline(cin, reply_line) && !reply_line.empty())
  509. reply += reply_line + "+"; send(new_socket, reply.data(), reply.size(), 0); if(chat == "bye+") break;
  510. }
  511. return 0; }
  512. Server Side:
  513.  Client Side:
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535.  
  536.  
  537.  
  538.  
  539.  
  540.  
  541.  
  542. //Q8: WAP to create an authentication server where client sends a password in encrypted form (use RSA algorithm for encryption) and user name. Server decrypt the password and verify it (from it’s own database) for proper authentication?
  543. Client Side:
  544. #include <stdio.h> #include <sys/socket.h> #include <stdlib.h> #include <netinet/in.h> #include <string.h> #include<math.h> #include <arpa/inet.h> #include<unistd.h> #include<iostream> #include<bits/stdc++.h>
  545. using namespace std;
  546. #defne PORT  s0s0
  547. #defne loop(i, a, b) for(i=a; i<b; i++)
  548. int solve_ext_gcd(int a,int b, int &x,int &y){ if(a==0){
  549. x=0; y=1;
  550. return b; }
  551. int x1,y1;
  552. int g = solve_ext_gcd(b%a,a,x1,y1); x=y1-(b/a)*x1;
  553. y=x1;
  554. return g;
  555. }
  556. int solve_modulo_multi_inverse(int a, int m){ int x, y;
  557. int g = solve_ext_gcd(a, m, x, y);
  558. if (g != 1) {
  559. cout <<"Inverse doesn't exist"; return -1; }
  560. else{
  561. int res=(x%m+m)%m; return res;
  562. } }
  563. int solve_fast_modulo_exponent(int x, int y, int n){ int res = 1; x = x%n;
  564. while(y>0){
  565. if(y&1) res = (res*x)%n; y >>= 1;
  566. x = (x*x)%n;
  567.  
  568. }
  569. return res; }
  570. void init_rsa(int *public_key, int *private_key, int *n){ int p = 73, q = [2[27;
  571. int phi_n = (p - 1) * (q - 1);
  572. *public_key = [25;
  573. *n = p*q;
  574. *private_key = solve_modulo_multi_inverse(*public_key, phi_n); }
  575. int main(int argc, char const *argv[]){ struct sockaddr_in address;
  576. int sock = 0, valread;
  577. struct sockaddr_in serv_addr; char bufer[[256] = {0};
  578. if((sock = socket(AF_INET , SOCK_ST REAM, 0)) < 0){ printf("\n Socket creation error \n");
  579. return -1;
  580. }
  581. memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET ;
  582. serv_addr.sin_port = htons(PORT );
  583. if(inet_pton(AF_INET , "1[27.0.0.1", &serv_addr.sin_addr)<=0){
  584. printf("\nInvalid address \n");
  585. return -1; }
  586. if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){ printf("\nConnection Failed \n");
  587. return -1;
  588. }
  589. cout<<"Welcome to Authentication Services : \n"; int public_key, private_key, n, i; init_rsa(&public_key, &private_key, &n);
  590. while(true){
  591. string username, password, encrypted_password, status; cout<<"\n username : "; getline(cin, username); cout<<" password : "; getline(cin, password);
  592. loop(i, 0, password.length()-1){
  593. encrypted_password += to_string(solve_fast_modulo_exponent(password[i], public_key, n)) + "*";
  594. }
  595. encrypted_password += to_string(solve_fast_modulo_exponent(password[password.length()-1], public_key, n));
  596. send(sock, username.data(), username.size(), 0);
  597. send(sock, encrypted_password.data(), encrypted_password.size(), 0);
  598.  
  599. int len = recv(sock, bufer, sizeof(bufer), 0); status.append(bufer, bufer + len); cout<<" status : "<<status<<endl;
  600. }
  601. return 0;
  602. }
  603. Server Side:
  604. #include <unistd.h> #include <stdio.h> #include <sys/socket.h> #include <stdlib.h> #include <netinet/in.h> #include <string.h> #include<math.h> #include <arpa/inet.h> #include<unistd.h> #include<iostream> #include<bits/stdc++.h>
  605. using namespace std;
  606. #defne PORT  s0s0
  607. #defne loop(i, a, b) for(i=a; i<b; i++)
  608. int solve_ext_gcd(int a,int b, int &x,int &y){ // Extended GCD if(a==0){
  609. x=0; y=1;
  610. return b; }
  611. int x1,y1;
  612. int g = solve_ext_gcd(b%a,a,x1,y1); x=y1-(b/a)*x1;
  613. y=x1;
  614. return g;
  615. }
  616. int solve_modulo_multi_inverse(int a, int m){ int x, y;
  617. int g = solve_ext_gcd(a, m, x, y);
  618. if (g != 1) {
  619. cout <<"Inverse doesn't exist"; return -1; }
  620. else{
  621. int res=(x%m+m)%m; return res;
  622. } }
  623. int solve_fast_modulo_exponent(int x, int y, int n){ int res = 1; x = x%n;
  624. while(y>0){
  625. if(y&1) res = (res*x)%n;
  626.  
  627. y >>= 1;
  628. x = (x*x)%n; }
  629. return res; }
  630. void init_rsa(int *public_key, int *private_key, int *n){ int p = 73, q = [2[27;
  631. int phi_n = (p - 1) * (q - 1);
  632. *public_key = [25;
  633. *n = p*q;
  634. *private_key = solve_modulo_multi_inverse(*public_key, phi_n); }
  635. int main(int argc, char const *argv[]){ int server_fd, new_socket, valread; struct sockaddr_in address;
  636. int opt = 1;
  637. int addrlen = sizeof(address);
  638. char bufer[[256] = {0};
  639. map<string, string> mp; mp["alokpatra199s@gmail.com"] = "patra_style";
  640. if ((server_fd = socket(AF_INET , SOCK_ST REAM, 0)) == 0){ perror("socket failed");
  641. exit(EXIT _FAILURE); }
  642. if (setsockopt(server_fd, SOL_SOCKET , SO_REUSEADDR | SO_REUSEPORT ,&opt, sizeof(opt))){
  643. perror("setsockopt"); exit(EXIT _FAILURE);
  644. }
  645. address.sin_family = AF_INET ;
  646. address.sin_addr.s_addr = INADDR_ANY;
  647. address.sin_port = htons( PORT  );
  648. if (bind(server_fd, (struct sockaddr *)&address,sizeof(address))<0){
  649. perror("bind failed");
  650. exit(EXIT _FAILURE); }
  651. if (listen(server_fd, 3) < 0){ perror("listen");
  652. exit(EXIT _FAILURE); }
  653. if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0){
  654. perror("accept"); exit(EXIT _FAILURE);
  655. }
  656. int public_key, private_key, n, i; init_rsa(&public_key, &private_key, &n);
  657.  
  658. while(true){
  659. string username, decrypted_password, status; vector<int> password;
  660. int len = recv(new_socket, bufer, sizeof(bufer), 0); username.append(bufer, bufer + len);
  661. len = recv(new_socket, bufer, sizeof(bufer), 0); char delim[] = "*";
  662. char *token = strtok(bufer, delim);
  663. while(token) {
  664. password.push_back(stoi(token));
  665. token = strtok(NULL, delim); }
  666. if(mp.fnd(username) == mp.end()){ status = "Incorrect Username";
  667. } else{
  668. loop(i, 0, password.size()){
  669. decrypted_password += (char)solve_fast_modulo_exponent(password[i], private_key, n);
  670. }
  671. if(decrypted_password == mp[username]){
  672. status = "Success"; }
  673. else{
  674. status = "Incorrect Password";
  675. } }
  676. send(new_socket, status.data(), status.size(), 0);
  677. }
  678. return 0; }
  679. Client Side:
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  //Q9: Hospital Management System.
  704. // Step by Step functionalities needs to be implemented:
  705. // I. Client will enter patient details along with department name he/she wants to
  706. // visit to the server.
  707. // II. Server will hold all doctors’ name for each department in a database.
  708. // III. Whenever any patient comes from client, server will check doctor’s availability and assign one doctor to the patient for the concerned department.
  709. // IV. If no doctor will be available in that time no doctor would be assigned to that patient.
  710. // V. After every 1 minute, each assigned doctor will be free.
  711. // VI. Server will maintain one queue for the waited patient, and assign free doctor
  712. // accordingly
  713. //VII. Connection will be closed when client wants to.
  714. Client Side:
  715. #include <stdio.h> #include <sys/socket.h> #include <stdlib.h> #include <netinet/in.h> #include <string.h> #include<math.h> #include <arpa/inet.h> #include<unistd.h> #include<iostream> #include<bits/stdc++.h>
  716. using namespace std;
  717. #defne PORT  s0s0
  718. #defne loop8i, a, b ior8iaa; i<b; i++
  719. int main8int argc, char const *argv[] { struct sockaddr_in address;
  720. int sock a 0, valread;
  721. struct sockaddr_in serv_addr; char bufer[[256] a {0};
  722. ii88sock a socket8AF_INET , SOCK_ST REAM, 0 printi8"\n Socket creation error \n" ; return -1;
  723. < 0 {
  724. }
  725. memset8&serv_addr, '0', sizeoi8serv_addr ; serv_addr.sin_iamily a AF_INET ;
  726. serv_addr.sin_port a htons8PORT  ;
  727. ii8inet_pton8AF_INET , "1[27.0.0.1", &serv_addr.sin_addr <a0 {
  728. printi8"\nInvalid address \n" ;
  729. return -1; }
  730. ii 8connect8sock, 8struct sockaddr * &serv_addr, sizeoi8serv_addr printi8"\nConnection Failed \n" ;
  731. return -1;
  732. < 0 {
  733. }
  734. cout<<"~ Welcome to ISM Health Center ~\n";
  735. while8true {
  736. string patient_name, department_to_visit, patient_sex, patient_age,
  737. patient_status;
  738. cout<<"\nPlease enter the iollowing patient details 8or to exit:\n"; cout<<"\nName: "; getline8cin, patient_name ;
  739. cout<<"\nAge: "; getline8cin, patient_age ;
  740. cout<<"\nSex:"; getline8cin, patient_sex ;
  741. cout<<"\nDepartment to visit: "; getline8cin, department_to_visit ;
  742. string send_data a patient_name + "@" + patient_age +"@" + patient_sex + "@" + department_to_visit;
  743. send8sock, send_data.data8 , send_data.size8 , 0 ; int len a recv8sock, bufer, sizeoi8bufer , 0 ; patient_status.append8bufer, bufer + len ; cout<<"Patient Status:"<<patient_status<<endl;
  744. } return 0; }
  745. Server Side:
  746. #include <unistd.h> #include <stdio.h> #include <sys/socket.h> #include <stdlib.h> #include <netinet/in.h> #include <string.h> #include<math.h> #include <arpa/inet.h> #include<iostream> #include<bits/stdc++.h>
  747. using namespace std;
  748. #defne PORT  s0s0
  749. #defne loop8i, a, b ior8iaa; i<b; i++
  750. typedei struct Doctor{ string name;
  751. bool avialable;
  752. }doc;
  753. typedei struct Department{ string name;
  754. doc doctor[[2];
  755. }dept;
  756. typedei struct Patient{ string name;
  757.  
  758. string age;
  759. string sex;
  760. string department_to_visit;
  761. }pat;
  762. dept departments[[2];
  763. void init_db8 {
  764. departments[0].name a "ENT "; departments[0].doctor[0].name a "Dr. Khana"; departments[0].doctor[0].avialable a true; departments[0].doctor[1].name a "Dr. Sharma"; departments[0].doctor[1].avialable a true;
  765. departments[1].name a "A&E"; departments[1].doctor[0].name a "Dr. Kumar"; departments[1].doctor[0].avialable a true; departments[1].doctor[1].name a "Dr. Singh"; departments[1].doctor[1].avialable a true;
  766. }
  767. int main8int argc, char const *argv[] { queue<pat> waiting_queue;
  768. int server_id, new_socket, valread; struct sockaddr_in address;
  769. int opt a 1;
  770. int addrlen a sizeoi8address ;
  771. char bufer[51[2] a {0};
  772. ii 88server_id a socket8AF_INET , SOCK_ST REAM, 0 perror8"socket iailed" ;
  773. exit8EXIT _FAILURE ; }
  774. aa 0 {
  775. ii 8setsockopt8server_id, SOL_SOCKET , SO_REUSEADDR | SO_REUSEPORT ,&opt, sizeoi8opt {
  776. perror8"setsockopt" ; exit8EXIT _FAILURE ;
  777. }
  778. address.sin_iamily a AF_INET ;
  779. address.sin_addr.s_addr a INADDR_ANY;
  780. address.sin_port a htons8 PORT  ;
  781. ii 8bind8server_id, 8struct sockaddr * &address,sizeoi8address
  782. perror8"bind iailed" ;
  783. exit8EXIT _FAILURE ; }
  784. ii 8listen8server_id, 3 < 0 { perror8"listen" ;
  785. exit8EXIT _FAILURE ; }
  786. <0 {
  787. ii 88new_socket a accept8server_id, 8struct sockaddr * &address, 8socklen_t* &addrlen <0 {
  788. perror8"accept" ; exit8EXIT _FAILURE ;
  789.  
  790. } init_db8 ;
  791. int i, j; while8true {
  792. vector<string> p_data;
  793. int tag a 1;
  794. string status;
  795. int len a recv8new_socket, bufer, sizeoi8bufer , 0 ; char delim[] a "@";
  796. char *token a strtok8bufer, delim ; while8token {
  797. p_data.push_back8token ;
  798. token a strtok8NULL, delim ; }
  799. len a p_data.size8 ;
  800. bool fag a ialse;
  801. ior8int ka0; k<len; k++ {
  802. ii8p_data[k] aa "0" { fag a true;
  803. break;
  804. } }
  805. ii8fag break;
  806. ior8ia0; i<51[2; i++
  807. pat patient;
  808. patient.name a p_data[0]; patient.age a p_data[1];
  809. patient.sex a p_data[[2]; patient.department_to_visit a p_data[3]; p_data.clear8 ;
  810. waiting_queue.push8patient ;
  811. bufer[i] a '\0';
  812. ii8waiting_queue.size8 > 3 { init_db8 ;
  813. }
  814. pat patient_new a waiting_queue.iront8 ; waiting_queue.pop8 ;
  815. ior8ia0; i<[2; i++ {
  816. ii8departments[i].name aa patient_new.department_to_visit
  817. }
  818. ii8iaa[2 {
  819. status a "T his Department doesn't exits"; }
  820. else{
  821. ior8ja0; j<[2; j++ {
  822. ii8departments[i].doctor[j].avialable { departments[i].doctor[j].avialable a ialse; break;
  823. } }
  824. ii8jaa[2 {
  825. status a "Doctors are busy please wait ior some time"; tag a 0;
  826. } else{
  827. break;
  828.  
  829. status a departments[i].doctor[j].name + " is assinged to " + patient_new.name;
  830. } }
  831. ii8tagaa0 { waiting_queue.push8patient_new ;
  832. }
  833. send8new_socket, status.data8 , status.size8 , 0 ; }
  834. return 0; }
  835. Output:
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853. // Q10: Implement a data transfer simulation between 3 nodes using NS2?
  854. set ns [new Simulator] # Event scheduler object creation
  855. set nf [open prog1.nam w] # creating nam objects $ns namtrace-all $nf
  856. set nd [open prog1.tr w] # creating trace objects $ns trace-all $nd
  857. proc fnish { } { global ns nf nd $ns flush-trace close $nf
  858. # fnish procedure
  859. close $nd
  860. exec nam prog1.nam &
  861. exec awk -f data.awk prog1.tr > output.tr & exit 0
  862. }
  863. $ns color 1 darkmagenta $ns color 2 yellow
  864. $ns color 3 blue
  865. $ns color 4 green
  866. $ns color 5 black
  867. set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node]
  868. set server 0 set router 1 set client1 2 set client2 3
  869. # Setting color IDs
  870. # Creating Network
  871. # Setting Nodes
  872. $ns duplex-link $n0 $n1 1Mb 10ms DropTail $ns duplex-link $n1 $n2 512Kb 10ms DropTail $ns duplex-link $n1 $n3 512Kb 10ms DropTail
  873. $ns queue-limit $n1 $n2 5 $ns queue-limit $n1 $n3 5
  874. $ns duplex-link-op $n0 $n1 orient right
  875. $ns duplex-link-op $n1 $n2 orient right-down $ns duplex-link-op $n1 $n3 orient right-up
  876. # Creating Duplex Link
  877. $ns at 0.0 "$n0 label Server" $ns at 0.0 "$n1 label Router" $ns at 0.0 "$n2 label Client_1" $ns at 0.0 "$n3 label Client_2"
  878. # Labelling
  879. $ns at 0.0 "$n0 color green" $ns at 0.0 "$n2 color blue" $ns at 0.0 "$n3 color blue"
  880. $n0 shape hexagon $n2 shape hexagon $n3 shape hexagon
  881. set udp0 [new Agent/UDP] $ns attach-agent $n0 $udp0
  882. # Data Transfer between Nodes
  883. set cbr0 [new Application/Trafc/C/R] $cbr0 set packetSize_ 500
  884. $cbr0 set interval_ 0.005
  885. $cbr0 attach-agent $udp0
  886. set udp1 [new Agent/UDP] $ns attach-agent $n1 $udp1
  887. set cbr1 [new Application/Trafc/C/R] $cbr1 set packetSize_ 500
  888. $cbr1 set interval_ 0.005
  889. $cbr1 attach-agent $udp1
  890. set sink [new Agent/Null] $ns attach-agent $n2 $sink $ns connect $udp0 $sink
  891. set sink1 [new Agent/Null] $ns attach-agent $n3 $sink1 $ns connect $udp1 $sink1
  892. $udp0 set fd_ 4 $udp1 set fd_ 1
  893. $ns at 0.2 "$cbr0 start" $ns at 0.5 "$cbr1 start"
  894. $ns at 4.5 "$cbr0 stop" $ns at 4.7 "$cbr1 stop"
  895. #Calling fnish procedure $ns at 5.0 "fnish"
  896. $ns run
  897. # Setting flow color
  898. # data packet generation starting time
  899. # data packet generation ending time
  900.  
  901. Code(.awk):
  902. BEGIN {
  903. dcount = 0;
  904. rcount = 0; }
  905. {
  906. event = $1;
  907. if(event == "d"){ dcount++;
  908. }
  909. if(event == "r"){
  910. rcount++; }
  911. }
  912. END {
  913. printf("packets dropped : %d\n ",dcount); printf("packets recieved : %d\n ",rcount);
  914. }
  915. Output:
  916.  
  917.  
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.  
  929.  
  930.  
  931.  
  932.  
  933. // Q11: Implement DSDV routing protocol using NS2?
  934. Code(.tcl):
  935. set val(chan) Channel/WirelessChannel set val(prop) Propagation/TwoRayGround set val(netif) Phy/WirelessPhy
  936. set val(mac) Mac/802_11
  937. set val(ifq) CMUPriQueue
  938. set val(ll) LL
  939. set val(ant) Antenna/OmniAntenna set val(ifqlen) 50
  940. ;# channel type
  941. ;# radio-propagation model
  942. ;# network interface type
  943. ;# MAC type
  944. ;# interface queue type ;# link layer type
  945. ;# antenna model
  946. ;# max packet in ifq
  947. ;# number of mobilenodes
  948. ;# routing protocol
  949. ;# X dimension of topography ;# Y dimension of topography ;# time of simulation end
  950. set val(nn) 3
  951. set val(rp) DSDV set val(x) 500 set val(y) 400 set val(stop) 150
  952. set ns
  953. [new Simulator]
  954. # Event scheduler object creation
  955. set tracefd
  956. set windowVsTime2 [open win.tr w] set namtrace [open dsr.nam w]
  957. [open dsr.tr w]
  958. $ns trace-all $tracefd
  959. $ns namtrace-all-wireless $namtrace $val(x) $val(y)
  960. set topo [new Topography] # set up topography object $topo load_flatgrid $val(x) $val(y)
  961. create-god $val(nn)
  962. $ns node-confg -adhocRouting $val(rp) \ -llType $val(ll) \
  963. -macType $val(mac) \ -ifqType $val(ifq) \
  964. -ifqLen $val(ifqlen) \ -antType $val(ant) \ -propType $val(prop) \ -phyType $val(netif) \ -channelType $val(chan) \ -topoInstance $topo \ -agentTrace ON \
  965. -routerTrace ON \ -macTrace OFF \ -movementTrace ON
  966. for {set i 0} {$i < $val(nn) } { incr i } { set node_($i) [$ns node]
  967. }
  968. # confgure the nodes
  969. #Creating trace fle and nam fle
  970.  
  971. $node_(0) set X_ 5.0 # Provide initial location of mobilenodes $node_(0) set Y_ 5.0
  972. $node_(0) set Z_ 0.0
  973. $node_(1) set X_ 490.0 $node_(1) set Y_ 285.0 $node_(1) set Z_ 0.0
  974. $node_(2) set X_ 150.0 $node_(2) set Y_ 240.0 $node_(2) set Z_ 0.0
  975. $ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0" # Generation of movements $ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0"
  976. $ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"
  977. # Set a TCP connection between node_(0) and node_(1)
  978. set tcp [new Agent/TCP/Newreno] $tcp set class_ 2
  979. set sink [new Agent/TCPSink]
  980. $ns attach-agent $node_(0) $tcp $ns attach-agent $node_(1) $sink $ns connect $tcp $sink
  981. set ftp [new Application/FTP] $ftp attach-agent $tcp
  982. $ns at 10.0 "$ftp start"
  983. proc plotWindow {tcpSource fle} { global ns
  984. set time 0.01
  985. set now [$ns now]
  986. # Printing the window size
  987. set cwnd [$tcpSource set cwnd_]
  988. puts $fle "$now $cwnd"
  989. $ns at [expr $now+$time] "plotWindow $tcpSource $fle" } $ns at 10.1 "plotWindow $tcp $windowVsTime2"
  990. for {set i 0} {$i < $val(nn)} { incr i } { # Defne node initial position in nam # 30 defnes the node size for nam
  991. $ns initial_node_pos $node_($i) 30
  992. }
  993. # Telling nodes when the simulation ends
  994. for {set i 0} {$i < $val(nn) } { incr i } { $ns at $val(stop) "$node_($i) reset";
  995. }
  996. # ending nam and the simulation
  997. $ns at $val(stop) "$ns nam-end-wireless $val(stop)"
  998.  
  999. $ns at $val(stop) "stop"
  1000. $ns at 150.01 "puts \"end simulation\" ; $ns halt" proc stop {} {
  1001. global ns tracefd namtrace $ns flush-trace
  1002. close $tracefd
  1003. close $namtrace
  1004. exec nam dsr.nam & exit 0
  1005. }
  1006. $ns run
  1007. Output:
  1008.  
  1009.  
  1010.  
  1011.  
  1012.  
  1013.  
  1014.  
  1015.  
  1016.  
  1017.  
  1018.  
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.  
  1029. // Q12:Implement AODV routing protocol using tcl-script in NS2?
  1030. Code(.tcl):
  1031. set val(chan)
  1032. set val(prop)
  1033. set val(netif)
  1034. set val(mac)
  1035. set val(ifq)
  1036. set val(ll)
  1037. set val(ant)
  1038. set val(ifqlen)  50;
  1039. Channel/WirelessChannel; # channel
  1040. Propagation/TwoRayGround;# radio-propagation
  1041. Phy/WirelessPhy;
  1042. Mac/802_11;
  1043. Queue/DropTail;
  1044. LL;
  1045. Antenna/OmniAntenna;
  1046.                                           # network interface
  1047.                                           # MAC type
  1048.                                           # interface queue
  1049.                                           # link layer
  1050.                                           # antenna model
  1051.                                           # max packet in ifq
  1052.                                           # #mobilenodes
  1053.                                           # routing protocol
  1054.                                           # X of topography
  1055.                                           # Y of topography
  1056.                                           # time of simulation
  1057. $ns trace-all $tracefd
  1058. $ns use-newtrace
  1059. $ns namtrace-all-wireless $namtrace $val(x) $val(y)
  1060. set topo          [new Topography]
  1061. $topo load_flatgrid $val(x) $val(y)
  1062. create-god $val(nn)
  1063. #
  1064. # Create nn mobilenodes [$val(nn)] and attach them to the
  1065. channel.
  1066. #
  1067. # configure the nodes
  1068. $ns node-config -adhocRouting $val(rp) \
  1069.                 -llType $val(ll) \
  1070.                 -macType $val(mac) \
  1071.                 -ifqType $val(ifq) \
  1072.                 -ifqLen $val(ifqlen) \
  1073.                 -antType $val(ant) \
  1074.                 -propType $val(prop) \
  1075.                 -phyType $val(netif) \
  1076.                 -channelType $val(chan) \
  1077.                 -topoInstance $topo \
  1078.                 -agentTrace ON \
  1079.                 -routerTrace ON \
  1080.                 -macTrace OFF \
  1081.                 -movementTrace ON
  1082. set val(nn)
  1083. set val(rp)
  1084. set val(x)
  1085. set val(y)
  1086. set val(stop)
  1087. 4;
  1088. AODV;
  1089. 500;
  1090. 400;
  1091. 150;
  1092. [new Simulator]
  1093. [open simple-dsdv.tr w]
  1094. set ns
  1095. set tracefd
  1096. set windowVsTime2 [open win.tr w]
  1097. set namtrace      [open simwrls1.nam w]
  1098. for {set i 0} {$i < $val(nn) } { incr i } {
  1099.          set node_($i) [$ns node]
  1100. }
  1101. # Provide initial location of mobile nodes
  1102. for {set i 0} {$i < $val(nn)} {incr i} {
  1103.   $node_($i) set X_ [expr rand()*500]
  1104.   $node_($i) set Y_ [expr rand()*400]
  1105.   $node_($i) set Z_ 0
  1106. }
  1107. # Generation of movements
  1108. $ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0"
  1109. $ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0"
  1110. $ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"
  1111. # Set a TCP connection between node_(0) and node_(1)
  1112. set tcp [new Agent/TCP/Newreno]
  1113. $tcp set class_ 2
  1114. set sink [new Agent/TCPSink]
  1115. $ns attach-agent $node_(0) $tcp
  1116. $ns attach-agent $node_(1) $sink
  1117. $ns connect $tcp $sink
  1118. set ftp [new Application/FTP]
  1119. $ftp attach-agent $tcp
  1120. $ns at 10.0 "$ftp start"
  1121. # Define node initial position in nam
  1122. for {set i 0} {$i < $val(nn)} { incr i } {
  1123.      # 30 defines the node size for nam
  1124.      $ns initial_node_pos $node_($i) 30
  1125. }
  1126. # Telling nodes when the simulation ends
  1127. for {set i 0} {$i < $val(nn) } { incr i } {
  1128.     $ns at $val(stop) "$node_($i) reset";
  1129. }
  1130. # ending nam and the simulation
  1131. $ns at $val(stop) "$ns nam-end-wireless $val(stop)"
  1132. $ns at $val(stop) "stop"
  1133. $ns at 150.01 "puts \"end simulation\" ; $ns halt"
  1134. proc stop {} {
  1135.     global ns tracefd namtrace
  1136.     $ns flush-trace
  1137.     close $tracefd
  1138.     close $namtrace
  1139.     exec nam simwrls1.nam &
  1140. }
  1141. $ns run
  1142.  
  1143. Output:
  1144.  
  1145.  
  1146.  
  1147.  
  1148.  
  1149.  
  1150.  
  1151.  
  1152.  
  1153.  
  1154.  
  1155.  
  1156.  
  1157.  
  1158.  
  1159.  
  1160.  
  1161.  
  1162.  
  1163.  
  1164.  
  1165.  
  1166.  
  1167.  
  1168.  
  1169.  
  1170.  
  1171.  
  1172.  
  1173.  
  1174.  
  1175.  
  1176.  
  1177.  
  1178.  
  1179.  
  1180. // 1.   WAP in C to find cluster size and reuse factor of a cellular system.
  1181. // 2.   WAP in C to calculate the channel capacity of a cellular system.
  1182. // WAP to check the validity for a given cluster size of a cellular system
  1183. #include<bits/stdc++.h>
  1184. #include<math.h>
  1185. using namespace std;
  1186.  
  1187. #define ll long long
  1188. #define loop(i, a, b) for(i=a; i<b; i++)
  1189. #define rloop(i, a, b) for(i=a; i>b; i++)
  1190.  
  1191. void channel_capacity(){
  1192.   ll i, j, itr, N, no_of_channels_per_cell, no_clusters;
  1193.   while(true){
  1194.     cout<<"\n Enter(tab separated) i, j, no_of_channels_per_cell, no_clusters: ";
  1195.     cin>>i>>j>>no_of_channels_per_cell>>no_clusters;
  1196.  
  1197.     N = i * i + i * j + j * j ;
  1198.  
  1199.     cout<<"\n Channel Capacity: "<<no_of_channels_per_cell * N * no_clusters<<endl;
  1200.   }
  1201. }
  1202.  
  1203. void cluster_size_and_freq_reuse_factor(){
  1204.   while(true){
  1205.     double n, S_I, io, temp;
  1206.     ll N;
  1207.     cout<<"\n Enter path loss exponent(n), signal to interference ratio(S/I), #co_channels cell interfer: ";
  1208.     cin>>n>>S_I>>io;
  1209.     temp = S_I * io ;
  1210.     N = pow(temp, ((double)2 / n)) / 3 ;
  1211.     cout<<"\n Cluster Size: "<<N<<endl;
  1212.     cout<<"\n frequency reuse factor: "<<"1/"<<N<<endl;
  1213.   }
  1214. }
  1215.  
  1216. void validate_cluster_size(){
  1217.   while(true){
  1218.     ll N, i, j;
  1219.     cout<<"\n Enter cluster size(N): ";
  1220.     cin>>N;
  1221.     loop(i, 1, N){
  1222.       loop(j, 1, N){
  1223.         if(N == (i * i + i * j + j * j)){
  1224.           cout<<"\n Valid Cluster\n";
  1225.           return ;
  1226.         }
  1227.       }
  1228.     }
  1229.     cout<<"\n Invalid Cluster Size\n";
  1230.   }
  1231. }
  1232.  
  1233. void cdma(){
  1234.   ll d[4], i, j, c;
  1235.   cout<<"\n Enter 4 data points: ";
  1236.   cin>>d[0]>>d[1]>>d[2]>>d[3];
  1237.   ll n = 4, w[][4]={ {1,1,1,1},
  1238.                     {1,-1,1,-1},
  1239.                     {1,1,-1,-1},
  1240.                     {1,-1,-1,1} },f[n];
  1241.   cout<<"\n Chips for each cahnnel are: "<<endl;
  1242.   loop(j, 1, n + 1){
  1243.     cout<<" C"<<j<<": [";
  1244.     loop(i, 0, n - 1)
  1245.       cout<<w[j-1][i]<<", ";
  1246.     cout<<w[j-1][n-1]<<"]"<<endl;
  1247.   }
  1248.  
  1249.   loop(i, 0, n){
  1250.     c = 0;
  1251.     loop(j, 0, n){
  1252.       c += (w[j][i] * d[j]);
  1253.     }
  1254.     f[i] = c;
  1255.   }
  1256.  
  1257.   cout<<"\n Data Sent: [";
  1258.   loop(i, 0, n - 1) cout<<f[i]<<", ";
  1259.   cout<<f[n-1]<<"]"<<endl;
  1260.  
  1261.   loop(i, 0, n){
  1262.     c = 0;
  1263.     loop(j, 0, n)
  1264.       c += (w[i][j] * f[j]) ;
  1265.     cout<<"\n Bit Sent By "<<i<<" = "<<c / n;
  1266.   }
  1267.   cout<<endl;
  1268. }
  1269.  
  1270. int main(int argc, char const *argv[]) {
  1271.   // channel_capacity();
  1272.   // cluster_size_and_freq_reuse_factor();
  1273.   //validate_cluster_size();
  1274.   // cdma();
  1275.   return 0;
  1276. }
  1277.  
  1278.  
  1279.  
  1280.  
  1281.  
  1282.  
  1283.  
  1284.  
  1285.  
  1286.  
  1287.  
  1288.  
  1289.  
  1290.  
  1291.  
  1292.  
  1293.  
  1294.  
  1295.  
  1296.  
  1297.  
  1298.  
  1299. //Implement Wired Equivalent Privacy (WEP) protocol in C for encryption and decryption using WEP key.
  1300. #include<bits/stdc++.h>
  1301.  
  1302. using namespace std;
  1303.  
  1304. string rc4(string plaintext, string key){
  1305.     int S[256];
  1306.     for(int i = 0; i < 256; i++)
  1307.         S[i] = i;
  1308.     int j = 0;
  1309.     int keylength = 8;
  1310.     for(int i = 0; i < 256; i++){
  1311.         j = (j + S[i] + key[i%keylength])%256;
  1312.         S[i] += S[j];
  1313.         S[j] = S[i] - S[j];
  1314.         S[i] -= S[j];
  1315.     }
  1316.  
  1317.     int i = 0;
  1318.     j = 0;
  1319.     int length = plaintext.length();
  1320.     string ciphertext = "";
  1321.     for(int k = 0; k < length; k++){
  1322.         i = (i + 1)%256;
  1323.         j = (j + S[i])%256;
  1324.         S[i] += S[j];
  1325.         S[j] = S[i] - S[j];
  1326.         S[i] -= S[j];
  1327.         int rnd = S[(S[i] + S[j])%256];
  1328.         char x = rnd^plaintext[k];
  1329.         ciphertext += x;
  1330.     }
  1331. return ciphertext;
  1332. }
  1333.  
  1334. string crc32fun(string text){
  1335.      unsigned int i, j;
  1336.      unsigned int c;
  1337.      int table[256];
  1338.      for (i = 0; i < 256; i++)
  1339.        {
  1340.      for (c = i << 24, j = 8; j > 0; --j)
  1341.        c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
  1342.      table[i] = c;
  1343.        }
  1344.      unsigned int crc32 = 0xFFFFFFFF;
  1345.      unsigned int index;
  1346.      int length = text.length();
  1347.      for(int k = 0; k < length; k++){
  1348.     index = (crc32^text[k])&(0xFF);
  1349.     crc32 = (crc32<<8)^table[index];
  1350.      }
  1351.      crc32 = crc32^0xFFFFFFFF;
  1352. return to_string(crc32);
  1353. }
  1354.  
  1355.  
  1356. int main(){
  1357.     cout << "Enter plain text:";
  1358.     string plaintext;
  1359.     getline(cin, plaintext);
  1360.     cout << "Enter 24 bit IV:";
  1361.     string iv;
  1362.     cin >> iv;
  1363.     cout << "Enter 40 bit Key:";
  1364.     string key;
  1365.     cin >> key;
  1366.     string key64bit = iv + key;
  1367.  
  1368.  
  1369.     string crc32 = crc32fun(plaintext);
  1370.     string text = plaintext + crc32;
  1371.     string ciphertext = rc4(text, key64bit);
  1372.     string wepoutput = iv + ciphertext;
  1373.  
  1374.     cout << endl << endl << "Encryption" << endl;
  1375.     cout << "ASCII Codes:" << endl;
  1376.     int length = wepoutput.length();
  1377.     for(int i = 0; i < length; i++){
  1378.         if((i%15 == 0) && i != 0) cout << endl;
  1379.         cout << +(u_int8_t)wepoutput[i] << " ";}
  1380.  
  1381.     cout << endl << "Encrypted-data:" << endl;
  1382.     cout << wepoutput << endl << endl;
  1383.  
  1384.  
  1385.     cout << "Decryption" << endl;
  1386.     string ciphermess = wepoutput.substr(3);
  1387.     string message = rc4(ciphermess, key64bit);
  1388.     cout << message << endl;
  1389.     string actual_message = message.substr(0, plaintext.length());
  1390.     string ICV = message.substr(plaintext.length());
  1391.     cout << "ICV:" << ICV << endl;
  1392.     string calculated_ICV = crc32fun(actual_message);
  1393.     cout << "Calculated_ICV:" << calculated_ICV << endl;
  1394.     if(ICV == calculated_ICV){
  1395.          cout << "Value Matched" << endl;
  1396.          cout << "Decrypted data:" << actual_message << endl;
  1397.     }
  1398.     else cout << "Value Unmatched" << endl;
  1399. return 0;
  1400. }
  1401.  
  1402.  
  1403.  
  1404.  
  1405.  
  1406.  
  1407.  
  1408.  
  1409.  
  1410.  
  1411.  
  1412. // Q. Transmission bw 3 nodes
  1413. set ns [new Simulator]
  1414.  
  1415. set nam_obj [open data.nam w]
  1416. $ns namtrace-all $nam_obj
  1417.  
  1418. set trace_obj [open data.tr w]
  1419. $ns trace-all $trace_obj
  1420.  
  1421. proc finish { } {
  1422. global ns nam_obj trace_obj
  1423. $ns flush-trace
  1424. close $nam_obj
  1425. close $trace_obj
  1426. exec nam data.nam &
  1427. exec awk -f data.awk data.tr > output.tr &
  1428. exit 0
  1429. }
  1430.  
  1431. set node0 [$ns node]
  1432. set node1 [$ns node]
  1433. set node2 [$ns node]
  1434. set node3 [$ns node]
  1435.  
  1436. $ns duplex-link $node0 $node1 1Mb 10ms DropTail
  1437. $ns duplex-link $node1 $node2 512Kb 10ms DropTail
  1438. $ns duplex-link $node1 $node3 512Kb 10ms DropTail
  1439.  
  1440. $ns queue-limit $node1 $node2 5
  1441. $ns queue-limit $node1 $node3 5
  1442.  
  1443. $ns duplex-link-op $node0 $node1 orient right
  1444. $ns duplex-link-op $node1 $node2 orient right-down
  1445. $ns duplex-link-op $node1 $node3 orient right-up
  1446.  
  1447. $ns at 0.0 "$node0 label Server"
  1448. $ns at 0.0 "$node1 label Router"
  1449. $ns at 0.0 "$node2 label Client_1"
  1450. $ns at 0.0 "$node3 label Client_2"
  1451.  
  1452. set udp0 [new Agent/UDP]
  1453. $ns attach-agent $node0 $udp0
  1454.  
  1455. set cbr0 [new Application/Traffic/CBR]
  1456. $cbr0 set packetSize_ 500
  1457. $cbr0 set interval_ .005
  1458. $cbr0 attach-agent $udp0
  1459.  
  1460. set udp1 [new Agent/UDP]
  1461. $ns attach-agent $node1 $udp1
  1462.  
  1463. set cbr1 [new Application/Traffic/CBR]
  1464. $cbr1 set packetSize_ 500
  1465. $cbr1 set interval_ .005
  1466. $cbr1 attach-agent $udp1
  1467.  
  1468. set sink0 [new Agent/Null]
  1469. $ns attach-agent $node2 $sink0
  1470. $ns connect $udp0 $sink0
  1471.  
  1472. set sink1 [new Agent/Null]
  1473. $ns attach-agent $node3 $sink1
  1474. $ns connect $udp1 $sink1
  1475.  
  1476. $ns at 0.2 "$cbr0 start"
  1477. $ns at 0.5 "$cbr1 start"
  1478.  
  1479. $ns at 4.5 "$cbr0 stop"
  1480. $ns at 4.7 "$cbr1 stop"
  1481.  
  1482. $ns at 5.0 "finish"
  1483. $ns run
  1484.  
  1485.  
  1486.  
  1487. //dsdv.tcl
  1488. # wrls1.tcl
  1489. # A 3-node example for ad-hoc simulation with DSDV
  1490.  
  1491. # Define options
  1492. set val(chan)           Channel/WirelessChannel    ;# channel type
  1493. set val(prop)           Propagation/TwoRayGround   ;# radio-propagation model
  1494. set val(netif)          Phy/WirelessPhy            ;# network interface type
  1495. set val(mac)            Mac/802_11                 ;# MAC type
  1496. set val(ifq)            Queue/DropTail    ;# interface queue type
  1497. set val(ll)             LL                         ;# link layer type
  1498. set val(ant)            Antenna/OmniAntenna        ;# antenna model
  1499. set val(ifqlen)         50                         ;# max packet in ifq
  1500. set val(nn)             3                          ;# number of mobilenodes
  1501. set val(rp)             DSDV                       ;# routing protocol
  1502. set val(x)              500                ;# X dimension of topography
  1503. set val(y)              400                ;# Y dimension of topography  
  1504. set val(stop)       150            ;# time of simulation end
  1505.  
  1506. set ns        [new Simulator]
  1507. set tracefd       [open simple-dsdv.tr w]
  1508. set windowVsTime2 [open win.tr w]
  1509. set namtrace      [open simwrls.nam w]    
  1510.  
  1511. $ns trace-all $tracefd
  1512. $ns use-newtrace
  1513. $ns namtrace-all-wireless $namtrace $val(x) $val(y)
  1514.  
  1515. # set up topography object
  1516. set topo       [new Topography]
  1517.  
  1518. $topo load_flatgrid $val(x) $val(y)
  1519.  
  1520. create-god $val(nn)
  1521.  
  1522. #
  1523. #  Create nn mobilenodes [$val(nn)] and attach them to the channel.
  1524. #
  1525.  
  1526. # configure the nodes
  1527.         $ns node-config -adhocRouting $val(rp) \
  1528.              -llType $val(ll) \
  1529.              -macType $val(mac) \
  1530.              -ifqType $val(ifq) \
  1531.              -ifqLen $val(ifqlen) \
  1532.              -antType $val(ant) \
  1533.              -propType $val(prop) \
  1534.              -phyType $val(netif) \
  1535.              -channelType $val(chan) \
  1536.              -topoInstance $topo \
  1537.              -agentTrace ON \
  1538.              -routerTrace ON \
  1539.              -macTrace OFF \
  1540.              -movementTrace ON
  1541.              
  1542.     for {set i 0} {$i < $val(nn) } { incr i } {
  1543.         set node_($i) [$ns node]   
  1544.     }
  1545.  
  1546. # Provide initial location of mobilenodes
  1547. $node_(0) set X_ 5.0
  1548. $node_(0) set Y_ 5.0
  1549. $node_(0) set Z_ 0.0
  1550.  
  1551. $node_(1) set X_ 490.0
  1552. $node_(1) set Y_ 285.0
  1553. $node_(1) set Z_ 0.0
  1554.  
  1555. $node_(2) set X_ 150.0
  1556. $node_(2) set Y_ 240.0
  1557. $node_(2) set Z_ 0.0
  1558.  
  1559. # Generation of movements
  1560. $ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0"
  1561. $ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0"
  1562. $ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"
  1563.  
  1564. # Set a TCP connection between node_(0) and node_(1)
  1565. set tcp [new Agent/TCP/Newreno]
  1566. $tcp set class_ 2
  1567. set sink [new Agent/TCPSink]
  1568. $ns attach-agent $node_(0) $tcp
  1569. $ns attach-agent $node_(1) $sink
  1570. $ns connect $tcp $sink
  1571. set ftp [new Application/FTP]
  1572. $ftp attach-agent $tcp
  1573. $ns at 10.0 "$ftp start"
  1574.  
  1575.  
  1576.  
  1577. # Define node initial position in nam
  1578. for {set i 0} {$i < $val(nn)} { incr i } {
  1579. # 30 defines the node size for nam
  1580. $ns initial_node_pos $node_($i) 30
  1581. }
  1582.  
  1583. # Telling nodes when the simulation ends
  1584. for {set i 0} {$i < $val(nn) } { incr i } {
  1585.     $ns at $val(stop) "$node_($i) reset";
  1586. }
  1587.  
  1588. # ending nam and the simulation
  1589. $ns at $val(stop) "$ns nam-end-wireless $val(stop)"
  1590. $ns at $val(stop) "stop"
  1591. $ns at 150.01 "puts \"end simulation\" ; $ns halt"
  1592. proc stop {} {
  1593.     global ns tracefd namtrace
  1594.     $ns flush-trace
  1595.     close $tracefd
  1596.     close $namtrace
  1597.     exec nam simwrls.nam &
  1598. }
  1599.  
  1600. $ns run
  1601.  
  1602.  
  1603.  
  1604.  
  1605.  
  1606.  
  1607.  
  1608.  
  1609.  
  1610.  
  1611.  
  1612.  
  1613.  
  1614.  
  1615.  
  1616.  
  1617.  
  1618.  
  1619.  
  1620.  
  1621.  
  1622.  
  1623.  
  1624.  
  1625.  
  1626.  
  1627.  
  1628.  
  1629.  
  1630.  
  1631.  
  1632.  
  1633.  
  1634.  
  1635.  
  1636. //rsa
  1637. #include<bits/stdc++.h>
  1638. using namespace std;
  1639.  
  1640. #define loop(i, a, b) for(i=a; i<b; i++)
  1641.  
  1642. int solve_ext_gcd(int a,int b, int &x,int &y){
  1643.   if(a==0){
  1644.     x=0; y=1;
  1645.     return b;
  1646.   }
  1647.   int x1,y1;
  1648.   int g = solve_ext_gcd(b%a,a,x1,y1);
  1649.   x=y1-(b/a)*x1;
  1650.   y=x1;
  1651.   return g;
  1652. }
  1653.  
  1654. int solve_modulo_multi_inverse(int a, int m){
  1655.  int x, y;
  1656.  int g = solve_ext_gcd(a, m, x, y);
  1657.  if (g != 1) {
  1658.    cout <<"Inverse doesn't exist"; return -1;
  1659.  }
  1660.  else{
  1661.    int res=(x%m+m)%m;
  1662.    return res;
  1663.  }
  1664. }
  1665.  
  1666. int solve_fast_modulo_exponent(int x, int y, int n){
  1667.   int res = 1; x = x%n;
  1668.   while(y>0){
  1669.     if(y&1) res = (res*x)%n;
  1670.     y >>= 1;
  1671.     x = (x*x)%n;
  1672.   }
  1673.   return res;
  1674. }
  1675.  
  1676. void init_rsa(int *public_key, int *private_key, int *n){
  1677.   int p = 73, q = 227;
  1678.   int phi_n = (p - 1) * (q - 1);
  1679.   *public_key = 25;
  1680.   *n = p*q;
  1681.   *private_key = solve_modulo_multi_inverse(*public_key, phi_n);
  1682. }
  1683.  
  1684. int main(){
  1685.   //int p, q, e, i; cout<<"Enter prime1(p), prime2(q), public key(e) :";
  1686.   //cin>>p>>q>>e;
  1687.   int public_key, private_key, n, i;
  1688.   init_rsa(&public_key, &private_key, &n);
  1689.   string plain_text; cout<<"Enter Plain_text : "; cin>>plain_text;
  1690.   // int phi_n = (p-1)*(q-1);
  1691.   // e = 25
  1692.   // int d = solve_modulo_multi_inverse(e, phi_n);
  1693.  
  1694.   cout<<"Private Key :"<<private_key<<endl;
  1695.   // vector<int> cipher_code(plain_text.length());
  1696.   string str;
  1697.   cout<<"Cipher codes :"; // Encryption
  1698.   loop(i, 0, plain_text.length()-1){
  1699.     str += to_string(solve_fast_modulo_exponent(plain_text[i], public_key, n)) + "*";
  1700.     // cout<<cipher_code[i]<<" ";
  1701.   }
  1702.   str += to_string(solve_fast_modulo_exponent(plain_text[plain_text.length()-1], public_key, n));
  1703.   cout<<str;
  1704.   // cout<<"\nplain_text codes :"; // Decryption
  1705.   // loop(i, 0, plain_text.length()){
  1706.     // cout<<(;
  1707.   // }
  1708. return 0;
  1709. }
  1710.  
  1711.  
  1712.  
  1713.  
  1714.  
  1715.  
  1716.  
  1717.  
  1718.  
  1719.  
  1720.  
  1721.  
  1722.  
  1723.  
  1724. //dsr
  1725. set val(chan)           Channel/WirelessChannel    ;# channel type
  1726. set val(prop)           Propagation/TwoRayGround   ;# radio-propagation model
  1727. set val(netif)          Phy/WirelessPhy            ;# network interface type
  1728. set val(mac)            Mac/802_11                 ;# MAC type
  1729. set val(ifq)            CMUPriQueue                ;# interface queue type
  1730. set val(ll)             LL                         ;# link layer type
  1731. set val(ant)            Antenna/OmniAntenna        ;# antenna model
  1732. set val(ifqlen)         50                         ;# max packet in ifq
  1733. set val(nn)             5                         ;# number of mobilenodes
  1734. set val(rp)             DSR                       ;# routing protocol
  1735. set val(x)              500                ;# X dimension of topography
  1736. set val(y)              400                ;# Y dimension of topography
  1737. set val(stop)       50             ;# time of simulation end
  1738.  
  1739. set ns        [new Simulator]
  1740. set tracefd       [open simple-dsdv.tr w]
  1741. set windowVsTime2 [open win.tr w]
  1742. set namtrace      [open simwrls.nam w]
  1743.  
  1744. $ns trace-all $tracefd
  1745. $ns use-newtrace
  1746. $ns namtrace-all-wireless $namtrace $val(x) $val(y)
  1747.  
  1748. # set up topography object
  1749. set topo       [new Topography]
  1750.  
  1751. $topo load_flatgrid $val(x) $val(y)
  1752.  
  1753. create-god $val(nn)
  1754.  
  1755. #
  1756. #  Create nn mobilenodes [$val(nn)] and attach them to the channel.
  1757. #
  1758.  
  1759. # configure the nodes
  1760.         $ns node-config -adhocRouting $val(rp) \
  1761.              -llType $val(ll) \
  1762.              -macType $val(mac) \
  1763.              -ifqType $val(ifq) \
  1764.              -ifqLen $val(ifqlen) \
  1765.              -antType $val(ant) \
  1766.              -propType $val(prop) \
  1767.              -phyType $val(netif) \
  1768.              -channelType $val(chan) \
  1769.              -topoInstance $topo \
  1770.              -agentTrace ON \
  1771.              -routerTrace ON \
  1772.              -macTrace OFF \
  1773.              -movementTrace ON
  1774.  
  1775.     for {set i 0} {$i < $val(nn) } { incr i } {
  1776.         set node_($i) [$ns node]
  1777.     }
  1778. for {set i 0} {$i < $val(nn)} {incr i} {
  1779.  
  1780.     $node_($i) set X_ [expr rand()*500]
  1781.     $node_($i) set Y_ [expr rand()*400]
  1782.     $node_($i) set Z_ 0
  1783.  
  1784. }
  1785.  
  1786. # Generation of movements
  1787. #$ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0"
  1788. #$ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0"
  1789. #$ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"
  1790.  
  1791. # Set a TCP connection between node_(0) and node_(1)
  1792. set tcp [new Agent/TCP/Newreno]
  1793. $tcp set class_ 2
  1794. set sink [new Agent/TCPSink]
  1795. $ns attach-agent $node_(0) $tcp
  1796. $ns attach-agent $node_(5) $sink
  1797. $ns connect $tcp $sink
  1798. set ftp [new Application/FTP]
  1799. $ftp attach-agent $tcp
  1800. $ns at 2.0 "$ftp start"
  1801.  
  1802. for {set i 0} {$i<$val(nn)} {incr i} {
  1803. $ns initial_node_pos $node_($i) 30
  1804. }
  1805.  
  1806. # Telling nodes when the simulation ends
  1807. for {set i 0} {$i < $val(nn) } { incr i } {
  1808.     $ns at $val(stop) "$node_($i) reset";
  1809. }
  1810.  
  1811. # ending nam and the simulation
  1812. $ns at $val(stop) "$ns nam-end-wireless $val(stop)"
  1813. $ns at $val(stop) "stop"
  1814. $ns at 150.01 "puts \"end simulation\" ; $ns halt"
  1815. proc stop {} {
  1816.     global ns tracefd namtrace
  1817.     $ns flush-trace
  1818.     close $tracefd
  1819.     close $namtrace
  1820.     exec nam simwrls.nam &
  1821. }
  1822.  
  1823. $ns run
  1824.  
  1825.  
  1826.  
  1827.  
  1828.  
  1829.  
  1830.  
  1831.  
  1832.  
  1833.  
  1834.  
  1835.  
  1836.  
  1837.  
  1838.  
  1839.  
  1840.  
  1841.  
  1842.  
  1843. //aodv
  1844. puts "Enter the routing protocol in mobile networking {1 : AODV, 2 : DSR}"
  1845. set nrp [gets stdin]
  1846. if {$nrp == 1} {
  1847.   set val(rp)         AODV;
  1848. } elseif {$nrp == 2} {
  1849.   set val(rp)         DSR;
  1850. }
  1851. puts "Enter the no of nodes in network"
  1852. set no_nodes [gets stdin]
  1853.  
  1854. puts "Enter the node number to send data"
  1855. set src_node [gets stdin]
  1856.  
  1857. # Define options
  1858. set val(chan)           Channel/WirelessChannel    ;# channel type
  1859. set val(prop)           Propagation/TwoRayGround   ;# radio-propagation model
  1860. set val(netif)          Phy/WirelessPhy            ;# network interface type
  1861. set val(mac)            Mac/802_11                 ;# MAC type
  1862. set val(ifq)            Queue/DropTail    ;# interface queue type
  1863. set val(ll)             LL                         ;# link layer type
  1864. set val(ant)            Antenna/OmniAntenna        ;# antenna model
  1865. set val(ifqlen)         50                         ;# max packet in ifq
  1866. set val(nn)             $no_nodes                          ;# number of mobilenodes
  1867. set val(x)              500                ;# X dimension of topography
  1868. set val(y)              400                ;# Y dimension of topography
  1869. set val(stop)       150            ;# time of simulation end
  1870.  
  1871. set ns        [new Simulator]
  1872. set tracefd       [open simple-dsdv.tr w]
  1873. set windowVsTime2 [open win.tr w]
  1874. set namtrace      [open simwrls1.nam w]
  1875.  
  1876. $ns trace-all $tracefd
  1877. $ns use-newtrace
  1878. $ns namtrace-all-wireless $namtrace $val(x) $val(y)
  1879.  
  1880. # set up topography object
  1881. set topo       [new Topography]
  1882.  
  1883. $topo load_flatgrid $val(x) $val(y)
  1884.  
  1885. create-god $val(nn)
  1886.  
  1887. #
  1888. #  Create nn mobilenodes [$val(nn)] and attach them to the channel.
  1889. #
  1890.  
  1891. # configure the nodes
  1892.         $ns node-config -adhocRouting $val(rp) \
  1893.              -llType $val(ll) \
  1894.              -macType $val(mac) \
  1895.              -ifqType $val(ifq) \
  1896.              -ifqLen $val(ifqlen) \
  1897.              -antType $val(ant) \
  1898.              -propType $val(prop) \
  1899.              -phyType $val(netif) \
  1900.              -channelType $val(chan) \
  1901.              -topoInstance $topo \
  1902.              -agentTrace ON \
  1903.              -routerTrace ON \
  1904.              -macTrace OFF \
  1905.              -movementTrace ON
  1906.  
  1907.     for {set i 0} {$i < $val(nn) } { incr i } {
  1908.         set node_($i) [$ns node]
  1909.     }
  1910.  
  1911. # Provide initial location of mobilenodes
  1912.  
  1913. for {set i 0} {$i < $val(nn)} {incr i} {
  1914.   $node_($i) set X_ [expr rand()*500]
  1915.   $node_($i) set Y_ [expr rand()*400]
  1916.   $node_($i) set Z_ 0
  1917. }
  1918.  
  1919. # Generation of movements
  1920. $ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0"
  1921. $ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0"
  1922. $ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"
  1923.  
  1924. # Set a TCP connection between node_(0) and node_(1)
  1925. set tcp [new Agent/TCP/Newreno]
  1926. $tcp set class_ 2
  1927. set sink [new Agent/TCPSink]
  1928. $ns attach-agent $node_($src_node) $tcp
  1929. $ns attach-agent $node_(1) $sink
  1930. $ns connect $tcp $sink
  1931. set ftp [new Application/FTP]
  1932. $ftp attach-agent $tcp
  1933. $ns at 10.0 "$ftp start"
  1934.  
  1935.  
  1936.  
  1937. # Define node initial position in nam
  1938. for {set i 0} {$i < $val(nn)} { incr i } {
  1939. # 30 defines the node size for nam
  1940. $ns initial_node_pos $node_($i) 30
  1941. }
  1942.  
  1943. # Telling nodes when the simulation ends
  1944. for {set i 0} {$i < $val(nn) } { incr i } {
  1945.     $ns at $val(stop) "$node_($i) reset";
  1946. }
  1947.  
  1948. # ending nam and the simulation
  1949. $ns at $val(stop) "$ns nam-end-wireless $val(stop)"
  1950. $ns at $val(stop) "stop"
  1951. $ns at 150.01 "puts \"end simulation\" ; $ns halt"
  1952. proc stop {} {
  1953.     global ns tracefd namtrace
  1954.     $ns flush-trace
  1955.     close $tracefd
  1956.     close $namtrace
  1957.     exec nam simwrls1.nam &
  1958.     exec awk -f aodv.awk simple-dsdv.tr > output.tr &
  1959. }
  1960.  
  1961. $ns run
  1962.  
  1963.  
  1964.  
  1965. //Upload
  1966. //client
  1967. #include <arpa/inet.h>
  1968. #include <netinet/in.h>
  1969. #include <stdio.h>
  1970. #include <stdlib.h>
  1971. #include <string.h>
  1972. #include <sys/socket.h>
  1973. #include <sys/types.h>
  1974. #include <unistd.h>
  1975.  
  1976. #define IP_PROTOCOL 0
  1977. #define IP_ADDRESS "127.0.0.1"
  1978. #define PORT_NO 15050
  1979. #define NET_BUF_SIZE 32
  1980. #define cipherKey 'S'
  1981. #define sendrecvflag 0
  1982.  
  1983.  
  1984. void clearBuf(char* b)
  1985. {
  1986.     int i;
  1987.     for (i = 0; i < NET_BUF_SIZE; i++)
  1988.         b[i] = '\0';
  1989. }
  1990.  
  1991.  
  1992. char Cipher(char ch)
  1993. {
  1994.     return ch ^ cipherKey;
  1995. }
  1996.  
  1997.  
  1998. int recvFile(char* buf, int s)
  1999. {
  2000.     int i;
  2001.     char ch;
  2002.     FILE *newfile;
  2003.     newfile = fopen("received_file.txt","a");
  2004.     for (i = 0; i < s; i++) {
  2005.         ch = buf[i];
  2006.         ch = Cipher(ch);
  2007.         if (ch == EOF){
  2008.         fclose(newfile);
  2009.             return 1;
  2010.         }
  2011.         else{
  2012.         printf("%c", ch);
  2013.             fprintf(newfile,"%c", ch);}
  2014.     }
  2015.     fclose(newfile);
  2016.     return 0;
  2017. }
  2018.  
  2019.  
  2020. int main()
  2021. {
  2022.     int sockfd, nBytes;
  2023.     struct sockaddr_in addr_con;
  2024.     int addrlen = sizeof(addr_con);
  2025.     addr_con.sin_family = AF_INET;
  2026.     addr_con.sin_port = htons(PORT_NO);
  2027.     addr_con.sin_addr.s_addr = inet_addr(IP_ADDRESS);
  2028.     char net_buf[NET_BUF_SIZE];
  2029.     FILE* fp;
  2030.  
  2031.    
  2032.     sockfd = socket(AF_INET, SOCK_DGRAM,
  2033.                     IP_PROTOCOL);
  2034.  
  2035.     if (sockfd < 0)
  2036.         printf("\nfile descriptor not received!!\n");
  2037.     else
  2038.         printf("\nfile descriptor %d received\n", sockfd);
  2039.  
  2040.     while (1) {
  2041.         printf("\nPlease enter file name to upload:\n");
  2042.         scanf("%s", net_buf);
  2043.         sendto(sockfd, net_buf, NET_BUF_SIZE,
  2044.                sendrecvflag, (struct sockaddr*)&addr_con,
  2045.                addrlen);
  2046.  
  2047.         printf("\n---------Data Uploaded---------\n");
  2048.  
  2049.            while(1){
  2050.            
  2051.             clearBuf(net_buf);
  2052.             nBytes = recvfrom(sockfd, net_buf, NET_BUF_SIZE,
  2053.                               sendrecvflag, (struct sockaddr*)&addr_con,
  2054.                               &addrlen);
  2055.  
  2056.            
  2057.             if (recvFile(net_buf, NET_BUF_SIZE)) {
  2058.                 break;
  2059.             }
  2060.         }
  2061.         printf("\n-------------------------------\n");
  2062.     }
  2063.     return 0;
  2064. }
  2065. //server
  2066.  
  2067. #include <arpa/inet.h>
  2068. #include <netinet/in.h>
  2069. #include <stdio.h>
  2070. #include <stdlib.h>
  2071. #include <string.h>
  2072. #include <sys/socket.h>
  2073. #include <sys/types.h>
  2074. #include <unistd.h>
  2075.  
  2076. #define IP_PROTOCOL 0
  2077. #define PORT_NO 15050
  2078. #define NET_BUF_SIZE 32
  2079. #define cipherKey 'S'
  2080. #define sendrecvflag 0
  2081. #define nofile "File Not Found!"
  2082.  
  2083.  
  2084. void clearBuf(char* b)
  2085. {
  2086.     int i;
  2087.     for (i = 0; i < NET_BUF_SIZE; i++)
  2088.         b[i] = '\0';
  2089. }
  2090.  
  2091.  
  2092. char Cipher(char ch)
  2093. {
  2094.     return ch ^ cipherKey;
  2095. }
  2096.  
  2097.  
  2098. int sendFile(FILE* fp, char* buf, int s)
  2099. {
  2100.     int i, len;
  2101.     if (fp == NULL) {
  2102.         strcpy(buf, nofile);
  2103.         len = strlen(nofile);
  2104.         buf[len] = EOF;
  2105.         for (i = 0; i <= len; i++)
  2106.             buf[i] = Cipher(buf[i]);
  2107.         return 1;
  2108.     }
  2109.  
  2110.     char ch, ch2;
  2111.     for (i = 0; i < s; i++) {
  2112.         ch = fgetc(fp);
  2113.         ch2 = Cipher(ch);
  2114.         buf[i] = ch2;
  2115.         if (ch == EOF)
  2116.             return 1;
  2117.     }
  2118.     return 0;
  2119. }
  2120.  
  2121.  
  2122. int main()
  2123. {
  2124.     int sockfd, nBytes;
  2125.     struct sockaddr_in addr_con;
  2126.     int addrlen = sizeof(addr_con);
  2127.     addr_con.sin_family = AF_INET;
  2128.     addr_con.sin_port = htons(PORT_NO);
  2129.     addr_con.sin_addr.s_addr = INADDR_ANY;
  2130.     char net_buf[NET_BUF_SIZE];
  2131.     FILE* fp;
  2132.  
  2133.     sockfd = socket(AF_INET, SOCK_DGRAM, IP_PROTOCOL);
  2134.  
  2135.     if (sockfd < 0)
  2136.         printf("\nfile descriptor not received!!\n");
  2137.     else
  2138.         printf("\nfile descriptor %d received\n", sockfd);
  2139.  
  2140.     if (bind(sockfd, (struct sockaddr*)&addr_con, sizeof(addr_con)) == 0)
  2141.         printf("\nSuccessfully binded!\n");
  2142.     else
  2143.         printf("\nBinding Failed!\n");
  2144.  
  2145.     while (1) {
  2146.         printf("\nWaiting for file name...\n");
  2147.  
  2148.      
  2149.         clearBuf(net_buf);
  2150.  
  2151.         nBytes = recvfrom(sockfd, net_buf,
  2152.                         NET_BUF_SIZE, sendrecvflag,
  2153.                         (struct sockaddr*)&addr_con, &addrlen);
  2154.  
  2155.         fp = fopen(net_buf, "r");
  2156.         printf("\nFile Name Received: %s\n", net_buf);
  2157.         if (fp == NULL)
  2158.             printf("\nFile open failed!\n");
  2159.         else
  2160.             printf("\nFile Successfully opened!\n");
  2161.  
  2162.         while (1) {
  2163.  
  2164.  
  2165.             if (sendFile(fp, net_buf, NET_BUF_SIZE)) {
  2166.                 sendto(sockfd, net_buf, NET_BUF_SIZE,
  2167.                     sendrecvflag,
  2168.                     (struct sockaddr*)&addr_con, addrlen);
  2169.                 break;
  2170.             }
  2171.  
  2172.    
  2173.             sendto(sockfd, net_buf, NET_BUF_SIZE,
  2174.                 sendrecvflag,
  2175.                 (struct sockaddr*)&addr_con, addrlen);
  2176.             clearBuf(net_buf);
  2177.         }
  2178.         if (fp != NULL)
  2179.             fclose(fp);
  2180.     }
  2181.     return 0;
  2182. }
  2183.  
  2184.  
  2185. //download
  2186. //client
  2187. #include <arpa/inet.h>
  2188. #include <netinet/in.h>
  2189. #include <stdio.h>
  2190. #include <stdlib.h>
  2191. #include <string.h>
  2192. #include <sys/socket.h>
  2193. #include <sys/types.h>
  2194. #include <unistd.h>
  2195.  
  2196. #define IP_PROTOCOL 0
  2197. #define IP_ADDRESS "127.0.0.1"
  2198. #define PORT_NO 15050
  2199. #define NET_BUF_SIZE 32
  2200. #define cipherKey 'S'
  2201. #define sendrecvflag 0
  2202.  
  2203.  
  2204. void clearBuf(char* b)
  2205. {
  2206.     int i;
  2207.     for (i = 0; i < NET_BUF_SIZE; i++)
  2208.         b[i] = '\0';
  2209. }
  2210.  
  2211.  
  2212. char Cipher(char ch)
  2213. {
  2214.     return ch ^ cipherKey;
  2215. }
  2216.  
  2217.  
  2218. int recvFile(char* buf, int s)
  2219. {
  2220.     int i;
  2221.     char ch;
  2222.     FILE *newfile;
  2223.     newfile = fopen("received_file.txt","a");
  2224.     for (i = 0; i < s; i++) {
  2225.         ch = buf[i];
  2226.         ch = Cipher(ch);
  2227.         if (ch == EOF){
  2228.         fclose(newfile);
  2229.             return 1;
  2230.         }
  2231.         else{
  2232.         printf("%c", ch);
  2233.             fprintf(newfile,"%c", ch);}
  2234.     }
  2235.     fclose(newfile);
  2236.     return 0;
  2237. }
  2238.  
  2239.  
  2240. int main()
  2241. {
  2242.     int sockfd, nBytes;
  2243.     struct sockaddr_in addr_con;
  2244.     int addrlen = sizeof(addr_con);
  2245.     addr_con.sin_family = AF_INET;
  2246.     addr_con.sin_port = htons(PORT_NO);
  2247.     addr_con.sin_addr.s_addr = inet_addr(IP_ADDRESS);
  2248.     char net_buf[NET_BUF_SIZE];
  2249.     FILE* fp;
  2250.  
  2251.    
  2252.     sockfd = socket(AF_INET, SOCK_DGRAM,
  2253.                     IP_PROTOCOL);
  2254.  
  2255.     if (sockfd < 0)
  2256.         printf("\nfile descriptor not received!!\n");
  2257.     else
  2258.         printf("\nfile descriptor %d received\n", sockfd);
  2259.  
  2260.     while (1) {
  2261.         printf("\nPlease enter file name to download:\n");
  2262.         scanf("%s", net_buf);
  2263.         sendto(sockfd, net_buf, NET_BUF_SIZE,
  2264.                sendrecvflag, (struct sockaddr*)&addr_con,
  2265.                addrlen);
  2266.  
  2267.         printf("\n---------Data Downloaded---------\n");
  2268.  
  2269.            while(1){
  2270.            
  2271.             clearBuf(net_buf);
  2272.             nBytes = recvfrom(sockfd, net_buf, NET_BUF_SIZE,
  2273.                               sendrecvflag, (struct sockaddr*)&addr_con,
  2274.                               &addrlen);
  2275.  
  2276.            
  2277.             if (recvFile(net_buf, NET_BUF_SIZE)) {
  2278.                 break;
  2279.             }
  2280.         }
  2281.         printf("\n-------------------------------\n");
  2282.     }
  2283.     return 0;
  2284. }
  2285.  
  2286. //server
  2287.  
  2288. #include <arpa/inet.h>
  2289. #include <netinet/in.h>
  2290. #include <stdio.h>
  2291. #include <stdlib.h>
  2292. #include <string.h>
  2293. #include <sys/socket.h>
  2294. #include <sys/types.h>
  2295. #include <unistd.h>
  2296.  
  2297. #define IP_PROTOCOL 0
  2298. #define PORT_NO 15050
  2299. #define NET_BUF_SIZE 32
  2300. #define cipherKey 'S'
  2301. #define sendrecvflag 0
  2302. #define nofile "File Not Found!"
  2303.  
  2304.  
  2305. void clearBuf(char* b)
  2306. {
  2307.     int i;
  2308.     for (i = 0; i < NET_BUF_SIZE; i++)
  2309.         b[i] = '\0';
  2310. }
  2311.  
  2312.  
  2313. char Cipher(char ch)
  2314. {
  2315.     return ch ^ cipherKey;
  2316. }
  2317.  
  2318.  
  2319. int sendFile(FILE* fp, char* buf, int s)
  2320. {
  2321.     int i, len;
  2322.     if (fp == NULL) {
  2323.         strcpy(buf, nofile);
  2324.         len = strlen(nofile);
  2325.         buf[len] = EOF;
  2326.         for (i = 0; i <= len; i++)
  2327.             buf[i] = Cipher(buf[i]);
  2328.         return 1;
  2329.     }
  2330.  
  2331.     char ch, ch2;
  2332.     for (i = 0; i < s; i++) {
  2333.         ch = fgetc(fp);
  2334.         ch2 = Cipher(ch);
  2335.         buf[i] = ch2;
  2336.         if (ch == EOF)
  2337.             return 1;
  2338.     }
  2339.     return 0;
  2340. }
  2341.  
  2342.  
  2343. int main()
  2344. {
  2345.     int sockfd, nBytes;
  2346.     struct sockaddr_in addr_con;
  2347.     int addrlen = sizeof(addr_con);
  2348.     addr_con.sin_family = AF_INET;
  2349.     addr_con.sin_port = htons(PORT_NO);
  2350.     addr_con.sin_addr.s_addr = INADDR_ANY;
  2351.     char net_buf[NET_BUF_SIZE];
  2352.     FILE* fp;
  2353.  
  2354.     sockfd = socket(AF_INET, SOCK_DGRAM, IP_PROTOCOL);
  2355.  
  2356.     if (sockfd < 0)
  2357.         printf("\nfile descriptor not received!!\n");
  2358.     else
  2359.         printf("\nfile descriptor %d received\n", sockfd);
  2360.  
  2361.     if (bind(sockfd, (struct sockaddr*)&addr_con, sizeof(addr_con)) == 0)
  2362.         printf("\nSuccessfully binded!\n");
  2363.     else
  2364.         printf("\nBinding Failed!\n");
  2365.  
  2366.     while (1) {
  2367.         printf("\nWaiting for file name...\n");
  2368.  
  2369.      
  2370.         clearBuf(net_buf);
  2371.  
  2372.         nBytes = recvfrom(sockfd, net_buf,
  2373.                         NET_BUF_SIZE, sendrecvflag,
  2374.                         (struct sockaddr*)&addr_con, &addrlen);
  2375.  
  2376.         fp = fopen(net_buf, "r");
  2377.         printf("\nFile Name Received: %s\n", net_buf);
  2378.         if (fp == NULL)
  2379.             printf("\nFile open failed!\n");
  2380.         else
  2381.             printf("\nFile Successfully opened!\n");
  2382.  
  2383.         while (1) {
  2384.  
  2385.  
  2386.             if (sendFile(fp, net_buf, NET_BUF_SIZE)) {
  2387.                 sendto(sockfd, net_buf, NET_BUF_SIZE,
  2388.                     sendrecvflag,
  2389.                     (struct sockaddr*)&addr_con, addrlen);
  2390.                 break;
  2391.             }
  2392.  
  2393.    
  2394.             sendto(sockfd, net_buf, NET_BUF_SIZE,
  2395.                 sendrecvflag,
  2396.                 (struct sockaddr*)&addr_con, addrlen);
  2397.             clearBuf(net_buf);
  2398.         }
  2399.         if (fp != NULL)
  2400.             fclose(fp);
  2401.     }
  2402.     return 0;
  2403. }
  2404.  
  2405. //1.    WAP using client-server programming: Server maintains the database of students (at least 10 students) with the roll number as key. Client sends a roll number to server and server replies with all the corresponding information (at least 5 details). Connection should not terminate till the client wants to.
  2406. //client
  2407. #include <unistd.h>
  2408. #include <stdio.h>
  2409. #include <sys/socket.h>
  2410. #include <stdlib.h>
  2411. #include <netinet/in.h>
  2412. #include <string.h>
  2413. #define PORT 8080
  2414. int main(int argc, char const *argv[])
  2415. {
  2416.     struct sockaddr_in address;
  2417.     int sock = 0, valread;
  2418.     struct sockaddr_in serv_addr;
  2419.    
  2420.    
  2421.     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  2422.     {
  2423.         printf("\n Socket creation error \n");
  2424.         return -1;
  2425.     }
  2426.    
  2427.     memset(&serv_addr, '0', sizeof(serv_addr));
  2428.    
  2429.     serv_addr.sin_family = AF_INET;
  2430.     serv_addr.sin_port = htons(PORT);
  2431.        
  2432.     // Convert IPv4 and IPv6 addresses from text to binary form
  2433.     if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
  2434.     {
  2435.         printf("\nInvalid address/ Address not supported \n");
  2436.         return -1;
  2437.     }
  2438.    
  2439.     if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
  2440.     {
  2441.         printf("\nConnection Failed \n");
  2442.         return -1;
  2443.     }
  2444.     printf("connection created!\n");
  2445.    
  2446.     while(1)
  2447.     {  
  2448.        char buffer[1024] = {0};
  2449.        printf("Enter Roll no to get details:");
  2450.        int roll_no;
  2451.        scanf("%d",&roll_no);
  2452.        int msg = htonl(roll_no);
  2453.        send(sock , &msg, sizeof(msg),0);
  2454.        valread=read( sock, buffer, 1024);
  2455.        printf("%s\n",buffer );
  2456.     }
  2457. }
  2458.    
  2459.  
  2460. //server
  2461. #include <unistd.h>
  2462. #include <stdio.h>
  2463. #include <sys/socket.h>
  2464. #include <stdlib.h>
  2465. #include <netinet/in.h>
  2466. #include <string.h>
  2467. struct db{
  2468. int r;
  2469. char name[30];
  2470. };
  2471. #define N 5
  2472. #define PORT 8080
  2473. int main(int argc, char const *argv[])
  2474. {    
  2475.     struct db record[N];
  2476.     int i=0;
  2477.     char end;
  2478.     printf("Enter records to database\n");
  2479.     for(i=0;i<N;i++){
  2480.        printf("roll no:");
  2481.        scanf("%d",&record[i].r);
  2482.        printf("name:");
  2483.        scanf("%s",record[i].name);
  2484.       }
  2485.     int server_fd, new_socket, valread;
  2486.     struct sockaddr_in address;
  2487.     int opt = 1;
  2488.     int addrlen = sizeof(address);
  2489.     char buffer[1024] = {0};
  2490.        
  2491.     // Creating socket file descriptor
  2492.     if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
  2493.     {
  2494.         perror("socket failed");
  2495.         exit(EXIT_FAILURE);
  2496.     }
  2497.        
  2498.     // Forcefully attaching socket to the port 8080
  2499.     if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
  2500.                                                   &opt, sizeof(opt)))
  2501.     {
  2502.         perror("setsockopt");
  2503.         exit(EXIT_FAILURE);
  2504.     }
  2505.  
  2506.     address.sin_family = AF_INET;
  2507.     address.sin_addr.s_addr = INADDR_ANY;
  2508.     address.sin_port = htons( PORT );
  2509.        
  2510.     // Forcefully attaching socket to the port 8080
  2511.     if (bind(server_fd, (struct sockaddr *)&address,
  2512.                                  sizeof(address))<0)
  2513.     {
  2514.         perror("bind failed");
  2515.         exit(EXIT_FAILURE);
  2516.     }
  2517.     if (listen(server_fd, 3) < 0)
  2518.     {
  2519.         perror("listen");
  2520.         exit(EXIT_FAILURE);
  2521.     }
  2522.    
  2523.     if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0)
  2524.     {
  2525.         perror("accept");
  2526.         exit(EXIT_FAILURE);
  2527.     }
  2528.    
  2529.     while(1)
  2530.     {
  2531.  
  2532.        int msg,status=1;
  2533.        read( new_socket , &msg, sizeof(msg));
  2534.        int roll_no =ntohl(msg);
  2535.        printf("Request for details of roll No %d",roll_no);
  2536.        char rec[30]="No records found";
  2537.        for( i=0;i<N;i++)
  2538.        {
  2539.            if(record[i].r==roll_no)
  2540.            {
  2541.                 send(new_socket , record[i].name , strlen(record[i].name ) , 0 );
  2542.                 printf("Details of roll No %d sent",roll_no);
  2543.                 status=0;
  2544.                 break;
  2545.            }
  2546.        }
  2547.    
  2548.        if(status==1){
  2549.         send(new_socket , rec , strlen(rec) , 0 );
  2550.         printf("Staus sent: No records found");
  2551.        }
  2552.     }
  2553. }
  2554.  
  2555.  
  2556.  
  2557.  
  2558.  
  2559. //crc
  2560. //client
  2561. #include <stdio.h>
  2562. #include <sys/socket.h>
  2563. #include <stdlib.h>
  2564. #include <netinet/in.h>
  2565. #include <string.h>
  2566. #include<math.h>
  2567. #include <arpa/inet.h>
  2568. #include<unistd.h>
  2569. #include<iostream>
  2570. #include<bits/stdc++.h>
  2571. using namespace std;
  2572. #define PORT 8080
  2573. int main(int argc, char const *argv[]){
  2574.     struct sockaddr_in address;
  2575.     int sock = 0, valread;
  2576.     struct sockaddr_in serv_addr;
  2577.     char bufer[2256] = {0};
  2578.     if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
  2579.         printf("\n Socket creation error \n");
  2580.         return -1;
  2581.     }
  2582.     memset(&serv_addr, '0', sizeof(serv_addr));
  2583.     serv_addr.sin_family = AF_INET;
  2584.     serv_addr.sin_port = htons(PORT);
  2585.     if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0){
  2586.         printf("\nInvalid address \n");
  2587.         return -1;
  2588.     }
  2589.     if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
  2590.         printf("\nConnection Failed \n");
  2591.         return -1;
  2592.     }
  2593.  
  2594.     while(1){
  2595.         long long codeword, key;
  2596.         cout<<" Enter the dataword with appended CRC(7, 4) and key (or) 0 to exit:";
  2597.         cin>>codeword>>key;
  2598.         send(sock, &codeword, sizeof(long long), 0);
  2599.         send(sock, &key, sizeof(long long), 0);
  2600.         if(codeword == 0 || key == 0) break;
  2601.         int len = recv(sock, bufer, sizeof(bufer), 0);
  2602.         string info;
  2603.         info.append(bufer, bufer + len);
  2604.         cout<<" Result : "<<info<<endl;
  2605.     }
  2606.     return 0;
  2607. }
  2608.  
  2609. //server
  2610. #include <unistd.h>
  2611. #include <stdio.h>
  2612. #include <sys/socket.h>
  2613. #include <stdlib.h>
  2614. #include <netinet/in.h>
  2615. #include <string.h>
  2616. #include<math.h>
  2617. #include <arpa/inet.h>
  2618. #include<unistd.h>
  2619. #include<iostream>
  2620. #include<bits/stdc++.h>
  2621. using namespace std;
  2622. #define PORT 8080
  2623. string Xor(string a, string b){
  2624.     string res;
  2625.     for(int i=0; i<b.length(); i++){
  2626.         if(a[i] == b[i]) res += "0";
  2627.         else res += "1";
  2628.     }
  2629.     return res;
  2630. }
  2631. string DecToBin(long long dec){
  2632.     string res ;
  2633.     while(dec > 0){
  2634.         res = to_string(dec%2) + res;
  2635.         dec = dec/2;
  2636.     }
  2637.     return res;
  2638. }
  2639. long long BinToDec(string bin){
  2640.     long long dec = 0;
  2641.     for(int i=0;i<bin.length(); i++){
  2642.         dec += pow(2, bin.length() - 1 - i)*(bin[i] - '0');
  2643.     }
  2644.     return dec;
  2645. }
  2646. long long crc(string divident, string divisor){
  2647.     int len = divisor.length();
  2648.     string tmp_res = divident.substr(0, len);
  2649.     while(len < divident.length()){
  2650.         if(tmp_res[0] == '1'){
  2651.             tmp_res = Xor(divisor, tmp_res).substr(1,3) + divident[len];
  2652.         } else{
  2653.             string tmp;
  2654.             for(int i=0; i< len; i++) tmp += '0';
  2655.                 tmp_res = Xor(tmp, tmp_res).substr(1, 3) + divident[len];
  2656.         } len++;
  2657.     } if(tmp_res[0] == '1'){
  2658.         tmp_res = Xor(divisor, tmp_res);
  2659.     } else{
  2660.         string tmp;
  2661.         for(int i=0; i< len; i++) tmp += '0';
  2662.             tmp_res = Xor(tmp, tmp_res).substr(1, 3) + divident[len];
  2663.     } return BinToDec(tmp_res);
  2664. }
  2665. int main(int argc, char const *argv[]){
  2666.     int server_fd, new_socket, valread;
  2667.     struct sockaddr_in address;
  2668.     int opt = 1;
  2669.     int addrlen = sizeof(address);
  2670.     char bufer[2256] = {0};
  2671.     if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0){
  2672.         perror("socket failed");
  2673.         exit(EXIT_FAILURE);
  2674.     }
  2675.     if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR |
  2676.         SO_REUSEPORT,&opt, sizeof(opt))){
  2677.         perror("setsockopt");
  2678.     exit(EXIT_FAILURE);
  2679. }
  2680. address.sin_family = AF_INET;
  2681. address.sin_addr.s_addr = INADDR_ANY;
  2682. address.sin_port = htons( PORT );
  2683. if (bind(server_fd, (struct sockaddr *)&address,sizeof(address))<0){
  2684.     perror("bind failed");
  2685.     exit(EXIT_FAILURE);
  2686. }
  2687. if (listen(server_fd, 3) < 0){
  2688.     perror("listen");
  2689.     exit(EXIT_FAILURE);
  2690. }
  2691. if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
  2692.     (socklen_t*)&addrlen))<0){
  2693.     perror("accept");
  2694. exit(EXIT_FAILURE);
  2695. }
  2696. while(true){
  2697.     long long cwd, ky;
  2698.     valread = read(new_socket, &cwd, sizeof(long long));
  2699.     valread = read(new_socket, &ky, sizeof(long long));
  2700.     if(cwd == 0 || ky == 0) break;
  2701.     string codeword = DecToBin(cwd);
  2702.     string key = DecToBin(ky);
  2703.     string res;
  2704.     crc(codeword, key) == 0 ? res = "good data" : res = "bad data";
  2705.     send(new_socket, res.data(), res.size(), 0);
  2706. }
  2707. return 0;
  2708. }
  2709.  
  2710.  
  2711.  
  2712.  
  2713.  
  2714.  
  2715.  
  2716.  
  2717.  
  2718.  
  2719. //Question answers
  2720. //client
  2721. #include <stdio.h>
  2722. #include <sys/socket.h>
  2723. #include <stdlib.h>
  2724. #include <netinet/in.h>
  2725. #include <string.h>
  2726. #include<math.h>
  2727. #include <arpa/inet.h>
  2728. #include<unistd.h>
  2729. #include<iostream>
  2730. #include<bits/stdc++.h>
  2731. using namespace std;
  2732. #define PORT 8080
  2733. int main(int argc, char const *argv[]){
  2734. struct sockaddr_in address;
  2735. int sock = 0, valread;
  2736. struct sockaddr_in serv_addr;
  2737. char bufer[2256] = {0};
  2738. if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
  2739. printf("\n Socket creation error \n");
  2740. return -1;
  2741. }
  2742. memset(&serv_addr, '0', sizeof(serv_addr));
  2743. serv_addr.sin_family = AF_INET;
  2744. serv_addr.sin_port = htons(PORT);
  2745. if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0){
  2746. printf("\nInvalid address \n");
  2747. return -1;
  2748. } if (
  2749. connect(sock, (
  2750. struct sockaddr *)&serv_addr, sizeof(serv_addr)) <
  2751. 0){
  2752. printf("\nConnection Failed \n");
  2753. return -1;
  2754. }
  2755. while(true){
  2756. string ques;
  2757. cout<<"Enter The Question (all lowercase) (or) 0 to exit ?\n Q : ";
  2758. getline(cin, ques);
  2759. send(sock, ques.data(), ques.size(), 0);
  2760. if(ques == "0") break;
  2761. int len = recv(sock, bufer, sizeof(bufer), 0);
  2762. string res;
  2763. res.append(bufer, bufer + len);
  2764. cout<<" A : "<<res<<endl;
  2765. }
  2766. return 0;
  2767. }
  2768.  
  2769.  
  2770.  
  2771. //server
  2772. #include <unistd.h>
  2773. #include <stdio.h>
  2774. #include <sys/socket.h>
  2775. #include <stdlib.h>
  2776. #include <netinet/in.h>
  2777. #include <string.h>
  2778. #include<math.h>
  2779. #include <arpa/inet.h>
  2780. #include<unistd.h>
  2781. #include<iostream>
  2782. #include<bits/stdc++.h>
  2783. using namespace std;
  2784. #define PORT 8080
  2785. map<string, string> mp;
  2786. int main(int argc, char const *argv[]){
  2787. int server_fd, new_socket, valread;
  2788. struct sockaddr_in address;
  2789. int opt = 1;
  2790. int addrlen = sizeof(address);
  2791. char bufer[2256] = {0};
  2792. mp["who am i"] = "My Boss";
  2793. mp["who is your daddy"] = "You daddy";
  2794. if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0){
  2795. perror("socket failed");
  2796. exit(EXIT_FAILURE);
  2797. } if (
  2798. setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR |
  2799. SO_REUSEPORT,&opt, sizeof(opt))){
  2800. perror("setsockopt");
  2801. exit(EXIT_FAILURE);
  2802. }
  2803. address.sin_family = AF_INET;
  2804. address.sin_addr.s_addr = INADDR_ANY;
  2805. address.sin_port = htons( PORT );
  2806. if (bind(server_fd, (struct sockaddr *)&address,sizeof(address))<0){
  2807. perror("bind failed");
  2808. exit(EXIT_FAILURE);
  2809. }
  2810. if (listen(server_fd, 3) < 0){
  2811. perror("listen");
  2812. exit(EXIT_FAILURE);
  2813. } if ((new_socket =
  2814. accept(server_fd, (
  2815. struct sockaddr *)&address,
  2816. (socklen_t*)&addrlen))<0){
  2817. perror("accept");
  2818. exit(EXIT_FAILURE);
  2819. }
  2820. while(true){
  2821. string ques;
  2822. int len = recv(new_socket, bufer, sizeof(bufer), 0);
  2823. ques.append(bufer, bufer + len);
  2824. cout<<"Q."<<ques<<endl;
  2825. if(ques == "0") break;
  2826. send(new_socket, mp[ques].data(), mp[ques].size(), 0);
  2827. }
  2828. return 0;
  2829. }
  2830.  
  2831.  
  2832.  
  2833.  
  2834.  
  2835. //dynamic aodv
  2836. puts "Enter the routing protocol in mobile networking {1 : AODV, 2 : DSR}"
  2837. set nrp [gets stdin]
  2838. if {$nrp == 1} {
  2839.   set val(rp)         AODV;
  2840. } elseif {$nrp == 2} {
  2841.   set val(rp)         DSR;
  2842. }
  2843. puts "Enter the no of nodes in network"
  2844. set no_nodes [gets stdin]
  2845.  
  2846. puts "Enter the node number to send data"
  2847. set src_node [gets stdin]
  2848.  
  2849. # Define options
  2850. set val(chan)           Channel/WirelessChannel    ;# channel type
  2851. set val(prop)           Propagation/TwoRayGround   ;# radio-propagation model
  2852. set val(netif)          Phy/WirelessPhy            ;# network interface type
  2853. set val(mac)            Mac/802_11                 ;# MAC type
  2854. set val(ifq)            Queue/DropTail    ;# interface queue type
  2855. set val(ll)             LL                         ;# link layer type
  2856. set val(ant)            Antenna/OmniAntenna        ;# antenna model
  2857. set val(ifqlen)         50                         ;# max packet in ifq
  2858. set val(nn)             $no_nodes                          ;# number of mobilenodes
  2859. set val(x)              500                ;# X dimension of topography
  2860. set val(y)              400                ;# Y dimension of topography
  2861. set val(stop)       150            ;# time of simulation end
  2862.  
  2863. set ns        [new Simulator]
  2864. set tracefd       [open simple-dsdv.tr w]
  2865. set windowVsTime2 [open win.tr w]
  2866. set namtrace      [open simwrls1.nam w]
  2867.  
  2868. $ns trace-all $tracefd
  2869. $ns use-newtrace
  2870. $ns namtrace-all-wireless $namtrace $val(x) $val(y)
  2871.  
  2872. # set up topography object
  2873. set topo       [new Topography]
  2874.  
  2875. $topo load_flatgrid $val(x) $val(y)
  2876.  
  2877. create-god $val(nn)
  2878.  
  2879. #
  2880. #  Create nn mobilenodes [$val(nn)] and attach them to the channel.
  2881. #
  2882.  
  2883. # configure the nodes
  2884.         $ns node-config -adhocRouting $val(rp) \
  2885.              -llType $val(ll) \
  2886.              -macType $val(mac) \
  2887.              -ifqType $val(ifq) \
  2888.              -ifqLen $val(ifqlen) \
  2889.              -antType $val(ant) \
  2890.              -propType $val(prop) \
  2891.              -phyType $val(netif) \
  2892.              -channelType $val(chan) \
  2893.              -topoInstance $topo \
  2894.              -agentTrace ON \
  2895.              -routerTrace ON \
  2896.              -macTrace OFF \
  2897.              -movementTrace ON
  2898.  
  2899.     for {set i 0} {$i < $val(nn) } { incr i } {
  2900.         set node_($i) [$ns node]
  2901.     }
  2902.  
  2903. # Provide initial location of mobilenodes
  2904.  
  2905. for {set i 0} {$i < $val(nn)} {incr i} {
  2906.   $node_($i) set X_ [expr rand()*500]
  2907.   $node_($i) set Y_ [expr rand()*400]
  2908.   $node_($i) set Z_ 0
  2909. }
  2910.  
  2911. # Generation of movements
  2912. $ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0"
  2913. $ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0"
  2914. $ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"
  2915.  
  2916. # Set a TCP connection between node_(0) and node_(1)
  2917. set tcp [new Agent/TCP/Newreno]
  2918. $tcp set class_ 2
  2919. set sink [new Agent/TCPSink]
  2920. $ns attach-agent $node_($src_node) $tcp
  2921. $ns attach-agent $node_(1) $sink
  2922. $ns connect $tcp $sink
  2923. set ftp [new Application/FTP]
  2924. $ftp attach-agent $tcp
  2925. $ns at 10.0 "$ftp start"
  2926.  
  2927.  
  2928.  
  2929. # Define node initial position in nam
  2930. for {set i 0} {$i < $val(nn)} { incr i } {
  2931. # 30 defines the node size for nam
  2932. $ns initial_node_pos $node_($i) 30
  2933. }
  2934.  
  2935. # Telling nodes when the simulation ends
  2936. for {set i 0} {$i < $val(nn) } { incr i } {
  2937.     $ns at $val(stop) "$node_($i) reset";
  2938. }
  2939.  
  2940. # ending nam and the simulation
  2941. $ns at $val(stop) "$ns nam-end-wireless $val(stop)"
  2942. $ns at $val(stop) "stop"
  2943. $ns at 150.01 "puts \"end simulation\" ; $ns halt"
  2944. proc stop {} {
  2945.     global ns tracefd namtrace
  2946.     $ns flush-trace
  2947.     close $tracefd
  2948.     close $namtrace
  2949.     exec nam simwrls1.nam &
  2950.     exec awk -f aodv.awk simple-dsdv.tr > output.tr &
  2951. }
  2952.  
  2953. $ns run
Add Comment
Please, Sign In to add comment