Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Q1. WAP in C/C++ so that a client enters an integer and server returns
- // i) Nth Fibonacci number
- // ii) Factorial of n
- Client Side:
- #include <stdio.h> #include <sys/socket.h> #include <stdlib.h> #include <netinet/in.h> #include <string.h> #include<math.h> #defne PORT 8080
- int main(int argc, char const *argv[]){ struct sockaddr_in address;
- int sock = 0, valread;
- struct sockaddr_in serv_addr;
- if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){ printf("\n Socket creation error \n");
- return -1;
- }
- memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET;
- serv_addr.sin_port = htons(PORT);
- if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0){
- printf("\nInvalid address \n");
- return -1; }
- If (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){ printf("\nConnection Failed \n");
- return -1;
- }
- int n;
- printf("\n Enter the Number n :\n"); scanf("%d",&n);
- send(sock , &n , sizeof(int) , 0 );
- int res;
- valread = read( sock , &res, sizeof(int)); printf("Fibonacci : %d\n",res );
- valread = read( sock , &res, sizeof(int)); printf("Factorial : %d\n",res );
- return 0;
- }
- Server Side:-
- #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
- int main(int argc, char const *argv[]){
- int server_fd, new_socket, valread; struct sockaddr_in address;
- int opt = 1;
- int addrlen = sizeof(address);
- If ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0){
- perror("socket failed");
- exit(EXIT_FAILURE); }
- If (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,&opt, sizeof(opt))){
- perror("setsockopt"); exit(EXIT_FAILURE);
- }
- address.sin_family = AF_INET;
- address.sin_addr.s_addr = INADDR_ANY;
- address.sin_port = htons( PORT );
- If (bind(server_fd, (struct sockaddr *)&address,sizeof(address))<0){
- perror("bind failed");
- exit(EXIT_FAILURE); }
- If (listen(server_fd, 3) < 0){ perror("listen");
- exit(EXIT_FAILURE); }
- If ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0){
- perror("accept"); exit(EXIT_FAILURE);
- }
- int n;
- valread = read( new_socket , &n , sizeof(int)); printf("Received : %d\n",n);
- int dp[n];
- dp[0]=1,dp[1]=1; int i; for(i=2;i<n;i++){
- dp[i] = dp[i-1] + dp[i-2]; }
- send(new_socket , &dp[n-1] , sizeof(int) , 0 ); printf("Fibonacci sent\n");
- int fact = 1;
- for(i=1;i<=n;i++){
- fact = fact*i; }
- send(new_socket , &fact , sizeof(int) , 0 );
- return 0; }
- // Q2. WAP so that client sends two numbers and server calculate addition, subtraction, multiplication, division and returns it.
- Client Side:
- #include <stdio.h>
- #include <sys/socket.h>
- #include <stdlib.h>
- #include <netinet/in.h>
- #include <string.h> #include<math.h>
- #defne PORT 8080
- int main(int argc, char const *argv[]){
- struct sockaddr_in address;
- int sock = 0, valread;
- struct sockaddr_in serv_addr;
- If ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
- printf("\n Socket creation error \n");
- return -1; }
- memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET;
- serv_addr.sin_port = htons(PORT);
- if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0){
- printf("\nInvalid address \n");
- return -1; }
- if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){ printf("\nConnection Failed \n");
- return -1;
- }
- int n1,n2, res;
- float sol;
- scanf("%d",&n1);
- scanf("%d",&n2);
- send(sock , &n1 , sizeof(int) , 0 ); send(sock , &n2 , sizeof(int) , 0 ); valread = read( sock , &res, sizeof(int)); printf("Sum : %d\n",res);
- valread = read( sock , &res, sizeof(int)); printf("Product : %d\n",res);
- valread = read(sock, &res, sizeof(int)); printf("Minus : %d\n", res);
- valread = read(sock, &sol, sizeof(float));
- printf("Divide : %f", sol);
- return 0; }
- Server Side:
- #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
- int main(int argc, char const *argv[]){
- int server_fd, new_socket, valread; struct sockaddr_in address;
- int opt = 1;
- int addrlen = sizeof(address);
- if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0){
- perror("socket failed");
- exit(EXIT_FAILURE); }
- if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,&opt, sizeof(opt))){
- perror("setsockopt"); exit(EXIT_FAILURE);
- }
- address.sin_family = AF_INET;
- address.sin_addr.s_addr = INADDR_ANY;
- address.sin_port = htons( PORT );
- if (bind(server_fd, (struct sockaddr *)&address,sizeof(address))<0){
- perror("bind failed");
- exit(EXIT_FAILURE); }
- If (listen(server_fd, 3) < 0){ perror("listen");
- exit(EXIT_FAILURE); }
- if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0){
- perror("accept"); exit(EXIT_FAILURE);
- }
- int n1,n2,sum,prod, minus, divide;
- valread = read( new_socket , &n1 , sizeof(int));
- valread = read( new_socket , &n2 , sizeof(int)); sum = n1 + n2;
- prod = n1 * n2;
- minus = n1 - n2;
- divide = n1 / n2;
- send(new_socket , &sum , sizeof(int) , 0 ); send(new_socket , &prod , sizeof(int) , 0 ); send(new_socket , &minus , sizeof(int) , 0 ); send(new_socket , ÷ , sizeof(float) , 0 ); return 0;
- }
- // 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?.
- // Code:
- // Client Side:
- #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>
- using namespace std;
- #defne PORT 8080
- int main(int argc, char const *argv[]){
- struct sockaddr_in address;
- int sock = 0, valread;
- struct sockaddr_in serv_addr;
- char bufer[2256] = {0};
- if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
- printf("\n Socket creation error \n");
- return -1; }
- memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET;
- serv_addr.sin_port = htons(PORT);
- if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0){
- printf("\nInvalid address \n");
- return -1; }
- if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){ printf("\nConnection Failed \n");
- return -1;
- } while(true){
- string roll_id;
- cout<<"\n Enter The Roll_ID (or) 0 for exit:";
- getline(cin, roll_id);
- send(sock, roll_id.data(), roll_id.size(), 0); if(roll_id == "0") break;
- string name, branch, year;
- int len = recv(sock, bufer, sizeof(bufer), 0); name.append(bufer, bufer + len);
- len = recv(sock, bufer, sizeof(bufer), 0); branch.append(bufer, bufer + len);
- len = recv(sock, bufer, sizeof(bufer), 0); year.append(bufer, bufer + len);
- cout<<" Roll_ID : "<<roll_id;
- cout<<"\n Name : "<<name;
- cout<<"\n Branch : "<< branch;
- cout<<"\n Year : "<< year;
- }
- return 0;
- }
- Server Side:
- #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>
- using namespace std;
- #defne PORT 8080
- struct stu{ string name; string branch; string year;
- };
- map<string, struct stu> mp;
- int main(int argc, char const *argv[]){
- int server_fd, new_socket, valread; struct sockaddr_in address;
- int opt = 1;
- int addrlen = sizeof(address); char bufer[2256] = {0};
- 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";
- if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0){ perror("socket failed");
- exit(EXIT_FAILURE); }
- if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,&opt, sizeof(opt))){
- perror("setsockopt"); exit(EXIT_FAILURE);
- }
- address.sin_family = AF_INET;
- address.sin_addr.s_addr = INADDR_ANY;
- address.sin_port = htons( PORT );
- if (bind(server_fd, (struct sockaddr *)&address,sizeof(address))<0){
- perror("bind failed");
- exit(EXIT_FAILURE); }
- if (listen(server_fd, 3) < 0){ perror("listen");
- exit(EXIT_FAILURE); }
- if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0){
- perror("accept"); exit(EXIT_FAILURE);
- } while(true){
- string roll_id;
- int len = recv(new_socket, bufer, sizeof(bufer), 0); roll_id.append(bufer, bufer + len);
- if(roll_id == "0") break;
- 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);
- }
- return 0;
- }
- // Q4. Server stores set of questions and their answers. Client sends a question to server and server replies with corresponding answers?.
- Client Side:
- #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>
- using namespace std;
- #defne PORT 8080
- int main(int argc, char const *argv[]){
- struct sockaddr_in address;
- int sock = 0, valread;
- struct sockaddr_in serv_addr;
- char bufer[2256] = {0};
- if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
- printf("\n Socket creation error \n");
- return -1; }
- memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET;
- serv_addr.sin_port = htons(PORT);
- if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0){
- printf("\nInvalid address \n");
- return -1; }
- if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){ printf("\nConnection Failed \n");
- return -1;
- } while(true){
- string ques;
- cout<<"Enter The Question (all lowercase) (or) 0 to exit ?\n Q : "; getline(cin, ques);
- send(sock, ques.data(), ques.size(), 0); if(ques == "0") break;
- int len = recv(sock, bufer, sizeof(bufer), 0); string res;
- res.append(bufer, bufer + len);
- cout<<" A : "<<res<<endl;
- }
- return 0;
- }
- Server Side:
- #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>
- using namespace std;
- #defne PORT 8080
- map<string, string> mp;
- int main(int argc, char const *argv[]){
- int server_fd, new_socket, valread; struct sockaddr_in address;
- int opt = 1;
- int addrlen = sizeof(address);
- char bufer[2256] = {0};
- mp["who am i"] = "My Boss";
- mp["who is your daddy"] = "You daddy";
- if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0){
- perror("socket failed");
- exit(EXIT_FAILURE); }
- if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,&opt, sizeof(opt))){
- }
- perror("setsockopt"); exit(EXIT_FAILURE);
- address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons( PORT );
- if (bind(server_fd, (struct sockaddr *)&address,sizeof(address))<0){ perror("bind failed");
- exit(EXIT_FAILURE); }
- if (listen(server_fd, 3) < 0){ perror("listen");
- exit(EXIT_FAILURE); }
- if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0){
- perror("accept"); exit(EXIT_FAILURE);
- } while(true){
- string ques;
- int len = recv(new_socket, bufer, sizeof(bufer), 0); ques.append(bufer, bufer + len);
- if(ques == "0") break;
- send(new_socket, mp[ques].data(), mp[ques].size(), 0);
- }
- return 0;
- }
- Server Side:
- Client Side:
- //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 ?.
- Client Side:
- #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>
- using namespace std;
- #defne PORT 8080
- int main(int argc, char const *argv[]){
- struct sockaddr_in address;
- int sock = 0, valread;
- struct sockaddr_in serv_addr;
- char bufer[2256] = {0};
- if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
- printf("\n Socket creation error \n");
- return -1; }
- memset(&serv_addr, '0', sizeof(serv_addr));
- serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(PORT);
- if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0){ printf("\nInvalid address \n");
- return -1;
- }
- if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){ printf("\nConnection Failed \n");
- return -1;
- }
- while(true){
- long long codeword, key;
- cout<<" Enter the dataword with appended CRC(7, 4) and key (or) 0 to exit: ";
- cin>>codeword>>key;
- send(sock, &codeword, sizeof(long long), 0); send(sock, &key, sizeof(long long), 0); if(codeword == 0 || key == 0) break;
- int len = recv(sock, bufer, sizeof(bufer), 0); string info;
- info.append(bufer, bufer + len);
- cout<<" Result : "<<info<<endl;
- }
- return 0;
- }
- Server Side:
- #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>
- using namespace std;
- #defne PORT 8080
- string Xor(string a, string b){ string res;
- for(int i=0; i<b.length(); i++){
- if(a[i] == b[i]) res += "0";
- else res += "1"; }
- return res; }
- string DecToBin(long long dec){
- string res ; while(dec > 0){
- res = to_string(dec%2) + res;
- dec = dec/2; }
- return res; }
- long long BinToDec(string bin){ long long dec = 0;
- for(int i=0;i<bin.length(); i++){
- dec += pow(2, bin.length() - 1 - i)*(bin[i] - '0'); }
- return dec; }
- long long crc(string divident, string divisor){ int len = divisor.length();
- string tmp_res = divident.substr(0, len); while(len < divident.length()){
- if(tmp_res[0] == '1'){
- tmp_res = Xor(divisor, tmp_res).substr(1,3) + divident[len];
- } else{
- }
- len++; }
- if(tmp_res[0] == '1'){
- tmp_res = Xor(divisor, tmp_res);
- } else{
- }
- return BinToDec(tmp_res); }
- int main(int argc, char const *argv[]){
- int server_fd, new_socket, valread; struct sockaddr_in address;
- string tmp;
- for(int i=0; i< len; i++) tmp += '0';
- tmp_res = Xor(tmp, tmp_res).substr(1, 3) + divident[len];
- string tmp;
- for(int i=0; i< len; i++) tmp += '0';
- tmp_res = Xor(tmp, tmp_res).substr(1, 3) + divident[len];
- int opt = 1;
- int addrlen = sizeof(address); char bufer[2256] = {0};
- if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0){ perror("socket failed");
- exit(EXIT_FAILURE); }
- if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,&opt, sizeof(opt))){
- perror("setsockopt"); exit(EXIT_FAILURE);
- }
- address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons( PORT );
- if (bind(server_fd, (struct sockaddr *)&address,sizeof(address))<0){ perror("bind failed");
- exit(EXIT_FAILURE); }
- if (listen(server_fd, 3) < 0){ perror("listen");
- exit(EXIT_FAILURE); }
- if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0){
- perror("accept"); exit(EXIT_FAILURE);
- }
- while(true){
- long long cwd, ky;
- valread = read(new_socket, &cwd, sizeof(long long)); valread = read(new_socket, &ky, sizeof(long long)); if(cwd == 0 || ky == 0) break;
- string codeword = DecToBin(cwd);
- string key = DecToBin(ky);
- string res;
- crc(codeword, key) == 0 ? res = "good data" : res = "bad data";
- send(new_socket, res.data(), res.size(), 0); }
- return 0;
- }
- Server Side:
- Client Side:
- //Q7: WAP to create a two way chat application where both processes can send and receive messages?
- Client Side:
- #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>
- using namespace std;
- #defne PORT s0s0
- int main(int argc, char const *argv[]){ struct sockaddr_in address;
- int sock = 0, valread;
- struct sockaddr_in serv_addr; char bufer[[256] = {0};
- if((sock = socket(AF_INET , SOCK_ST REAM, 0)) < 0){ printf("\n Socket creation error \n");
- return -1;
- }
- memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET ;
- serv_addr.sin_port = htons(PORT );
- if(inet_pton(AF_INET , "1[27.0.0.1", &serv_addr.sin_addr)<=0){
- printf("\nInvalid address \n");
- return -1; }
- if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){ printf("\nConnection Failed \n");
- return -1;
- }
- cout<<"Welcome to Chat Room :\n";
- while(true){
- string chat, chat_line, reply;
- cout<<"\nClient : ";
- while(std::getline(std::cin, chat_line) && !chat_line.empty() )
- chat += chat_line + "+" ;
- send(sock, chat.data(), chat.size(), 0);
- int len = recv(sock, bufer, sizeof(bufer), 0); reply.append(bufer, bufer + len); cout<<"\nBot : ";
- for(int i=0; i<reply.length(); i++){
- if(reply[i] == '+') cout<<endl;
- else cout<<reply[i]; }
- if(reply == "bye+") break; }
- return 0; }
- Server Side:
- #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>
- using namespace std;
- #defne PORT s0s0
- int main(int argc, char const *argv[]){ int server_fd, new_socket, valread; struct sockaddr_in address;
- int opt = 1;
- int addrlen = sizeof(address); char bufer[[256] = {0};
- if ((server_fd = socket(AF_INET , SOCK_ST REAM, 0)) == 0){ perror("socket failed");
- exit(EXIT _FAILURE); }
- if (setsockopt(server_fd, SOL_SOCKET , SO_REUSEADDR | SO_REUSEPORT ,&opt, sizeof(opt))){
- perror("setsockopt"); exit(EXIT _FAILURE);
- }
- address.sin_family = AF_INET ;
- address.sin_addr.s_addr = INADDR_ANY;
- address.sin_port = htons( PORT );
- if (bind(server_fd, (struct sockaddr *)&address,sizeof(address))<0){
- perror("bind failed");
- exit(EXIT _FAILURE); }
- if (listen(server_fd, 3) < 0){ perror("listen");
- exit(EXIT _FAILURE); }
- if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0){
- perror("accept"); exit(EXIT _FAILURE);
- }
- cout<<"Welcome to Chat Room :\n";
- while(true){
- string chat, reply, reply_line;
- int len = recv(new_socket, bufer, sizeof(bufer), 0); chat.append(bufer, bufer + len);
- cout<<"\nClient : ";
- for(int i=0; i<chat.size(); i++){
- if(chat[i] == '+') cout<<"\n";
- else cout<<chat[i]; }
- cout<<"\nBot : ";
- while(getline(cin, reply_line) && !reply_line.empty())
- reply += reply_line + "+"; send(new_socket, reply.data(), reply.size(), 0); if(chat == "bye+") break;
- }
- return 0; }
- Server Side:
- Client Side:
- //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?
- Client Side:
- #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>
- using namespace std;
- #defne PORT s0s0
- #defne loop(i, a, b) for(i=a; i<b; i++)
- int solve_ext_gcd(int a,int b, int &x,int &y){ if(a==0){
- x=0; y=1;
- return b; }
- int x1,y1;
- int g = solve_ext_gcd(b%a,a,x1,y1); x=y1-(b/a)*x1;
- y=x1;
- return g;
- }
- int solve_modulo_multi_inverse(int a, int m){ int x, y;
- int g = solve_ext_gcd(a, m, x, y);
- if (g != 1) {
- cout <<"Inverse doesn't exist"; return -1; }
- else{
- int res=(x%m+m)%m; return res;
- } }
- int solve_fast_modulo_exponent(int x, int y, int n){ int res = 1; x = x%n;
- while(y>0){
- if(y&1) res = (res*x)%n; y >>= 1;
- x = (x*x)%n;
- }
- return res; }
- void init_rsa(int *public_key, int *private_key, int *n){ int p = 73, q = [2[27;
- int phi_n = (p - 1) * (q - 1);
- *public_key = [25;
- *n = p*q;
- *private_key = solve_modulo_multi_inverse(*public_key, phi_n); }
- int main(int argc, char const *argv[]){ struct sockaddr_in address;
- int sock = 0, valread;
- struct sockaddr_in serv_addr; char bufer[[256] = {0};
- if((sock = socket(AF_INET , SOCK_ST REAM, 0)) < 0){ printf("\n Socket creation error \n");
- return -1;
- }
- memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET ;
- serv_addr.sin_port = htons(PORT );
- if(inet_pton(AF_INET , "1[27.0.0.1", &serv_addr.sin_addr)<=0){
- printf("\nInvalid address \n");
- return -1; }
- if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){ printf("\nConnection Failed \n");
- return -1;
- }
- cout<<"Welcome to Authentication Services : \n"; int public_key, private_key, n, i; init_rsa(&public_key, &private_key, &n);
- while(true){
- string username, password, encrypted_password, status; cout<<"\n username : "; getline(cin, username); cout<<" password : "; getline(cin, password);
- loop(i, 0, password.length()-1){
- encrypted_password += to_string(solve_fast_modulo_exponent(password[i], public_key, n)) + "*";
- }
- encrypted_password += to_string(solve_fast_modulo_exponent(password[password.length()-1], public_key, n));
- send(sock, username.data(), username.size(), 0);
- send(sock, encrypted_password.data(), encrypted_password.size(), 0);
- int len = recv(sock, bufer, sizeof(bufer), 0); status.append(bufer, bufer + len); cout<<" status : "<<status<<endl;
- }
- return 0;
- }
- Server Side:
- #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>
- using namespace std;
- #defne PORT s0s0
- #defne loop(i, a, b) for(i=a; i<b; i++)
- int solve_ext_gcd(int a,int b, int &x,int &y){ // Extended GCD if(a==0){
- x=0; y=1;
- return b; }
- int x1,y1;
- int g = solve_ext_gcd(b%a,a,x1,y1); x=y1-(b/a)*x1;
- y=x1;
- return g;
- }
- int solve_modulo_multi_inverse(int a, int m){ int x, y;
- int g = solve_ext_gcd(a, m, x, y);
- if (g != 1) {
- cout <<"Inverse doesn't exist"; return -1; }
- else{
- int res=(x%m+m)%m; return res;
- } }
- int solve_fast_modulo_exponent(int x, int y, int n){ int res = 1; x = x%n;
- while(y>0){
- if(y&1) res = (res*x)%n;
- y >>= 1;
- x = (x*x)%n; }
- return res; }
- void init_rsa(int *public_key, int *private_key, int *n){ int p = 73, q = [2[27;
- int phi_n = (p - 1) * (q - 1);
- *public_key = [25;
- *n = p*q;
- *private_key = solve_modulo_multi_inverse(*public_key, phi_n); }
- int main(int argc, char const *argv[]){ int server_fd, new_socket, valread; struct sockaddr_in address;
- int opt = 1;
- int addrlen = sizeof(address);
- char bufer[[256] = {0};
- map<string, string> mp; mp["alokpatra199s@gmail.com"] = "patra_style";
- if ((server_fd = socket(AF_INET , SOCK_ST REAM, 0)) == 0){ perror("socket failed");
- exit(EXIT _FAILURE); }
- if (setsockopt(server_fd, SOL_SOCKET , SO_REUSEADDR | SO_REUSEPORT ,&opt, sizeof(opt))){
- perror("setsockopt"); exit(EXIT _FAILURE);
- }
- address.sin_family = AF_INET ;
- address.sin_addr.s_addr = INADDR_ANY;
- address.sin_port = htons( PORT );
- if (bind(server_fd, (struct sockaddr *)&address,sizeof(address))<0){
- perror("bind failed");
- exit(EXIT _FAILURE); }
- if (listen(server_fd, 3) < 0){ perror("listen");
- exit(EXIT _FAILURE); }
- if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0){
- perror("accept"); exit(EXIT _FAILURE);
- }
- int public_key, private_key, n, i; init_rsa(&public_key, &private_key, &n);
- while(true){
- string username, decrypted_password, status; vector<int> password;
- int len = recv(new_socket, bufer, sizeof(bufer), 0); username.append(bufer, bufer + len);
- len = recv(new_socket, bufer, sizeof(bufer), 0); char delim[] = "*";
- char *token = strtok(bufer, delim);
- while(token) {
- password.push_back(stoi(token));
- token = strtok(NULL, delim); }
- if(mp.fnd(username) == mp.end()){ status = "Incorrect Username";
- } else{
- loop(i, 0, password.size()){
- decrypted_password += (char)solve_fast_modulo_exponent(password[i], private_key, n);
- }
- if(decrypted_password == mp[username]){
- status = "Success"; }
- else{
- status = "Incorrect Password";
- } }
- send(new_socket, status.data(), status.size(), 0);
- }
- return 0; }
- Client Side:
- //Q9: Hospital Management System.
- // Step by Step functionalities needs to be implemented:
- // I. Client will enter patient details along with department name he/she wants to
- // visit to the server.
- // II. Server will hold all doctors’ name for each department in a database.
- // III. Whenever any patient comes from client, server will check doctor’s availability and assign one doctor to the patient for the concerned department.
- // IV. If no doctor will be available in that time no doctor would be assigned to that patient.
- // V. After every 1 minute, each assigned doctor will be free.
- // VI. Server will maintain one queue for the waited patient, and assign free doctor
- // accordingly
- //VII. Connection will be closed when client wants to.
- Client Side:
- #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>
- using namespace std;
- #defne PORT s0s0
- #defne loop8i, a, b ior8iaa; i<b; i++
- int main8int argc, char const *argv[] { struct sockaddr_in address;
- int sock a 0, valread;
- struct sockaddr_in serv_addr; char bufer[[256] a {0};
- ii88sock a socket8AF_INET , SOCK_ST REAM, 0 printi8"\n Socket creation error \n" ; return -1;
- < 0 {
- }
- memset8&serv_addr, '0', sizeoi8serv_addr ; serv_addr.sin_iamily a AF_INET ;
- serv_addr.sin_port a htons8PORT ;
- ii8inet_pton8AF_INET , "1[27.0.0.1", &serv_addr.sin_addr <a0 {
- printi8"\nInvalid address \n" ;
- return -1; }
- ii 8connect8sock, 8struct sockaddr * &serv_addr, sizeoi8serv_addr printi8"\nConnection Failed \n" ;
- return -1;
- < 0 {
- }
- cout<<"~ Welcome to ISM Health Center ~\n";
- while8true {
- string patient_name, department_to_visit, patient_sex, patient_age,
- patient_status;
- cout<<"\nPlease enter the iollowing patient details 8or to exit:\n"; cout<<"\nName: "; getline8cin, patient_name ;
- cout<<"\nAge: "; getline8cin, patient_age ;
- cout<<"\nSex:"; getline8cin, patient_sex ;
- cout<<"\nDepartment to visit: "; getline8cin, department_to_visit ;
- string send_data a patient_name + "@" + patient_age +"@" + patient_sex + "@" + department_to_visit;
- 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;
- } return 0; }
- Server Side:
- #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>
- using namespace std;
- #defne PORT s0s0
- #defne loop8i, a, b ior8iaa; i<b; i++
- typedei struct Doctor{ string name;
- bool avialable;
- }doc;
- typedei struct Department{ string name;
- doc doctor[[2];
- }dept;
- typedei struct Patient{ string name;
- string age;
- string sex;
- string department_to_visit;
- }pat;
- dept departments[[2];
- void init_db8 {
- 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;
- 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;
- }
- int main8int argc, char const *argv[] { queue<pat> waiting_queue;
- int server_id, new_socket, valread; struct sockaddr_in address;
- int opt a 1;
- int addrlen a sizeoi8address ;
- char bufer[51[2] a {0};
- ii 88server_id a socket8AF_INET , SOCK_ST REAM, 0 perror8"socket iailed" ;
- exit8EXIT _FAILURE ; }
- aa 0 {
- ii 8setsockopt8server_id, SOL_SOCKET , SO_REUSEADDR | SO_REUSEPORT ,&opt, sizeoi8opt {
- perror8"setsockopt" ; exit8EXIT _FAILURE ;
- }
- address.sin_iamily a AF_INET ;
- address.sin_addr.s_addr a INADDR_ANY;
- address.sin_port a htons8 PORT ;
- ii 8bind8server_id, 8struct sockaddr * &address,sizeoi8address
- perror8"bind iailed" ;
- exit8EXIT _FAILURE ; }
- ii 8listen8server_id, 3 < 0 { perror8"listen" ;
- exit8EXIT _FAILURE ; }
- <0 {
- ii 88new_socket a accept8server_id, 8struct sockaddr * &address, 8socklen_t* &addrlen <0 {
- perror8"accept" ; exit8EXIT _FAILURE ;
- } init_db8 ;
- int i, j; while8true {
- vector<string> p_data;
- int tag a 1;
- string status;
- int len a recv8new_socket, bufer, sizeoi8bufer , 0 ; char delim[] a "@";
- char *token a strtok8bufer, delim ; while8token {
- p_data.push_back8token ;
- token a strtok8NULL, delim ; }
- len a p_data.size8 ;
- bool fag a ialse;
- ior8int ka0; k<len; k++ {
- ii8p_data[k] aa "0" { fag a true;
- break;
- } }
- ii8fag break;
- ior8ia0; i<51[2; i++
- pat patient;
- patient.name a p_data[0]; patient.age a p_data[1];
- patient.sex a p_data[[2]; patient.department_to_visit a p_data[3]; p_data.clear8 ;
- waiting_queue.push8patient ;
- bufer[i] a '\0';
- ii8waiting_queue.size8 > 3 { init_db8 ;
- }
- pat patient_new a waiting_queue.iront8 ; waiting_queue.pop8 ;
- ior8ia0; i<[2; i++ {
- ii8departments[i].name aa patient_new.department_to_visit
- }
- ii8iaa[2 {
- status a "T his Department doesn't exits"; }
- else{
- ior8ja0; j<[2; j++ {
- ii8departments[i].doctor[j].avialable { departments[i].doctor[j].avialable a ialse; break;
- } }
- ii8jaa[2 {
- status a "Doctors are busy please wait ior some time"; tag a 0;
- } else{
- break;
- status a departments[i].doctor[j].name + " is assinged to " + patient_new.name;
- } }
- ii8tagaa0 { waiting_queue.push8patient_new ;
- }
- send8new_socket, status.data8 , status.size8 , 0 ; }
- return 0; }
- Output:
- // Q10: Implement a data transfer simulation between 3 nodes using NS2?
- set ns [new Simulator] # Event scheduler object creation
- set nf [open prog1.nam w] # creating nam objects $ns namtrace-all $nf
- set nd [open prog1.tr w] # creating trace objects $ns trace-all $nd
- proc fnish { } { global ns nf nd $ns flush-trace close $nf
- # fnish procedure
- close $nd
- exec nam prog1.nam &
- exec awk -f data.awk prog1.tr > output.tr & exit 0
- }
- $ns color 1 darkmagenta $ns color 2 yellow
- $ns color 3 blue
- $ns color 4 green
- $ns color 5 black
- set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node]
- set server 0 set router 1 set client1 2 set client2 3
- # Setting color IDs
- # Creating Network
- # Setting Nodes
- $ns duplex-link $n0 $n1 1Mb 10ms DropTail $ns duplex-link $n1 $n2 512Kb 10ms DropTail $ns duplex-link $n1 $n3 512Kb 10ms DropTail
- $ns queue-limit $n1 $n2 5 $ns queue-limit $n1 $n3 5
- $ns duplex-link-op $n0 $n1 orient right
- $ns duplex-link-op $n1 $n2 orient right-down $ns duplex-link-op $n1 $n3 orient right-up
- # Creating Duplex Link
- $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"
- # Labelling
- $ns at 0.0 "$n0 color green" $ns at 0.0 "$n2 color blue" $ns at 0.0 "$n3 color blue"
- $n0 shape hexagon $n2 shape hexagon $n3 shape hexagon
- set udp0 [new Agent/UDP] $ns attach-agent $n0 $udp0
- # Data Transfer between Nodes
- set cbr0 [new Application/Trafc/C/R] $cbr0 set packetSize_ 500
- $cbr0 set interval_ 0.005
- $cbr0 attach-agent $udp0
- set udp1 [new Agent/UDP] $ns attach-agent $n1 $udp1
- set cbr1 [new Application/Trafc/C/R] $cbr1 set packetSize_ 500
- $cbr1 set interval_ 0.005
- $cbr1 attach-agent $udp1
- set sink [new Agent/Null] $ns attach-agent $n2 $sink $ns connect $udp0 $sink
- set sink1 [new Agent/Null] $ns attach-agent $n3 $sink1 $ns connect $udp1 $sink1
- $udp0 set fd_ 4 $udp1 set fd_ 1
- $ns at 0.2 "$cbr0 start" $ns at 0.5 "$cbr1 start"
- $ns at 4.5 "$cbr0 stop" $ns at 4.7 "$cbr1 stop"
- #Calling fnish procedure $ns at 5.0 "fnish"
- $ns run
- # Setting flow color
- # data packet generation starting time
- # data packet generation ending time
- Code(.awk):
- BEGIN {
- dcount = 0;
- rcount = 0; }
- {
- event = $1;
- if(event == "d"){ dcount++;
- }
- if(event == "r"){
- rcount++; }
- }
- END {
- printf("packets dropped : %d\n ",dcount); printf("packets recieved : %d\n ",rcount);
- }
- Output:
- // Q11: Implement DSDV routing protocol using NS2?
- Code(.tcl):
- set val(chan) Channel/WirelessChannel set val(prop) Propagation/TwoRayGround set val(netif) Phy/WirelessPhy
- set val(mac) Mac/802_11
- set val(ifq) CMUPriQueue
- set val(ll) LL
- set val(ant) Antenna/OmniAntenna set val(ifqlen) 50
- ;# channel type
- ;# radio-propagation model
- ;# network interface type
- ;# MAC type
- ;# interface queue type ;# link layer type
- ;# antenna model
- ;# max packet in ifq
- ;# number of mobilenodes
- ;# routing protocol
- ;# X dimension of topography ;# Y dimension of topography ;# time of simulation end
- set val(nn) 3
- set val(rp) DSDV set val(x) 500 set val(y) 400 set val(stop) 150
- set ns
- [new Simulator]
- # Event scheduler object creation
- set tracefd
- set windowVsTime2 [open win.tr w] set namtrace [open dsr.nam w]
- [open dsr.tr w]
- $ns trace-all $tracefd
- $ns namtrace-all-wireless $namtrace $val(x) $val(y)
- set topo [new Topography] # set up topography object $topo load_flatgrid $val(x) $val(y)
- create-god $val(nn)
- $ns node-confg -adhocRouting $val(rp) \ -llType $val(ll) \
- -macType $val(mac) \ -ifqType $val(ifq) \
- -ifqLen $val(ifqlen) \ -antType $val(ant) \ -propType $val(prop) \ -phyType $val(netif) \ -channelType $val(chan) \ -topoInstance $topo \ -agentTrace ON \
- -routerTrace ON \ -macTrace OFF \ -movementTrace ON
- for {set i 0} {$i < $val(nn) } { incr i } { set node_($i) [$ns node]
- }
- # confgure the nodes
- #Creating trace fle and nam fle
- $node_(0) set X_ 5.0 # Provide initial location of mobilenodes $node_(0) set Y_ 5.0
- $node_(0) set Z_ 0.0
- $node_(1) set X_ 490.0 $node_(1) set Y_ 285.0 $node_(1) set Z_ 0.0
- $node_(2) set X_ 150.0 $node_(2) set Y_ 240.0 $node_(2) set Z_ 0.0
- $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"
- $ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"
- # Set a TCP connection between node_(0) and node_(1)
- set tcp [new Agent/TCP/Newreno] $tcp set class_ 2
- set sink [new Agent/TCPSink]
- $ns attach-agent $node_(0) $tcp $ns attach-agent $node_(1) $sink $ns connect $tcp $sink
- set ftp [new Application/FTP] $ftp attach-agent $tcp
- $ns at 10.0 "$ftp start"
- proc plotWindow {tcpSource fle} { global ns
- set time 0.01
- set now [$ns now]
- # Printing the window size
- set cwnd [$tcpSource set cwnd_]
- puts $fle "$now $cwnd"
- $ns at [expr $now+$time] "plotWindow $tcpSource $fle" } $ns at 10.1 "plotWindow $tcp $windowVsTime2"
- for {set i 0} {$i < $val(nn)} { incr i } { # Defne node initial position in nam # 30 defnes the node size for nam
- $ns initial_node_pos $node_($i) 30
- }
- # Telling nodes when the simulation ends
- for {set i 0} {$i < $val(nn) } { incr i } { $ns at $val(stop) "$node_($i) reset";
- }
- # ending nam and the simulation
- $ns at $val(stop) "$ns nam-end-wireless $val(stop)"
- $ns at $val(stop) "stop"
- $ns at 150.01 "puts \"end simulation\" ; $ns halt" proc stop {} {
- global ns tracefd namtrace $ns flush-trace
- close $tracefd
- close $namtrace
- exec nam dsr.nam & exit 0
- }
- $ns run
- Output:
- // Q12:Implement AODV routing protocol using tcl-script in NS2?
- Code(.tcl):
- set val(chan)
- set val(prop)
- set val(netif)
- set val(mac)
- set val(ifq)
- set val(ll)
- set val(ant)
- set val(ifqlen) 50;
- Channel/WirelessChannel; # channel
- Propagation/TwoRayGround;# radio-propagation
- Phy/WirelessPhy;
- Mac/802_11;
- Queue/DropTail;
- LL;
- Antenna/OmniAntenna;
- # network interface
- # MAC type
- # interface queue
- # link layer
- # antenna model
- # max packet in ifq
- # #mobilenodes
- # routing protocol
- # X of topography
- # Y of topography
- # time of simulation
- $ns trace-all $tracefd
- $ns use-newtrace
- $ns namtrace-all-wireless $namtrace $val(x) $val(y)
- set topo [new Topography]
- $topo load_flatgrid $val(x) $val(y)
- create-god $val(nn)
- #
- # Create nn mobilenodes [$val(nn)] and attach them to the
- channel.
- #
- # configure the nodes
- $ns node-config -adhocRouting $val(rp) \
- -llType $val(ll) \
- -macType $val(mac) \
- -ifqType $val(ifq) \
- -ifqLen $val(ifqlen) \
- -antType $val(ant) \
- -propType $val(prop) \
- -phyType $val(netif) \
- -channelType $val(chan) \
- -topoInstance $topo \
- -agentTrace ON \
- -routerTrace ON \
- -macTrace OFF \
- -movementTrace ON
- set val(nn)
- set val(rp)
- set val(x)
- set val(y)
- set val(stop)
- 4;
- AODV;
- 500;
- 400;
- 150;
- [new Simulator]
- [open simple-dsdv.tr w]
- set ns
- set tracefd
- set windowVsTime2 [open win.tr w]
- set namtrace [open simwrls1.nam w]
- for {set i 0} {$i < $val(nn) } { incr i } {
- set node_($i) [$ns node]
- }
- # Provide initial location of mobile nodes
- for {set i 0} {$i < $val(nn)} {incr i} {
- $node_($i) set X_ [expr rand()*500]
- $node_($i) set Y_ [expr rand()*400]
- $node_($i) set Z_ 0
- }
- # Generation of movements
- $ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0"
- $ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0"
- $ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"
- # Set a TCP connection between node_(0) and node_(1)
- set tcp [new Agent/TCP/Newreno]
- $tcp set class_ 2
- set sink [new Agent/TCPSink]
- $ns attach-agent $node_(0) $tcp
- $ns attach-agent $node_(1) $sink
- $ns connect $tcp $sink
- set ftp [new Application/FTP]
- $ftp attach-agent $tcp
- $ns at 10.0 "$ftp start"
- # Define node initial position in nam
- for {set i 0} {$i < $val(nn)} { incr i } {
- # 30 defines the node size for nam
- $ns initial_node_pos $node_($i) 30
- }
- # Telling nodes when the simulation ends
- for {set i 0} {$i < $val(nn) } { incr i } {
- $ns at $val(stop) "$node_($i) reset";
- }
- # ending nam and the simulation
- $ns at $val(stop) "$ns nam-end-wireless $val(stop)"
- $ns at $val(stop) "stop"
- $ns at 150.01 "puts \"end simulation\" ; $ns halt"
- proc stop {} {
- global ns tracefd namtrace
- $ns flush-trace
- close $tracefd
- close $namtrace
- exec nam simwrls1.nam &
- }
- $ns run
- Output:
- // 1. WAP in C to find cluster size and reuse factor of a cellular system.
- // 2. WAP in C to calculate the channel capacity of a cellular system.
- // WAP to check the validity for a given cluster size of a cellular system
- #include<bits/stdc++.h>
- #include<math.h>
- using namespace std;
- #define ll long long
- #define loop(i, a, b) for(i=a; i<b; i++)
- #define rloop(i, a, b) for(i=a; i>b; i++)
- void channel_capacity(){
- ll i, j, itr, N, no_of_channels_per_cell, no_clusters;
- while(true){
- cout<<"\n Enter(tab separated) i, j, no_of_channels_per_cell, no_clusters: ";
- cin>>i>>j>>no_of_channels_per_cell>>no_clusters;
- N = i * i + i * j + j * j ;
- cout<<"\n Channel Capacity: "<<no_of_channels_per_cell * N * no_clusters<<endl;
- }
- }
- void cluster_size_and_freq_reuse_factor(){
- while(true){
- double n, S_I, io, temp;
- ll N;
- cout<<"\n Enter path loss exponent(n), signal to interference ratio(S/I), #co_channels cell interfer: ";
- cin>>n>>S_I>>io;
- temp = S_I * io ;
- N = pow(temp, ((double)2 / n)) / 3 ;
- cout<<"\n Cluster Size: "<<N<<endl;
- cout<<"\n frequency reuse factor: "<<"1/"<<N<<endl;
- }
- }
- void validate_cluster_size(){
- while(true){
- ll N, i, j;
- cout<<"\n Enter cluster size(N): ";
- cin>>N;
- loop(i, 1, N){
- loop(j, 1, N){
- if(N == (i * i + i * j + j * j)){
- cout<<"\n Valid Cluster\n";
- return ;
- }
- }
- }
- cout<<"\n Invalid Cluster Size\n";
- }
- }
- void cdma(){
- ll d[4], i, j, c;
- cout<<"\n Enter 4 data points: ";
- cin>>d[0]>>d[1]>>d[2]>>d[3];
- ll n = 4, w[][4]={ {1,1,1,1},
- {1,-1,1,-1},
- {1,1,-1,-1},
- {1,-1,-1,1} },f[n];
- cout<<"\n Chips for each cahnnel are: "<<endl;
- loop(j, 1, n + 1){
- cout<<" C"<<j<<": [";
- loop(i, 0, n - 1)
- cout<<w[j-1][i]<<", ";
- cout<<w[j-1][n-1]<<"]"<<endl;
- }
- loop(i, 0, n){
- c = 0;
- loop(j, 0, n){
- c += (w[j][i] * d[j]);
- }
- f[i] = c;
- }
- cout<<"\n Data Sent: [";
- loop(i, 0, n - 1) cout<<f[i]<<", ";
- cout<<f[n-1]<<"]"<<endl;
- loop(i, 0, n){
- c = 0;
- loop(j, 0, n)
- c += (w[i][j] * f[j]) ;
- cout<<"\n Bit Sent By "<<i<<" = "<<c / n;
- }
- cout<<endl;
- }
- int main(int argc, char const *argv[]) {
- // channel_capacity();
- // cluster_size_and_freq_reuse_factor();
- //validate_cluster_size();
- // cdma();
- return 0;
- }
- //Implement Wired Equivalent Privacy (WEP) protocol in C for encryption and decryption using WEP key.
- #include<bits/stdc++.h>
- using namespace std;
- string rc4(string plaintext, string key){
- int S[256];
- for(int i = 0; i < 256; i++)
- S[i] = i;
- int j = 0;
- int keylength = 8;
- for(int i = 0; i < 256; i++){
- j = (j + S[i] + key[i%keylength])%256;
- S[i] += S[j];
- S[j] = S[i] - S[j];
- S[i] -= S[j];
- }
- int i = 0;
- j = 0;
- int length = plaintext.length();
- string ciphertext = "";
- for(int k = 0; k < length; k++){
- i = (i + 1)%256;
- j = (j + S[i])%256;
- S[i] += S[j];
- S[j] = S[i] - S[j];
- S[i] -= S[j];
- int rnd = S[(S[i] + S[j])%256];
- char x = rnd^plaintext[k];
- ciphertext += x;
- }
- return ciphertext;
- }
- string crc32fun(string text){
- unsigned int i, j;
- unsigned int c;
- int table[256];
- for (i = 0; i < 256; i++)
- {
- for (c = i << 24, j = 8; j > 0; --j)
- c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
- table[i] = c;
- }
- unsigned int crc32 = 0xFFFFFFFF;
- unsigned int index;
- int length = text.length();
- for(int k = 0; k < length; k++){
- index = (crc32^text[k])&(0xFF);
- crc32 = (crc32<<8)^table[index];
- }
- crc32 = crc32^0xFFFFFFFF;
- return to_string(crc32);
- }
- int main(){
- cout << "Enter plain text:";
- string plaintext;
- getline(cin, plaintext);
- cout << "Enter 24 bit IV:";
- string iv;
- cin >> iv;
- cout << "Enter 40 bit Key:";
- string key;
- cin >> key;
- string key64bit = iv + key;
- string crc32 = crc32fun(plaintext);
- string text = plaintext + crc32;
- string ciphertext = rc4(text, key64bit);
- string wepoutput = iv + ciphertext;
- cout << endl << endl << "Encryption" << endl;
- cout << "ASCII Codes:" << endl;
- int length = wepoutput.length();
- for(int i = 0; i < length; i++){
- if((i%15 == 0) && i != 0) cout << endl;
- cout << +(u_int8_t)wepoutput[i] << " ";}
- cout << endl << "Encrypted-data:" << endl;
- cout << wepoutput << endl << endl;
- cout << "Decryption" << endl;
- string ciphermess = wepoutput.substr(3);
- string message = rc4(ciphermess, key64bit);
- cout << message << endl;
- string actual_message = message.substr(0, plaintext.length());
- string ICV = message.substr(plaintext.length());
- cout << "ICV:" << ICV << endl;
- string calculated_ICV = crc32fun(actual_message);
- cout << "Calculated_ICV:" << calculated_ICV << endl;
- if(ICV == calculated_ICV){
- cout << "Value Matched" << endl;
- cout << "Decrypted data:" << actual_message << endl;
- }
- else cout << "Value Unmatched" << endl;
- return 0;
- }
- // Q. Transmission bw 3 nodes
- set ns [new Simulator]
- set nam_obj [open data.nam w]
- $ns namtrace-all $nam_obj
- set trace_obj [open data.tr w]
- $ns trace-all $trace_obj
- proc finish { } {
- global ns nam_obj trace_obj
- $ns flush-trace
- close $nam_obj
- close $trace_obj
- exec nam data.nam &
- exec awk -f data.awk data.tr > output.tr &
- exit 0
- }
- set node0 [$ns node]
- set node1 [$ns node]
- set node2 [$ns node]
- set node3 [$ns node]
- $ns duplex-link $node0 $node1 1Mb 10ms DropTail
- $ns duplex-link $node1 $node2 512Kb 10ms DropTail
- $ns duplex-link $node1 $node3 512Kb 10ms DropTail
- $ns queue-limit $node1 $node2 5
- $ns queue-limit $node1 $node3 5
- $ns duplex-link-op $node0 $node1 orient right
- $ns duplex-link-op $node1 $node2 orient right-down
- $ns duplex-link-op $node1 $node3 orient right-up
- $ns at 0.0 "$node0 label Server"
- $ns at 0.0 "$node1 label Router"
- $ns at 0.0 "$node2 label Client_1"
- $ns at 0.0 "$node3 label Client_2"
- set udp0 [new Agent/UDP]
- $ns attach-agent $node0 $udp0
- set cbr0 [new Application/Traffic/CBR]
- $cbr0 set packetSize_ 500
- $cbr0 set interval_ .005
- $cbr0 attach-agent $udp0
- set udp1 [new Agent/UDP]
- $ns attach-agent $node1 $udp1
- set cbr1 [new Application/Traffic/CBR]
- $cbr1 set packetSize_ 500
- $cbr1 set interval_ .005
- $cbr1 attach-agent $udp1
- set sink0 [new Agent/Null]
- $ns attach-agent $node2 $sink0
- $ns connect $udp0 $sink0
- set sink1 [new Agent/Null]
- $ns attach-agent $node3 $sink1
- $ns connect $udp1 $sink1
- $ns at 0.2 "$cbr0 start"
- $ns at 0.5 "$cbr1 start"
- $ns at 4.5 "$cbr0 stop"
- $ns at 4.7 "$cbr1 stop"
- $ns at 5.0 "finish"
- $ns run
- //dsdv.tcl
- # wrls1.tcl
- # A 3-node example for ad-hoc simulation with DSDV
- # Define options
- set val(chan) Channel/WirelessChannel ;# channel type
- set val(prop) Propagation/TwoRayGround ;# radio-propagation model
- set val(netif) Phy/WirelessPhy ;# network interface type
- set val(mac) Mac/802_11 ;# MAC type
- set val(ifq) Queue/DropTail ;# interface queue type
- set val(ll) LL ;# link layer type
- set val(ant) Antenna/OmniAntenna ;# antenna model
- set val(ifqlen) 50 ;# max packet in ifq
- set val(nn) 3 ;# number of mobilenodes
- set val(rp) DSDV ;# routing protocol
- set val(x) 500 ;# X dimension of topography
- set val(y) 400 ;# Y dimension of topography
- set val(stop) 150 ;# time of simulation end
- set ns [new Simulator]
- set tracefd [open simple-dsdv.tr w]
- set windowVsTime2 [open win.tr w]
- set namtrace [open simwrls.nam w]
- $ns trace-all $tracefd
- $ns use-newtrace
- $ns namtrace-all-wireless $namtrace $val(x) $val(y)
- # set up topography object
- set topo [new Topography]
- $topo load_flatgrid $val(x) $val(y)
- create-god $val(nn)
- #
- # Create nn mobilenodes [$val(nn)] and attach them to the channel.
- #
- # configure the nodes
- $ns node-config -adhocRouting $val(rp) \
- -llType $val(ll) \
- -macType $val(mac) \
- -ifqType $val(ifq) \
- -ifqLen $val(ifqlen) \
- -antType $val(ant) \
- -propType $val(prop) \
- -phyType $val(netif) \
- -channelType $val(chan) \
- -topoInstance $topo \
- -agentTrace ON \
- -routerTrace ON \
- -macTrace OFF \
- -movementTrace ON
- for {set i 0} {$i < $val(nn) } { incr i } {
- set node_($i) [$ns node]
- }
- # Provide initial location of mobilenodes
- $node_(0) set X_ 5.0
- $node_(0) set Y_ 5.0
- $node_(0) set Z_ 0.0
- $node_(1) set X_ 490.0
- $node_(1) set Y_ 285.0
- $node_(1) set Z_ 0.0
- $node_(2) set X_ 150.0
- $node_(2) set Y_ 240.0
- $node_(2) set Z_ 0.0
- # Generation of movements
- $ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0"
- $ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0"
- $ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"
- # Set a TCP connection between node_(0) and node_(1)
- set tcp [new Agent/TCP/Newreno]
- $tcp set class_ 2
- set sink [new Agent/TCPSink]
- $ns attach-agent $node_(0) $tcp
- $ns attach-agent $node_(1) $sink
- $ns connect $tcp $sink
- set ftp [new Application/FTP]
- $ftp attach-agent $tcp
- $ns at 10.0 "$ftp start"
- # Define node initial position in nam
- for {set i 0} {$i < $val(nn)} { incr i } {
- # 30 defines the node size for nam
- $ns initial_node_pos $node_($i) 30
- }
- # Telling nodes when the simulation ends
- for {set i 0} {$i < $val(nn) } { incr i } {
- $ns at $val(stop) "$node_($i) reset";
- }
- # ending nam and the simulation
- $ns at $val(stop) "$ns nam-end-wireless $val(stop)"
- $ns at $val(stop) "stop"
- $ns at 150.01 "puts \"end simulation\" ; $ns halt"
- proc stop {} {
- global ns tracefd namtrace
- $ns flush-trace
- close $tracefd
- close $namtrace
- exec nam simwrls.nam &
- }
- $ns run
- //rsa
- #include<bits/stdc++.h>
- using namespace std;
- #define loop(i, a, b) for(i=a; i<b; i++)
- int solve_ext_gcd(int a,int b, int &x,int &y){
- if(a==0){
- x=0; y=1;
- return b;
- }
- int x1,y1;
- int g = solve_ext_gcd(b%a,a,x1,y1);
- x=y1-(b/a)*x1;
- y=x1;
- return g;
- }
- int solve_modulo_multi_inverse(int a, int m){
- int x, y;
- int g = solve_ext_gcd(a, m, x, y);
- if (g != 1) {
- cout <<"Inverse doesn't exist"; return -1;
- }
- else{
- int res=(x%m+m)%m;
- return res;
- }
- }
- int solve_fast_modulo_exponent(int x, int y, int n){
- int res = 1; x = x%n;
- while(y>0){
- if(y&1) res = (res*x)%n;
- y >>= 1;
- x = (x*x)%n;
- }
- return res;
- }
- void init_rsa(int *public_key, int *private_key, int *n){
- int p = 73, q = 227;
- int phi_n = (p - 1) * (q - 1);
- *public_key = 25;
- *n = p*q;
- *private_key = solve_modulo_multi_inverse(*public_key, phi_n);
- }
- int main(){
- //int p, q, e, i; cout<<"Enter prime1(p), prime2(q), public key(e) :";
- //cin>>p>>q>>e;
- int public_key, private_key, n, i;
- init_rsa(&public_key, &private_key, &n);
- string plain_text; cout<<"Enter Plain_text : "; cin>>plain_text;
- // int phi_n = (p-1)*(q-1);
- // e = 25
- // int d = solve_modulo_multi_inverse(e, phi_n);
- cout<<"Private Key :"<<private_key<<endl;
- // vector<int> cipher_code(plain_text.length());
- string str;
- cout<<"Cipher codes :"; // Encryption
- loop(i, 0, plain_text.length()-1){
- str += to_string(solve_fast_modulo_exponent(plain_text[i], public_key, n)) + "*";
- // cout<<cipher_code[i]<<" ";
- }
- str += to_string(solve_fast_modulo_exponent(plain_text[plain_text.length()-1], public_key, n));
- cout<<str;
- // cout<<"\nplain_text codes :"; // Decryption
- // loop(i, 0, plain_text.length()){
- // cout<<(;
- // }
- return 0;
- }
- //dsr
- set val(chan) Channel/WirelessChannel ;# channel type
- set val(prop) Propagation/TwoRayGround ;# radio-propagation model
- set val(netif) Phy/WirelessPhy ;# network interface type
- set val(mac) Mac/802_11 ;# MAC type
- set val(ifq) CMUPriQueue ;# interface queue type
- set val(ll) LL ;# link layer type
- set val(ant) Antenna/OmniAntenna ;# antenna model
- set val(ifqlen) 50 ;# max packet in ifq
- set val(nn) 5 ;# number of mobilenodes
- set val(rp) DSR ;# routing protocol
- set val(x) 500 ;# X dimension of topography
- set val(y) 400 ;# Y dimension of topography
- set val(stop) 50 ;# time of simulation end
- set ns [new Simulator]
- set tracefd [open simple-dsdv.tr w]
- set windowVsTime2 [open win.tr w]
- set namtrace [open simwrls.nam w]
- $ns trace-all $tracefd
- $ns use-newtrace
- $ns namtrace-all-wireless $namtrace $val(x) $val(y)
- # set up topography object
- set topo [new Topography]
- $topo load_flatgrid $val(x) $val(y)
- create-god $val(nn)
- #
- # Create nn mobilenodes [$val(nn)] and attach them to the channel.
- #
- # configure the nodes
- $ns node-config -adhocRouting $val(rp) \
- -llType $val(ll) \
- -macType $val(mac) \
- -ifqType $val(ifq) \
- -ifqLen $val(ifqlen) \
- -antType $val(ant) \
- -propType $val(prop) \
- -phyType $val(netif) \
- -channelType $val(chan) \
- -topoInstance $topo \
- -agentTrace ON \
- -routerTrace ON \
- -macTrace OFF \
- -movementTrace ON
- for {set i 0} {$i < $val(nn) } { incr i } {
- set node_($i) [$ns node]
- }
- for {set i 0} {$i < $val(nn)} {incr i} {
- $node_($i) set X_ [expr rand()*500]
- $node_($i) set Y_ [expr rand()*400]
- $node_($i) set Z_ 0
- }
- # Generation of movements
- #$ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0"
- #$ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0"
- #$ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"
- # Set a TCP connection between node_(0) and node_(1)
- set tcp [new Agent/TCP/Newreno]
- $tcp set class_ 2
- set sink [new Agent/TCPSink]
- $ns attach-agent $node_(0) $tcp
- $ns attach-agent $node_(5) $sink
- $ns connect $tcp $sink
- set ftp [new Application/FTP]
- $ftp attach-agent $tcp
- $ns at 2.0 "$ftp start"
- for {set i 0} {$i<$val(nn)} {incr i} {
- $ns initial_node_pos $node_($i) 30
- }
- # Telling nodes when the simulation ends
- for {set i 0} {$i < $val(nn) } { incr i } {
- $ns at $val(stop) "$node_($i) reset";
- }
- # ending nam and the simulation
- $ns at $val(stop) "$ns nam-end-wireless $val(stop)"
- $ns at $val(stop) "stop"
- $ns at 150.01 "puts \"end simulation\" ; $ns halt"
- proc stop {} {
- global ns tracefd namtrace
- $ns flush-trace
- close $tracefd
- close $namtrace
- exec nam simwrls.nam &
- }
- $ns run
- //aodv
- puts "Enter the routing protocol in mobile networking {1 : AODV, 2 : DSR}"
- set nrp [gets stdin]
- if {$nrp == 1} {
- set val(rp) AODV;
- } elseif {$nrp == 2} {
- set val(rp) DSR;
- }
- puts "Enter the no of nodes in network"
- set no_nodes [gets stdin]
- puts "Enter the node number to send data"
- set src_node [gets stdin]
- # Define options
- set val(chan) Channel/WirelessChannel ;# channel type
- set val(prop) Propagation/TwoRayGround ;# radio-propagation model
- set val(netif) Phy/WirelessPhy ;# network interface type
- set val(mac) Mac/802_11 ;# MAC type
- set val(ifq) Queue/DropTail ;# interface queue type
- set val(ll) LL ;# link layer type
- set val(ant) Antenna/OmniAntenna ;# antenna model
- set val(ifqlen) 50 ;# max packet in ifq
- set val(nn) $no_nodes ;# number of mobilenodes
- set val(x) 500 ;# X dimension of topography
- set val(y) 400 ;# Y dimension of topography
- set val(stop) 150 ;# time of simulation end
- set ns [new Simulator]
- set tracefd [open simple-dsdv.tr w]
- set windowVsTime2 [open win.tr w]
- set namtrace [open simwrls1.nam w]
- $ns trace-all $tracefd
- $ns use-newtrace
- $ns namtrace-all-wireless $namtrace $val(x) $val(y)
- # set up topography object
- set topo [new Topography]
- $topo load_flatgrid $val(x) $val(y)
- create-god $val(nn)
- #
- # Create nn mobilenodes [$val(nn)] and attach them to the channel.
- #
- # configure the nodes
- $ns node-config -adhocRouting $val(rp) \
- -llType $val(ll) \
- -macType $val(mac) \
- -ifqType $val(ifq) \
- -ifqLen $val(ifqlen) \
- -antType $val(ant) \
- -propType $val(prop) \
- -phyType $val(netif) \
- -channelType $val(chan) \
- -topoInstance $topo \
- -agentTrace ON \
- -routerTrace ON \
- -macTrace OFF \
- -movementTrace ON
- for {set i 0} {$i < $val(nn) } { incr i } {
- set node_($i) [$ns node]
- }
- # Provide initial location of mobilenodes
- for {set i 0} {$i < $val(nn)} {incr i} {
- $node_($i) set X_ [expr rand()*500]
- $node_($i) set Y_ [expr rand()*400]
- $node_($i) set Z_ 0
- }
- # Generation of movements
- $ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0"
- $ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0"
- $ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"
- # Set a TCP connection between node_(0) and node_(1)
- set tcp [new Agent/TCP/Newreno]
- $tcp set class_ 2
- set sink [new Agent/TCPSink]
- $ns attach-agent $node_($src_node) $tcp
- $ns attach-agent $node_(1) $sink
- $ns connect $tcp $sink
- set ftp [new Application/FTP]
- $ftp attach-agent $tcp
- $ns at 10.0 "$ftp start"
- # Define node initial position in nam
- for {set i 0} {$i < $val(nn)} { incr i } {
- # 30 defines the node size for nam
- $ns initial_node_pos $node_($i) 30
- }
- # Telling nodes when the simulation ends
- for {set i 0} {$i < $val(nn) } { incr i } {
- $ns at $val(stop) "$node_($i) reset";
- }
- # ending nam and the simulation
- $ns at $val(stop) "$ns nam-end-wireless $val(stop)"
- $ns at $val(stop) "stop"
- $ns at 150.01 "puts \"end simulation\" ; $ns halt"
- proc stop {} {
- global ns tracefd namtrace
- $ns flush-trace
- close $tracefd
- close $namtrace
- exec nam simwrls1.nam &
- exec awk -f aodv.awk simple-dsdv.tr > output.tr &
- }
- $ns run
- //Upload
- //client
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <unistd.h>
- #define IP_PROTOCOL 0
- #define IP_ADDRESS "127.0.0.1"
- #define PORT_NO 15050
- #define NET_BUF_SIZE 32
- #define cipherKey 'S'
- #define sendrecvflag 0
- void clearBuf(char* b)
- {
- int i;
- for (i = 0; i < NET_BUF_SIZE; i++)
- b[i] = '\0';
- }
- char Cipher(char ch)
- {
- return ch ^ cipherKey;
- }
- int recvFile(char* buf, int s)
- {
- int i;
- char ch;
- FILE *newfile;
- newfile = fopen("received_file.txt","a");
- for (i = 0; i < s; i++) {
- ch = buf[i];
- ch = Cipher(ch);
- if (ch == EOF){
- fclose(newfile);
- return 1;
- }
- else{
- printf("%c", ch);
- fprintf(newfile,"%c", ch);}
- }
- fclose(newfile);
- return 0;
- }
- int main()
- {
- int sockfd, nBytes;
- struct sockaddr_in addr_con;
- int addrlen = sizeof(addr_con);
- addr_con.sin_family = AF_INET;
- addr_con.sin_port = htons(PORT_NO);
- addr_con.sin_addr.s_addr = inet_addr(IP_ADDRESS);
- char net_buf[NET_BUF_SIZE];
- FILE* fp;
- sockfd = socket(AF_INET, SOCK_DGRAM,
- IP_PROTOCOL);
- if (sockfd < 0)
- printf("\nfile descriptor not received!!\n");
- else
- printf("\nfile descriptor %d received\n", sockfd);
- while (1) {
- printf("\nPlease enter file name to upload:\n");
- scanf("%s", net_buf);
- sendto(sockfd, net_buf, NET_BUF_SIZE,
- sendrecvflag, (struct sockaddr*)&addr_con,
- addrlen);
- printf("\n---------Data Uploaded---------\n");
- while(1){
- clearBuf(net_buf);
- nBytes = recvfrom(sockfd, net_buf, NET_BUF_SIZE,
- sendrecvflag, (struct sockaddr*)&addr_con,
- &addrlen);
- if (recvFile(net_buf, NET_BUF_SIZE)) {
- break;
- }
- }
- printf("\n-------------------------------\n");
- }
- return 0;
- }
- //server
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <unistd.h>
- #define IP_PROTOCOL 0
- #define PORT_NO 15050
- #define NET_BUF_SIZE 32
- #define cipherKey 'S'
- #define sendrecvflag 0
- #define nofile "File Not Found!"
- void clearBuf(char* b)
- {
- int i;
- for (i = 0; i < NET_BUF_SIZE; i++)
- b[i] = '\0';
- }
- char Cipher(char ch)
- {
- return ch ^ cipherKey;
- }
- int sendFile(FILE* fp, char* buf, int s)
- {
- int i, len;
- if (fp == NULL) {
- strcpy(buf, nofile);
- len = strlen(nofile);
- buf[len] = EOF;
- for (i = 0; i <= len; i++)
- buf[i] = Cipher(buf[i]);
- return 1;
- }
- char ch, ch2;
- for (i = 0; i < s; i++) {
- ch = fgetc(fp);
- ch2 = Cipher(ch);
- buf[i] = ch2;
- if (ch == EOF)
- return 1;
- }
- return 0;
- }
- int main()
- {
- int sockfd, nBytes;
- struct sockaddr_in addr_con;
- int addrlen = sizeof(addr_con);
- addr_con.sin_family = AF_INET;
- addr_con.sin_port = htons(PORT_NO);
- addr_con.sin_addr.s_addr = INADDR_ANY;
- char net_buf[NET_BUF_SIZE];
- FILE* fp;
- sockfd = socket(AF_INET, SOCK_DGRAM, IP_PROTOCOL);
- if (sockfd < 0)
- printf("\nfile descriptor not received!!\n");
- else
- printf("\nfile descriptor %d received\n", sockfd);
- if (bind(sockfd, (struct sockaddr*)&addr_con, sizeof(addr_con)) == 0)
- printf("\nSuccessfully binded!\n");
- else
- printf("\nBinding Failed!\n");
- while (1) {
- printf("\nWaiting for file name...\n");
- clearBuf(net_buf);
- nBytes = recvfrom(sockfd, net_buf,
- NET_BUF_SIZE, sendrecvflag,
- (struct sockaddr*)&addr_con, &addrlen);
- fp = fopen(net_buf, "r");
- printf("\nFile Name Received: %s\n", net_buf);
- if (fp == NULL)
- printf("\nFile open failed!\n");
- else
- printf("\nFile Successfully opened!\n");
- while (1) {
- if (sendFile(fp, net_buf, NET_BUF_SIZE)) {
- sendto(sockfd, net_buf, NET_BUF_SIZE,
- sendrecvflag,
- (struct sockaddr*)&addr_con, addrlen);
- break;
- }
- sendto(sockfd, net_buf, NET_BUF_SIZE,
- sendrecvflag,
- (struct sockaddr*)&addr_con, addrlen);
- clearBuf(net_buf);
- }
- if (fp != NULL)
- fclose(fp);
- }
- return 0;
- }
- //download
- //client
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <unistd.h>
- #define IP_PROTOCOL 0
- #define IP_ADDRESS "127.0.0.1"
- #define PORT_NO 15050
- #define NET_BUF_SIZE 32
- #define cipherKey 'S'
- #define sendrecvflag 0
- void clearBuf(char* b)
- {
- int i;
- for (i = 0; i < NET_BUF_SIZE; i++)
- b[i] = '\0';
- }
- char Cipher(char ch)
- {
- return ch ^ cipherKey;
- }
- int recvFile(char* buf, int s)
- {
- int i;
- char ch;
- FILE *newfile;
- newfile = fopen("received_file.txt","a");
- for (i = 0; i < s; i++) {
- ch = buf[i];
- ch = Cipher(ch);
- if (ch == EOF){
- fclose(newfile);
- return 1;
- }
- else{
- printf("%c", ch);
- fprintf(newfile,"%c", ch);}
- }
- fclose(newfile);
- return 0;
- }
- int main()
- {
- int sockfd, nBytes;
- struct sockaddr_in addr_con;
- int addrlen = sizeof(addr_con);
- addr_con.sin_family = AF_INET;
- addr_con.sin_port = htons(PORT_NO);
- addr_con.sin_addr.s_addr = inet_addr(IP_ADDRESS);
- char net_buf[NET_BUF_SIZE];
- FILE* fp;
- sockfd = socket(AF_INET, SOCK_DGRAM,
- IP_PROTOCOL);
- if (sockfd < 0)
- printf("\nfile descriptor not received!!\n");
- else
- printf("\nfile descriptor %d received\n", sockfd);
- while (1) {
- printf("\nPlease enter file name to download:\n");
- scanf("%s", net_buf);
- sendto(sockfd, net_buf, NET_BUF_SIZE,
- sendrecvflag, (struct sockaddr*)&addr_con,
- addrlen);
- printf("\n---------Data Downloaded---------\n");
- while(1){
- clearBuf(net_buf);
- nBytes = recvfrom(sockfd, net_buf, NET_BUF_SIZE,
- sendrecvflag, (struct sockaddr*)&addr_con,
- &addrlen);
- if (recvFile(net_buf, NET_BUF_SIZE)) {
- break;
- }
- }
- printf("\n-------------------------------\n");
- }
- return 0;
- }
- //server
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <unistd.h>
- #define IP_PROTOCOL 0
- #define PORT_NO 15050
- #define NET_BUF_SIZE 32
- #define cipherKey 'S'
- #define sendrecvflag 0
- #define nofile "File Not Found!"
- void clearBuf(char* b)
- {
- int i;
- for (i = 0; i < NET_BUF_SIZE; i++)
- b[i] = '\0';
- }
- char Cipher(char ch)
- {
- return ch ^ cipherKey;
- }
- int sendFile(FILE* fp, char* buf, int s)
- {
- int i, len;
- if (fp == NULL) {
- strcpy(buf, nofile);
- len = strlen(nofile);
- buf[len] = EOF;
- for (i = 0; i <= len; i++)
- buf[i] = Cipher(buf[i]);
- return 1;
- }
- char ch, ch2;
- for (i = 0; i < s; i++) {
- ch = fgetc(fp);
- ch2 = Cipher(ch);
- buf[i] = ch2;
- if (ch == EOF)
- return 1;
- }
- return 0;
- }
- int main()
- {
- int sockfd, nBytes;
- struct sockaddr_in addr_con;
- int addrlen = sizeof(addr_con);
- addr_con.sin_family = AF_INET;
- addr_con.sin_port = htons(PORT_NO);
- addr_con.sin_addr.s_addr = INADDR_ANY;
- char net_buf[NET_BUF_SIZE];
- FILE* fp;
- sockfd = socket(AF_INET, SOCK_DGRAM, IP_PROTOCOL);
- if (sockfd < 0)
- printf("\nfile descriptor not received!!\n");
- else
- printf("\nfile descriptor %d received\n", sockfd);
- if (bind(sockfd, (struct sockaddr*)&addr_con, sizeof(addr_con)) == 0)
- printf("\nSuccessfully binded!\n");
- else
- printf("\nBinding Failed!\n");
- while (1) {
- printf("\nWaiting for file name...\n");
- clearBuf(net_buf);
- nBytes = recvfrom(sockfd, net_buf,
- NET_BUF_SIZE, sendrecvflag,
- (struct sockaddr*)&addr_con, &addrlen);
- fp = fopen(net_buf, "r");
- printf("\nFile Name Received: %s\n", net_buf);
- if (fp == NULL)
- printf("\nFile open failed!\n");
- else
- printf("\nFile Successfully opened!\n");
- while (1) {
- if (sendFile(fp, net_buf, NET_BUF_SIZE)) {
- sendto(sockfd, net_buf, NET_BUF_SIZE,
- sendrecvflag,
- (struct sockaddr*)&addr_con, addrlen);
- break;
- }
- sendto(sockfd, net_buf, NET_BUF_SIZE,
- sendrecvflag,
- (struct sockaddr*)&addr_con, addrlen);
- clearBuf(net_buf);
- }
- if (fp != NULL)
- fclose(fp);
- }
- return 0;
- }
- //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.
- //client
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/socket.h>
- #include <stdlib.h>
- #include <netinet/in.h>
- #include <string.h>
- #define PORT 8080
- int main(int argc, char const *argv[])
- {
- struct sockaddr_in address;
- int sock = 0, valread;
- struct sockaddr_in serv_addr;
- if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
- {
- printf("\n Socket creation error \n");
- return -1;
- }
- memset(&serv_addr, '0', sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_port = htons(PORT);
- // Convert IPv4 and IPv6 addresses from text to binary form
- if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
- {
- printf("\nInvalid address/ Address not supported \n");
- return -1;
- }
- if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
- {
- printf("\nConnection Failed \n");
- return -1;
- }
- printf("connection created!\n");
- while(1)
- {
- char buffer[1024] = {0};
- printf("Enter Roll no to get details:");
- int roll_no;
- scanf("%d",&roll_no);
- int msg = htonl(roll_no);
- send(sock , &msg, sizeof(msg),0);
- valread=read( sock, buffer, 1024);
- printf("%s\n",buffer );
- }
- }
- //server
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/socket.h>
- #include <stdlib.h>
- #include <netinet/in.h>
- #include <string.h>
- struct db{
- int r;
- char name[30];
- };
- #define N 5
- #define PORT 8080
- int main(int argc, char const *argv[])
- {
- struct db record[N];
- int i=0;
- char end;
- printf("Enter records to database\n");
- for(i=0;i<N;i++){
- printf("roll no:");
- scanf("%d",&record[i].r);
- printf("name:");
- scanf("%s",record[i].name);
- }
- int server_fd, new_socket, valread;
- struct sockaddr_in address;
- int opt = 1;
- int addrlen = sizeof(address);
- char buffer[1024] = {0};
- // Creating socket file descriptor
- if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
- {
- perror("socket failed");
- exit(EXIT_FAILURE);
- }
- // Forcefully attaching socket to the port 8080
- if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
- &opt, sizeof(opt)))
- {
- perror("setsockopt");
- exit(EXIT_FAILURE);
- }
- address.sin_family = AF_INET;
- address.sin_addr.s_addr = INADDR_ANY;
- address.sin_port = htons( PORT );
- // Forcefully attaching socket to the port 8080
- if (bind(server_fd, (struct sockaddr *)&address,
- sizeof(address))<0)
- {
- perror("bind failed");
- exit(EXIT_FAILURE);
- }
- if (listen(server_fd, 3) < 0)
- {
- perror("listen");
- exit(EXIT_FAILURE);
- }
- if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0)
- {
- perror("accept");
- exit(EXIT_FAILURE);
- }
- while(1)
- {
- int msg,status=1;
- read( new_socket , &msg, sizeof(msg));
- int roll_no =ntohl(msg);
- printf("Request for details of roll No %d",roll_no);
- char rec[30]="No records found";
- for( i=0;i<N;i++)
- {
- if(record[i].r==roll_no)
- {
- send(new_socket , record[i].name , strlen(record[i].name ) , 0 );
- printf("Details of roll No %d sent",roll_no);
- status=0;
- break;
- }
- }
- if(status==1){
- send(new_socket , rec , strlen(rec) , 0 );
- printf("Staus sent: No records found");
- }
- }
- }
- //crc
- //client
- #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>
- using namespace std;
- #define PORT 8080
- int main(int argc, char const *argv[]){
- struct sockaddr_in address;
- int sock = 0, valread;
- struct sockaddr_in serv_addr;
- char bufer[2256] = {0};
- if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
- printf("\n Socket creation error \n");
- return -1;
- }
- memset(&serv_addr, '0', sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_port = htons(PORT);
- if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0){
- printf("\nInvalid address \n");
- return -1;
- }
- if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
- printf("\nConnection Failed \n");
- return -1;
- }
- while(1){
- long long codeword, key;
- cout<<" Enter the dataword with appended CRC(7, 4) and key (or) 0 to exit:";
- cin>>codeword>>key;
- send(sock, &codeword, sizeof(long long), 0);
- send(sock, &key, sizeof(long long), 0);
- if(codeword == 0 || key == 0) break;
- int len = recv(sock, bufer, sizeof(bufer), 0);
- string info;
- info.append(bufer, bufer + len);
- cout<<" Result : "<<info<<endl;
- }
- return 0;
- }
- //server
- #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>
- using namespace std;
- #define PORT 8080
- string Xor(string a, string b){
- string res;
- for(int i=0; i<b.length(); i++){
- if(a[i] == b[i]) res += "0";
- else res += "1";
- }
- return res;
- }
- string DecToBin(long long dec){
- string res ;
- while(dec > 0){
- res = to_string(dec%2) + res;
- dec = dec/2;
- }
- return res;
- }
- long long BinToDec(string bin){
- long long dec = 0;
- for(int i=0;i<bin.length(); i++){
- dec += pow(2, bin.length() - 1 - i)*(bin[i] - '0');
- }
- return dec;
- }
- long long crc(string divident, string divisor){
- int len = divisor.length();
- string tmp_res = divident.substr(0, len);
- while(len < divident.length()){
- if(tmp_res[0] == '1'){
- tmp_res = Xor(divisor, tmp_res).substr(1,3) + divident[len];
- } else{
- string tmp;
- for(int i=0; i< len; i++) tmp += '0';
- tmp_res = Xor(tmp, tmp_res).substr(1, 3) + divident[len];
- } len++;
- } if(tmp_res[0] == '1'){
- tmp_res = Xor(divisor, tmp_res);
- } else{
- string tmp;
- for(int i=0; i< len; i++) tmp += '0';
- tmp_res = Xor(tmp, tmp_res).substr(1, 3) + divident[len];
- } return BinToDec(tmp_res);
- }
- int main(int argc, char const *argv[]){
- int server_fd, new_socket, valread;
- struct sockaddr_in address;
- int opt = 1;
- int addrlen = sizeof(address);
- char bufer[2256] = {0};
- if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0){
- perror("socket failed");
- exit(EXIT_FAILURE);
- }
- if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR |
- SO_REUSEPORT,&opt, sizeof(opt))){
- perror("setsockopt");
- exit(EXIT_FAILURE);
- }
- address.sin_family = AF_INET;
- address.sin_addr.s_addr = INADDR_ANY;
- address.sin_port = htons( PORT );
- if (bind(server_fd, (struct sockaddr *)&address,sizeof(address))<0){
- perror("bind failed");
- exit(EXIT_FAILURE);
- }
- if (listen(server_fd, 3) < 0){
- perror("listen");
- exit(EXIT_FAILURE);
- }
- if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
- (socklen_t*)&addrlen))<0){
- perror("accept");
- exit(EXIT_FAILURE);
- }
- while(true){
- long long cwd, ky;
- valread = read(new_socket, &cwd, sizeof(long long));
- valread = read(new_socket, &ky, sizeof(long long));
- if(cwd == 0 || ky == 0) break;
- string codeword = DecToBin(cwd);
- string key = DecToBin(ky);
- string res;
- crc(codeword, key) == 0 ? res = "good data" : res = "bad data";
- send(new_socket, res.data(), res.size(), 0);
- }
- return 0;
- }
- //Question answers
- //client
- #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>
- using namespace std;
- #define PORT 8080
- int main(int argc, char const *argv[]){
- struct sockaddr_in address;
- int sock = 0, valread;
- struct sockaddr_in serv_addr;
- char bufer[2256] = {0};
- if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
- printf("\n Socket creation error \n");
- return -1;
- }
- memset(&serv_addr, '0', sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_port = htons(PORT);
- if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0){
- printf("\nInvalid address \n");
- return -1;
- } if (
- connect(sock, (
- struct sockaddr *)&serv_addr, sizeof(serv_addr)) <
- 0){
- printf("\nConnection Failed \n");
- return -1;
- }
- while(true){
- string ques;
- cout<<"Enter The Question (all lowercase) (or) 0 to exit ?\n Q : ";
- getline(cin, ques);
- send(sock, ques.data(), ques.size(), 0);
- if(ques == "0") break;
- int len = recv(sock, bufer, sizeof(bufer), 0);
- string res;
- res.append(bufer, bufer + len);
- cout<<" A : "<<res<<endl;
- }
- return 0;
- }
- //server
- #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>
- using namespace std;
- #define PORT 8080
- map<string, string> mp;
- int main(int argc, char const *argv[]){
- int server_fd, new_socket, valread;
- struct sockaddr_in address;
- int opt = 1;
- int addrlen = sizeof(address);
- char bufer[2256] = {0};
- mp["who am i"] = "My Boss";
- mp["who is your daddy"] = "You daddy";
- if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0){
- perror("socket failed");
- exit(EXIT_FAILURE);
- } if (
- setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR |
- SO_REUSEPORT,&opt, sizeof(opt))){
- perror("setsockopt");
- exit(EXIT_FAILURE);
- }
- address.sin_family = AF_INET;
- address.sin_addr.s_addr = INADDR_ANY;
- address.sin_port = htons( PORT );
- if (bind(server_fd, (struct sockaddr *)&address,sizeof(address))<0){
- perror("bind failed");
- exit(EXIT_FAILURE);
- }
- if (listen(server_fd, 3) < 0){
- perror("listen");
- exit(EXIT_FAILURE);
- } if ((new_socket =
- accept(server_fd, (
- struct sockaddr *)&address,
- (socklen_t*)&addrlen))<0){
- perror("accept");
- exit(EXIT_FAILURE);
- }
- while(true){
- string ques;
- int len = recv(new_socket, bufer, sizeof(bufer), 0);
- ques.append(bufer, bufer + len);
- cout<<"Q."<<ques<<endl;
- if(ques == "0") break;
- send(new_socket, mp[ques].data(), mp[ques].size(), 0);
- }
- return 0;
- }
- //dynamic aodv
- puts "Enter the routing protocol in mobile networking {1 : AODV, 2 : DSR}"
- set nrp [gets stdin]
- if {$nrp == 1} {
- set val(rp) AODV;
- } elseif {$nrp == 2} {
- set val(rp) DSR;
- }
- puts "Enter the no of nodes in network"
- set no_nodes [gets stdin]
- puts "Enter the node number to send data"
- set src_node [gets stdin]
- # Define options
- set val(chan) Channel/WirelessChannel ;# channel type
- set val(prop) Propagation/TwoRayGround ;# radio-propagation model
- set val(netif) Phy/WirelessPhy ;# network interface type
- set val(mac) Mac/802_11 ;# MAC type
- set val(ifq) Queue/DropTail ;# interface queue type
- set val(ll) LL ;# link layer type
- set val(ant) Antenna/OmniAntenna ;# antenna model
- set val(ifqlen) 50 ;# max packet in ifq
- set val(nn) $no_nodes ;# number of mobilenodes
- set val(x) 500 ;# X dimension of topography
- set val(y) 400 ;# Y dimension of topography
- set val(stop) 150 ;# time of simulation end
- set ns [new Simulator]
- set tracefd [open simple-dsdv.tr w]
- set windowVsTime2 [open win.tr w]
- set namtrace [open simwrls1.nam w]
- $ns trace-all $tracefd
- $ns use-newtrace
- $ns namtrace-all-wireless $namtrace $val(x) $val(y)
- # set up topography object
- set topo [new Topography]
- $topo load_flatgrid $val(x) $val(y)
- create-god $val(nn)
- #
- # Create nn mobilenodes [$val(nn)] and attach them to the channel.
- #
- # configure the nodes
- $ns node-config -adhocRouting $val(rp) \
- -llType $val(ll) \
- -macType $val(mac) \
- -ifqType $val(ifq) \
- -ifqLen $val(ifqlen) \
- -antType $val(ant) \
- -propType $val(prop) \
- -phyType $val(netif) \
- -channelType $val(chan) \
- -topoInstance $topo \
- -agentTrace ON \
- -routerTrace ON \
- -macTrace OFF \
- -movementTrace ON
- for {set i 0} {$i < $val(nn) } { incr i } {
- set node_($i) [$ns node]
- }
- # Provide initial location of mobilenodes
- for {set i 0} {$i < $val(nn)} {incr i} {
- $node_($i) set X_ [expr rand()*500]
- $node_($i) set Y_ [expr rand()*400]
- $node_($i) set Z_ 0
- }
- # Generation of movements
- $ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0"
- $ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0"
- $ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"
- # Set a TCP connection between node_(0) and node_(1)
- set tcp [new Agent/TCP/Newreno]
- $tcp set class_ 2
- set sink [new Agent/TCPSink]
- $ns attach-agent $node_($src_node) $tcp
- $ns attach-agent $node_(1) $sink
- $ns connect $tcp $sink
- set ftp [new Application/FTP]
- $ftp attach-agent $tcp
- $ns at 10.0 "$ftp start"
- # Define node initial position in nam
- for {set i 0} {$i < $val(nn)} { incr i } {
- # 30 defines the node size for nam
- $ns initial_node_pos $node_($i) 30
- }
- # Telling nodes when the simulation ends
- for {set i 0} {$i < $val(nn) } { incr i } {
- $ns at $val(stop) "$node_($i) reset";
- }
- # ending nam and the simulation
- $ns at $val(stop) "$ns nam-end-wireless $val(stop)"
- $ns at $val(stop) "stop"
- $ns at 150.01 "puts \"end simulation\" ; $ns halt"
- proc stop {} {
- global ns tracefd namtrace
- $ns flush-trace
- close $tracefd
- close $namtrace
- exec nam simwrls1.nam &
- exec awk -f aodv.awk simple-dsdv.tr > output.tr &
- }
- $ns run
Add Comment
Please, Sign In to add comment