· 7 years ago · Oct 23, 2018, 05:22 AM
1
2
32.a
4
5import java.io.*;
6
7import java.net.*;
8
9class TestServer
10
11{
12
13 public static void main(String ar[])
14
15 {
16
17 try {
18
19 ServerSocket ss=new ServerSocket(2000);
20
21 System.out.println("\n Waiting .....");
22
23 Socket s=ss.accept();
24
25 System.out.println("Accepted..");
26
27 InputStream in=s.getInputStream();
28
29 byte b[]=new byte[7];
30
31 in.read(b,0,6);
32
33 System.out.println(new String(b));
34
35 }
36
37 catch(Exception e)
38
39 {
40
41 System.out.println(e);
42
43 }
44
45 }
46
47}
48
49----------------------------------
50
51import java.io.*;
52
53import java.net.*;
54
55
56
57
58
59class TestClient
60
61{
62
63 public static void main(String ar[])
64
65 {
66
67 try {
68
69
70
71 Socket s=new Socket("localhost",2000);
72
73 OutputStream out=s.getOutputStream();
74
75 out.write("Hello".getBytes());
76
77 }
78
79 catch(Exception e) { System.out.println(e); }
80
81 }
82
83}
84
85
86
87
88
892.b
90
91
92
93
94
95import java.net.DatagramPacket;
96
97import java.net.DatagramSocket;
98
99import java.net.InetAddress;
100
101
102
103
104
105public class UDPsend {
106
107 public static void main(String[] args) throws Exception {
108
109 DatagramSocket ds = new DatagramSocket();
110
111 String str = "hello world";
112
113 InetAddress ia = InetAddress.getByName("192.169.0.231");
114
115 DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), ia, 3000);
116
117 ds.send(dp);
118
119 ds.close();
120
121 }
122
123}
124
125import java.net.DatagramPacket;
126
127import java.net.DatagramSocket;
128
129
130
131
132
133public class UDPreceive {
134
135 public static void main(String[] args) throws Exception {
136
137 DatagramSocket ds = new DatagramSocket(3000);
138
139 byte[] buf = new byte[1024];
140
141 DatagramPacket dp = new DatagramPacket(buf, 1024);
142
143 ds.receive(dp);
144
145 String strRecv = new String(dp.getData(), 0, dp.getLength()) + " from "
146
147 + dp.getAddress().getHostAddress() + ":" + dp.getPort();
148
149 System.out.println(strRecv);
150
151 ds.close();
152
153 }
154
155}
156
157
158
159
160
161
162
163
164
1653. Secured communication through encryption and decryption of messages.
166
167import java.io.*;
168
169import java.net.*;
170
171class serverencrypt
172
173{
174
175 public static void main(String args[])
176
177 {
178
179 try
180
181 {
182
183 ServerSocket ss = new ServerSocket(3000);
184
185 Socket s = ss.accept();
186
187 System.out.println("Connected...");
188
189 byte b[]= new byte[7];
190
191 OutputStream out = s.getOutputStream();
192
193 String t ="Amazing";
194
195 String k = new String();
196
197 int j;
198
199 int key = 3;
200
201 for (int i=0;i<t.length();i++)
202
203 { j=(int)t.charAt(i);
204
205 k=k+(char)(j+key);
206
207}
208
209System.out.println("\n Encrypted form "+k);
210
211out.write(k.getBytes());}
212
213catch(Exception e)
214
215{
216
217 System.out.println(e);
218
219}
220
221}}
222
223
224
225
226
2274. Program for Remote procedure call under client server environment (RMI)
228
229/* Program PrimeServer.java */
230
231import java.net.*;
232
233import java.rmi.*;
234
235import java.io.*;
236
237public class PrimeServer {
238
239public static void main(String args[]) {
240
241try {
242
243System.out.println("waiting for client.....");
244
245PrimeServerImpl prime1ServerImpl = new PrimeServerImpl();
246
247Naming.rebind("PrimeServer", prime1ServerImpl);
248
249}
250
251catch(Exception e) {
252
253System.out.println("Exception: " + e);
254
255}
256
257}
258
259}
260
261
262
263
264
265
266
267
268
269/* Program PrimeClient.java */
270
271import java.rmi.*;
272
273import java.io.*;
274
275public class PrimeClient
276
277{
278
279public static void main(String args[])
280
281{
282
283int n;
284
285try {
286
287String PrimeServerURL = "rmi://localhost/PrimeServer";
288
289PrimeServerIntf prime1ServerIntf =
290
291(PrimeServerIntf)Naming.lookup(PrimeServerURL);
292
293//System.out.println("Enter n" +args[1]);
294
295System.out.println("Enter n");
296
297InputStreamReader ir=new InputStreamReader(System.in);
298
299BufferedReader br=new BufferedReader(ir);
300
301n=Integer.parseInt(br.readLine());
302
303System.out.println("The count is: " + prime1ServerIntf.prime(n));
304
305}
306
307catch(Exception e) {
308
309System.out.println("Exception: " + e);
310
311}
312
313}
314
315}
316
317
318
319
320
321/* Program PrimeServerIntf */
322
323import java.rmi.*;
324
325public interface PrimeServerIntf extends Remote
326
327{
328
329int prime(int n) throws RemoteException;
330
331}
332
333
334
335
336
337/* Program PrimeServerImpl.java */
338
339import java.io.*;
340
341import java.rmi.*;
342
343import java.rmi.server.*;
344
345public class PrimeServerImpl extends UnicastRemoteObject
346
347implements PrimeServerIntf {
348
349public PrimeServerImpl() throws RemoteException {}
350
351public int prime(int n) throws RemoteException {
352
353int count=0;
354
355int i;
356
357if(n==1)
358
359{
360
361System.out.println("1 is neither prime nor composite");
362
363System.exit(0);
364
365}
366
367for(i=2;i<n/2;i++)
368
369{
370
371if(n%i==0)
372
373{
374
375count++;
376
377}
378
379}
380
381if(count>0)
382
383{
384
385System.out.println("the entered number is not prime");
386
387}
388
389else
390
391{
392
393System.out.println("the entered no is prime");
394
395}
396
397
398
399
400
401return count;
402
403}
404
405}
406
407
408
409
410
411
412
413
414
415
416
417
418
4196.
420
421a. Program for error correction using CRC
422
423/**** TCPServerCRC.java ***/
424
425import java.io.*;import java.util.*;
426
427import java.net.*;
428
429import java.nio.*;
430
431class TcpServerCRC
432
433{
434
435public static void main(String args[]) throws Exception
436
437{try{
438
439 ServerSocket ss = new ServerSocket(2222);
440
441 Socket soc=ss.accept();
442
443 byte j;String s;StringBuffer sb=new StringBuffer();
444
445 String str=new BufferedReader(new InputStreamReader(soc.getInputStream())).readLine();
446
447 InputStream fin = new FileInputStream(str);int m=0;
448
449 do
450
451 {
452
453 j = (byte)fin.read();
454
455//s[m]=j.toString();
456
457 sb.append((char)j);
458
459 m++;}while(j!=-1);
460
461
462
463 int[] divisor;int divisor_bits,tot_length;int[] div; int[] rem;int[] crc;
464
465 String s1="";
466
467 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
468
469 s=sb.toString();System.out.println("original data "+s);
470
471 System.out.println("Enter number of bits in divisor : ");
472
473 divisor_bits=Integer.parseInt(br.readLine());
474
475 divisor=new int[divisor_bits];
476
477 System.out.println("Enter Divisor bits : ");
478
479 for(int i=0; i<divisor_bits; i++)
480
481 divisor[i]=Integer.parseInt(br.readLine());
482
483
484
485
486
487 tot_length=s.length()+divisor_bits-1;//System.out.println("length "+tot_length);
488
489 div=new int[tot_length];
490
491 rem=new int[tot_length];
492
493 crc=new int[tot_length];
494
495s="["+s+"]";
496
497//System.out.println("data");
498
499
500
501
502
503String s8[]=s.replaceAll("\\[","").replaceAll("\\]","").split("");int data[]=new int[s8.length];
504
505//System.out.println("s8 length "+s8.length);
506
507for(int i=0;i<s8.length;i++)
508
509{
510
511try
512
513{data[i]=Integer.parseInt(s8[i]);}catch(Exception e){}}
514
515
516
517
518
519
520
521
522
523/*for(int i=0;i<data.length;i++)
524
525{
526
527try
528
529{System.out.print(data[i]);
530
531}
532
533catch(Exception e){}}
534
535*/System.out.println();
536
537
538
539
540
541
542
543
544
545
546
547
548
549for(int i=0;i<data.length;i++)
550
551 {
552
553try{ div[i]=data[i]; }catch(Exception e){} }
554
555System.out.print("Dividend (after appending 0's) are : ");
556
557 for(int i=0; i< div.length; i++)
558
559 {System.out.print(div[i]); }
560
561 System.out.println();
562
563
564
565
566
567
568
569
570
571 for(int i=0; i<div.length; i++){
572
573 rem[i] = div[i];
574
575 }
576
577
578
579
580
581
582
583 rem=divide(div, divisor, rem);
584
585
586
587
588
589
590
591
592
593 for(int i=0;i<div.length;i++) //append dividend and ramainder
594
595 {
596
597 crc[i]=(div[i]^rem[i]);
598
599 }
600
601
602
603 System.out.println();
604
605 System.out.println("CRC code : ");
606
607 for(int i=0;i<crc.length;i++)
608
609 { System.out.print(crc[i]);}
610
611System.out.println();
612
613//s.replaceAll("\\[","").replaceAll("\\]","").split("")
614
615//Arrays.sort(crc);
616
617//String[] a=Arrays.toString(crc).replaceAll("\\[","").replaceAll("\\]","").replaceAll("\\,,","").replaceAll("\\,","").split("");
618
619 // a=crc.replaceAll("\\[","").replaceAll("\\]","").replaceAll("\\,","").split("");
620
621//s1=Arrays.toString(a);
622
623
624
625
626
627String a=Arrays.toString(crc);
628
629String ar[]=a.substring(1,a.length()-1).split(", ,, ,");
630
631System.out.println(Arrays.toString(ar));
632
633s1=Arrays.toString(ar);
634
635
636
637
638
639
640
641
642
643String s9[]=s1.replaceAll("\\[","").replaceAll("\\]","").replaceAll("\\,","").split("");
644
645
646
647
648
649
650
651
652
653for(int i=0;i<s9.length;i++)
654
655{
656
657System.out.print(s9[i].toString());
658
659}
660
661// System.out.println("msg to b sent "+s1);
662
663File f1=new File("t.txt");
664
665FileOutputStream fs=new FileOutputStream(f1);
666
667if(s1!=null)
668
669{
670
671for(int i=0;i<s9.length;i++)
672
673{
674
675byte jj[]=s9[i].getBytes();
676
677fs.write(jj);
678
679}}fs.close();
680
681
682
683
684
685InputStream f=new FileInputStream("t.txt");
686
687{
688
689do
690
691{
692
693j=(byte)f.read();
694
695soc.getOutputStream().write(j);
696
697 }while(j!=-1);
698
699}}
700
701catch(Exception e)
702
703{}
704
705}
706
707
708
709
710
711 static int[] divide(int div[],int divisor[], int rem[])
712
713 {
714
715 int cur=0;
716
717 while(true)
718
719 {
720
721 for(int i=0;i<divisor.length;i++)
722
723 rem[cur+i]=(rem[cur+i]^divisor[i]);
724
725
726
727 while(rem[cur]==0 && cur!=rem.length-1)
728
729 cur++;
730
731
732
733 if((rem.length-cur)<divisor.length)
734
735 break;
736
737 }
738
739 return rem;
740
741 }
742
743}
744
745
746
747
748
749 /**** TCPClientCRC.java ****/
750
751import java.io.*;
752
753import java.net.*;
754
755public class TcpclientCRC
756
757{
758
759public static void main(String arg[])throws Exception
760
761{try{
762
763 Socket socket = new Socket("127.0.0.1",2222);
764
765 byte b;
766
767 StringBuffer sb = new StringBuffer();
768
769
770
771
772
773 InputStream in = socket.getInputStream();int e=0;
774
775 PrintStream out = new PrintStream(new DataOutputStream(socket.getOutputStream()));
776
777 String filename=new BufferedReader(new InputStreamReader(System.in)).readLine();
778
779 out.println(filename);
780
781 while((b=(byte)in.read())!=-1)
782
783 {
784
785 sb.append((char)b);
786
787 }
788
789
790
791
792
793
794
795String s1=sb.toString();System.out.println(s1);
796
797//String s8[]=s1.replaceAll("\\[","").replaceAll("\\]","").split("");
798
799
800
801
802
803//int data[]=new int[s8.length];
804
805
806
807
808
809//s="["+s+"]";
810
811
812
813
814
815
816
817
818
819String s8[]=s1.replaceAll("\\ ","").split(",");
820
821int data[]=new int[10];
822
823//data[0]=Integer.parseInt(s8[0]);
824
825System.out.println("s8 length "+s8.length);
826
827//int j=0;
828
829String az=s8[0].toString();
830
831System.out.println("string az "+az+" length "+az.length());
832
833for(int i=0;i<s8.length;i++)
834
835{
836
837
838
839
840
841String s="";
842
843/*if(s8[i].startsWith(","))
844
845{
846
847}*/
848
849
850
851
852
853data[i]=Integer.parseInt(s8[i]);
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871//j++;
872
873//i=i+2;
874
875}
876
877System.out.println("data");
878
879for(int i=0;i<1;i++)
880
881{
882
883
884
885System.out.print(data[i]);
886
887
888
889
890
891
892
893
894
895}
896
897System.out.println();
898
899System.out.print("string");
900
901System.out.println();
902
903for(int i=0;i<s8.length;i++)
904
905{
906
907System.out.print(s8[i].toString());
908
909}
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931az="["+az+"]";
932
933
934
935
936
937String sbuf[]=az.replaceAll("\\[","").replaceAll("\\]","").split("");
938
939int datacrc[]=new int[sbuf.length];
940
941System.out.println("sbuf length "+sbuf.length);
942
943for(int i=0;i<sbuf.length;i++)
944
945{
946
947try
948
949{datacrc[i]=Integer.parseInt(sbuf[i]);
950
951}catch(Exception ex){}
952
953}
954
955
956
957
958
959System.out.println("datacrc");
960
961for(int i=0;i<datacrc.length;i++)
962
963{
964
965try
966
967{System.out.print(datacrc[i]);
968
969}
970
971catch(Exception ex){}
972
973}
974
975
976
977
978
979System.out.println();
980
981int[] divisor=new int[4];
982
983int[] rem=new int[datacrc.length];
984
985BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
986
987System.out.println("enter the divisor bits");
988
989for(int i=0; i<4; i++)
990
991 { divisor[i]=Integer.parseInt(br.readLine());}
992
993
994
995
996
997 for(int j=0; j<datacrc.length; j++)
998
999 {
1000
1001 rem[j] =datacrc[j];
1002
1003 }
1004
1005
1006
1007rem=divide(datacrc, divisor, rem);
1008
1009
1010
1011 for(int i=0; i< rem.length; i++)
1012
1013 {
1014
1015 if(rem[i]!=0)
1016
1017 {
1018
1019 System.out.println("result : Error");
1020
1021 break;
1022
1023 }
1024
1025 if(i==rem.length-1)
1026
1027 System.out.println("result :No Error");
1028
1029 }
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043}catch(Exception e){}}
1044
1045
1046
1047 static int[] divide(int div[],int divisor[], int rem[])
1048
1049 {
1050
1051 int cur=0;
1052
1053 while(true)
1054
1055 {
1056
1057 for(int i=0;i<divisor.length;i++)
1058
1059 rem[cur+i]=(rem[cur+i]^divisor[i]);
1060
1061
1062
1063 while(rem[cur]==0 && cur!=rem.length-1)
1064
1065 cur++;
1066
1067
1068
1069 if((rem.length-cur)<divisor.length)
1070
1071 break;
1072
1073 }
1074
1075 return rem;
1076
1077 }
1078
1079}
1080
1081
1082
1083
1084
1085
1086
1087b. Program for error detection using hamming code
1088
1089
1090
1091
1092
1093/* program TestServer1ham.java */
1094
1095import java.io.*;
1096
1097import java.net.*;
1098
1099import java.util.*;
1100
1101public class Testserver1ham
1102
1103{
1104
1105static int set_parity_bit(int a[])
1106
1107{
1108
1109int count=0; //........Initialising count to zero which will count the number of 1.
1110
1111int l=a.length;
1112
1113
1114
1115
1116
1117for(int i=0;i<l;++i)
1118
1119if(a[i]==1)
1120
1121++count; //............Incrementing count if value in array "a" is 1.
1122
1123
1124
1125
1126
1127if((count%2)==0)
1128
1129return 0;//........Returning 0 if even number of 1
1130
1131else
1132
1133return 1;//........Returning 1 if odd number of 1
1134
1135}
1136
1137
1138
1139
1140
1141public static void main(String args[])throws IOException
1142
1143{
1144
1145ServerSocket ss;
1146
1147Socket s;
1148
1149 try
1150
1151 {
1152
1153 System.out.println("waiting ");
1154
1155 ss=new ServerSocket(8085);
1156
1157 s=ss.accept();
1158
1159 System.out.println("connection established");
1160
1161 Scanner scr= new Scanner(System.in);
1162
1163 System.out.println("HAMMING CODE");
1164
1165 System.out.println();
1166
1167 System.out.println("Enter 4 data bits");
1168
1169 int n=4;
1170
1171 int d[]=new int[4];
1172
1173 for(int i=n-1;i>=0;--i)
1174
1175 {
1176
1177 System.out.println("Enter the value of D"+(i+1));
1178
1179 d[i]=scr.nextInt();
1180
1181 }
1182
1183
1184
1185
1186
1187 /*.............. Formula for calculating 2^k>=n+k+1 ...............*/
1188
1189
1190
1191
1192
1193 int k=0;// k stands for number of parity bits.Initializing it to zero.
1194
1195
1196
1197
1198
1199 while(Math.pow(2,k)<(n+k+1)) // Calculating the value of k(number of parity bits).
1200
1201 {
1202
1203 ++k;
1204
1205 }
1206
1207 System.out.println();
1208
1209 System.out.println(k+" parity bits are required for the transmission of data bits.");
1210
1211
1212
1213
1214
1215 int parity[]=new int[k]; //..........Array to store parity bits
1216
1217 int h[]=new int[n+k+1];//.........Array to hold the hamming code.(n+k+1) as we start from pos 1.
1218
1219
1220
1221
1222
1223/********** Initialising array h[] to -1 ************/
1224
1225 for(int i=0;i<=7;++i)
1226
1227 h[i]=-1;
1228
1229
1230
1231
1232
1233 int count=0;
1234
1235 int c=2;
1236
1237 while(count<4)
1238
1239 {
1240
1241 ++c;
1242
1243 if(c==4)
1244
1245 continue;
1246
1247
1248
1249 h[c]=d[count];
1250
1251 ++count;
1252
1253 }
1254
1255 int p1[]={h[1],h[3],h[5],h[7]};
1256
1257 int p2[]={h[2],h[3],h[6],h[7]};
1258
1259 int p3[]={h[4],h[5],h[6],h[7]};
1260
1261
1262
1263
1264
1265/************Setting the value of parity bit*************/
1266
1267 parity[0]=set_parity_bit(p1);
1268
1269 parity[1]=set_parity_bit(p2);
1270
1271 parity[2]=set_parity_bit(p3);
1272
1273/************Inserting the parity bits in the hamming code**********/
1274
1275 h[1]=parity[0];
1276
1277 h[2]=parity[1];
1278
1279 h[4]=parity[2];
1280
1281 System.out.println("\nSENDER:");
1282
1283 System.out.print("\nThe data bits entered are: ");
1284
1285 for(int i=3;i>=0;--i)
1286
1287 System.out.print(d[i]+" ");
1288
1289
1290
1291
1292
1293 System.out.println("\nThe Parity bits are: ");
1294
1295 for(int i=2;i>=0;--i)
1296
1297 System.out.println("Value of P"+(i+1)+" is "+parity[i]+" ");
1298
1299
1300
1301
1302
1303 System.out.print("\nThe Hamming code is as follows ");
1304
1305 for(int i=(n+k);i>0;--i)
1306
1307 System.out.print(h[i]+" ");
1308
1309 OutputStream out= s.getOutputStream();
1310
1311 byte t[]=new byte[10];int i=0;byte x;
1312
1313 while((x=(byte)h[i])!=-1)
1314
1315 {
1316
1317 t[i]=x;
1318
1319 i++;
1320
1321 }
1322
1323 out.write(t);
1324
1325 }catch(Exception e){System.out.println("Exception:"+e);}
1326
1327}
1328
1329 }
1330
1331
1332
1333
1334
1335/* TestClient1ham.java */
1336
1337import java.io.*;
1338
1339import java.net.*;
1340
1341import java.util.*;
1342
1343class Testclient1ham
1344
1345{
1346
1347static int set_parity_bit(int a[])
1348
1349 {
1350
1351 int count=0; //........Initialising count to zero which will count the number of 1.
1352
1353 int l=a.length;
1354
1355
1356
1357
1358
1359 for(int i=0;i<l;++i)
1360
1361 if(a[i]==1)
1362
1363 ++count; //............Incrementing count if value in array "a" is 1.
1364
1365
1366
1367
1368
1369 if((count%2)==0)
1370
1371 return 0;//........Returning 0 if even number of 1
1372
1373 else
1374
1375 return 1;//........Returning 1 if odd number of 1
1376
1377 }
1378
1379public static void main(String ar[])throws IOException
1380
1381 {
1382
1383 try
1384
1385 {
1386
1387 Socket s=new Socket("127.0.0.1",8085);
1388
1389 byte b[]= new byte[10];
1390
1391 InputStream in=s.getInputStream();in.read(b,0,10);
1392
1393 Scanner scr=new Scanner(System.in);
1394
1395 int n=4;int k=0;
1396
1397
1398
1399 while(Math.pow(2,k)<(n+k+1)) // Calculating the value of k(number of parity bits).
1400
1401 {
1402
1403 ++k;
1404
1405 }
1406
1407 System.out.println();
1408
1409 int parity[]=new int[k];
1410
1411 int h[]=new int[n+k+1];
1412
1413 System.out.println("Enter the hamming code with error at any position of your choice.\nNOTE: ERROR should be present only at one bit position");
1414
1415 for(int i=7;i>0;--i)
1416
1417 h[i]=scr.nextInt();
1418
1419
1420
1421
1422
1423 int p4[]={h[1],h[3],h[5],h[7]};
1424
1425 int p5[]={h[2],h[3],h[6],h[7]};
1426
1427 int p6[]={h[4],h[5],h[6],h[7]};
1428
1429
1430
1431
1432
1433 parity[0]=set_parity_bit(p4);
1434
1435 parity[1]=set_parity_bit(p5);
1436
1437 parity[2]=set_parity_bit(p6);
1438
1439
1440
1441
1442
1443 int position=(int)(parity[2]*Math.pow(2,2)+parity[1]*Math.pow(2,1)+parity[0]*Math.pow(2,0));
1444
1445 System.out.println("\nRECEIVER:");
1446
1447 System.out.println("Error is detected at position "+position+" at the receiving end.");
1448
1449 System.out.println("Correcting the error.... ");
1450
1451
1452
1453
1454
1455 if(h[position]==1)
1456
1457 h[position]=0;
1458
1459 else
1460
1461 h[position]=1;
1462
1463
1464
1465
1466
1467 System.out.print("The correct code is ");
1468
1469 for(int i=7;i>0;--i)
1470
1471 System.out.print(h[i]+" ");
1472
1473 }
1474
1475 catch(Exception e)
1476
1477 {
1478
1479 System.out.println(e);
1480
1481 }
1482
1483 }
1484
1485 }
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
15231. RS232
1524
1525
1526
1527
1528
1529“RS232EXAMPLE.JAVAâ€Â
1530
1531import gnu.io.CommPortIdentifier;
1532
1533import gnu.io.SerialPort;
1534
1535import gnu.io.SerialPortEventListener;
1536
1537import gnu.io.PortInUseException;
1538
1539import gnu.io.*;
1540
1541import java.awt.*;
1542
1543import java.awt.event.*;
1544
1545import java.io.*;
1546
1547import java.util.*;
1548
1549
1550
1551public class RS232Example extends Frame implements ActionListener, SerialPortEventListener
1552
1553{
1554
1555 TextField tf = new TextField(20);
1556
1557 Button sd = new Button("Send");
1558
1559 public TextArea ta = new TextArea(10, 50);
1560
1561 OutputStream out;
1562
1563 InputStream in;
1564
1565 byte[] buffer = new byte[1024];
1566
1567 int tail = 0;
1568
1569 public RS232Example(String Title)
1570
1571 {
1572
1573 super(Title);
1574
1575 setLayout(new FlowLayout());
1576
1577 add(tf);
1578
1579 add(sd);
1580
1581 add(ta);
1582
1583 sd.addActionListener(this);
1584
1585 addWindowListener(new wa(this));
1586
1587 }
1588
1589 public void actionPerformed(ActionEvent ae)
1590
1591 {
1592
1593 String mes = tf.getText()+"\n";
1594
1595 send(mes.getBytes());
1596
1597 }
1598
1599 public void serialEvent(SerialPortEvent event)
1600
1601 {
1602
1603 switch(event.getEventType())
1604
1605 {
1606
1607 case SerialPortEvent.BI:
1608
1609 case SerialPortEvent.OE:
1610
1611 case SerialPortEvent.FE:
1612
1613 case SerialPortEvent.PE:
1614
1615 case SerialPortEvent.CD:
1616
1617 case SerialPortEvent.CTS:
1618
1619 case SerialPortEvent.DSR:
1620
1621 case SerialPortEvent.RI:
1622
1623 case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
1624
1625 break;
1626
1627 case SerialPortEvent.DATA_AVAILABLE:
1628
1629 byte b;
1630
1631 try
1632
1633 {
1634
1635 while((b = (byte)in.read()) != -1)
1636
1637 {
1638
1639 if (b=='\n')
1640
1641 {
1642
1643 onMessage();
1644
1645 }
1646
1647 else
1648
1649 {
1650
1651 buffer[tail] = b;
1652
1653 tail++;
1654
1655 }
1656
1657 }
1658
1659 onMessage();
1660
1661 }
1662
1663 catch (IOException e){System.out.println("IO Exception");}
1664
1665 }
1666
1667 }
1668
1669
1670
1671
1672
1673 public void send(byte[] bytes)
1674
1675 {
1676
1677 try {
1678
1679 ta.appendText("SENDING: " + new String(bytes, 0, bytes.length));
1680
1681 out.write(bytes);
1682
1683 out.flush();
1684
1685 }
1686
1687 catch (IOException e)
1688
1689 {
1690
1691 e.printStackTrace();
1692
1693 }
1694
1695 }
1696
1697 public void connect(String portName) throws Exception
1698
1699 {
1700
1701 //CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
1702
1703 ta.appendText("connecting...\n");
1704
1705 try
1706
1707 {
1708
1709 SerialPort serialPort = (SerialPort) portIdentifier.open("RS232Example", 2000);
1710
1711 try
1712
1713 {
1714
1715 serialPort.addEventListener(this);
1716
1717 } catch (TooManyListenersException e) {}
1718
1719 serialPort.notifyOnDataAvailable(true);
1720
1721 serialPort.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
1722
1723 out = serialPort.getOutputStream();
1724
1725 in = serialPort.getInputStream();
1726
1727 }
1728
1729 catch (PortInUseException e){
1730
1731 System.out.println("Port in use! Run again by giving another port name as argument");
1732
1733 System.exit(0);
1734
1735 }
1736
1737 }
1738
1739 private void onMessage() {
1740
1741 if (tail!=0) {
1742
1743 String message = new String(buffer, 0, tail);
1744
1745 ta.appendText("RECEIVED : " + message+"\n");
1746
1747 if ("BYE".equals(message)) {
1748
1749 send("ACK\n".getBytes());
1750
1751 this.dispose();
1752
1753 System.exit(0);
1754
1755 }
1756
1757 else if ("ACK".equals(message)) {
1758
1759 this.dispose();
1760
1761 System.exit(0);
1762
1763 }
1764
1765 tail = 0;
1766
1767 }
1768
1769 }
1770
1771
1772
1773 public static void main(String[] args) throws Exception {
1774
1775 RS232Example re = new RS232Example(args[0]);
1776
1777 re.setVisible(true);
1778
1779 re.setSize(300, 300);
1780
1781 re.connect(args[0]);
1782
1783
1784
1785 }
1786
1787}
1788
1789class wa extends WindowAdapter {
1790
1791 RS232Example r;
1792
1793public wa(RS232Example r){
1794
1795 this.r = r;
1796
1797}
1798
1799public void windowClosing(WindowEvent we){
1800
1801 r.setVisible(false);
1802
1803 r.dispose();
1804
1805 System.exit(0);
1806
1807}
1808
1809}
1810
1811PROTOCOL.JAVA
1812
1813public interface Protocol {
1814
1815
1816
1817 // protocol manager handles each received byte
1818
1819 void onReceive(byte b);
1820
1821
1822
1823 // protocol manager handles broken stream
1824
1825 void onStreamClosed();
1826
1827}
1828
1829ListAvailablePorts.java
1830
1831 import gnu.io.CommPortIdentifier;
1832
1833
1834
1835 import java.util.Enumeration;
1836
1837
1838
1839 public class ListAvailablePorts {
1840
1841
1842
1843 public void list() {
1844
1845 Enumeration ports = CommPortIdentifier.getPortIdentifiers();
1846
1847
1848
1849 while(ports.hasMoreElements())
1850
1851 System.out.println(((CommPortIdentifier)ports.nextElement()).getName());
1852
1853 }
1854
1855
1856
1857 public static void main(String[] args) {
1858
1859 new ListAvailablePorts().list();
1860
1861 }
1862
1863}
1864
1865Commportreceiver.java
1866
1867import java.io.IOException;
1868
1869import java.io.InputStream;
1870
1871
1872
1873public class CommPortReceiver extends Thread {
1874
1875
1876
1877 InputStream in;
1878
1879 Protocol protocol = new Protocolmpl();
1880
1881
1882
1883 public CommPortReceiver(InputStream in) {
1884
1885 this.in = in;
1886
1887 }
1888
1889
1890
1891 public void run() {
1892
1893 try {
1894
1895 int b;
1896
1897 while(true) {
1898
1899
1900
1901 // if stream is not bound in.read() method returns -1
1902
1903 while((b = in.read()) != -1) {
1904
1905 protocol.onReceive((byte) b);
1906
1907 }
1908
1909 protocol.onStreamClosed();
1910
1911
1912
1913 // wait 10ms when stream is broken and check again
1914
1915 sleep(10);
1916
1917 }
1918
1919 } catch (IOException e) {
1920
1921 e.printStackTrace();
1922
1923 } catch (InterruptedException e) {
1924
1925 e.printStackTrace();
1926
1927 }
1928
1929 }
1930
1931}
1932
1933COMMPORTSENDER.JAVA
1934
1935 import java.io.IOException;
1936
1937 import java.io.OutputStream;
1938
1939
1940
1941 public class CommPortSender {
1942
1943
1944
1945 static OutputStream out;
1946
1947
1948
1949 public static void setWriterStream(OutputStream out) {
1950
1951 CommPortSender.out = out;
1952
1953 }
1954
1955
1956
1957 public static void send(byte[] bytes) {
1958
1959 try {
1960
1961 System.out.println("SENDING: " + new String(bytes, 0, bytes.length));
1962
1963
1964
1965 // sending through serial port is simply writing into OutputStream
1966
1967 out.write(bytes);
1968
1969 out.flush();
1970
1971 } catch (IOException e) {
1972
1973 e.printStackTrace();
1974
1975 }
1976
1977 }
1978
1979 }
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989------
1990
19911. SLIDING WINDOW PROTOCOL
1992
19931. GO BACK N
1994
1995//Go-Back-N
1996
1997import java.io.*;
1998
1999import java.net.*;
2000
2001import java.util.*;
2002
2003public class GBNclient
2004
2005{
2006
2007public static void main(String args[])
2008
2009{
2010
2011try
2012
2013{
2014
2015while(true)
2016
2017{
2018
2019Socket s=new Socket("localhost",5555);
2020
2021System.out.println("socket created");
2022
2023BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
2024
2025String st=br.readLine();
2026
2027System.out.println("the frames are:"+st+"\nenter the frame where there is an error:");
2028
2029s.close();
2030
2031s=new Socket("localhost",5555);
2032
2033Scanner scanner=new Scanner(System.in);
2034
2035String error=scanner.nextLine();
2036
2037DataOutputStream out=new DataOutputStream(s.getOutputStream());
2038
2039out.writeBytes(error);
2040
2041System.out.println("sent");
2042
2043s.close();
2044
2045Thread.sleep(100);
2046
2047}
2048
2049}catch(Exception e)
2050
2051{}
2052
2053}
2054
2055}
2056
2057
2058
2059
2060
2061import java.io.*;
2062
2063import java.net.*;
2064
2065public class GBNserver
2066
2067{
2068
2069static int burst=5;
2070
2071public static void main(String args[])
2072
2073{
2074
2075try
2076
2077{
2078
2079int x=0,i;
2080
2081String error=" ";
2082
2083while(true && x<20)
2084
2085{
2086
2087String data=error;
2088
2089error=" ";
2090
2091int len=data.length();
2092
2093for(i=0;i<burst;i++,x++)
2094
2095data+=(char)('A'+x);
2096
2097System.out.println("the new set to be sent is:"+data);
2098
2099ServerSocket ss=new ServerSocket(5555);
2100
2101Socket s=ss.accept();
2102
2103DataOutputStream out=new DataOutputStream(s.getOutputStream());
2104
2105out.writeBytes(data);
2106
2107System.out.println("sent");
2108
2109s.close();
2110
2111ss.close();
2112
2113Thread.sleep(100);
2114
2115ss=new ServerSocket(5555);
2116
2117s=ss.accept();
2118
2119BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
2120
2121System.out.println("waiting....");
2122
2123error=br.readLine();
2124
2125System.out.println("the error is at:"+error);
2126
2127s.close();
2128
2129ss.close();
2130
2131}
2132
2133}catch(Exception e)
2134
2135{}
2136
2137}
2138
2139}
2140
2141
2142
2143//Selective Repeat
2144
2145//SERVER PGM
2146
2147import java.io.*;
2148
2149import java.net.*;
2150
2151public class SRserver
2152
2153{
2154
2155static int burst=5;
2156
2157public static void main(String args[]) throws Exception
2158
2159{
2160
2161try
2162
2163{
2164
2165int i, error=0;
2166
2167//String err=" ";
2168
2169while(true)
2170
2171{
2172
2173int x=error;
2174
2175String data=" ";
2176
2177int len=data.length();
2178
2179for(i=0;i<len;i++,x++)
2180
2181data+=(char)('A'+x);
2182
2183System.out.println("\n The new window to be sent is:"+data);
2184
2185ServerSocket ss=new ServerSocket(5555);
2186
2187Socket s=ss.accept();
2188
2189DataOutputStream out= new DataOutputStream(s.getOutputStream());
2190
2191out.writeBytes(data);
2192
2193System.out.println("\n sent");
2194
2195s.close();
2196
2197ss.close();
2198
2199Thread.sleep(100);
2200
2201ss=new ServerSocket(5555);
2202
2203s=ss.accept();
2204
2205BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
2206
2207System.out.println("\nwaiting");
2208
2209error=Integer.parseInt(br.readLine());
2210
2211System.out.println("\nError is at: "+error);
2212
2213s.close();
2214
2215ss.close();
2216
2217}
2218
2219}
2220
2221catch(Exception e)
2222
2223{}
2224
2225}
2226
2227}
2228
2229---
2230
2231//Selective Repeat
2232
2233//CLIENT PGM
2234
2235import java.io.*;
2236
2237import java.net.*;
2238
2239import java.util.*;
2240
2241public class SRclient
2242
2243{
2244
2245public static void main(String args[]) throws Exception
2246
2247{
2248
2249try
2250
2251{
2252
2253while(true)
2254
2255{
2256
2257Socket s=new Socket("localhost",5555);
2258
2259System.out.println("socket created");
2260
2261BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
2262
2263System.out.println("make duplicate to retransmit");
2264
2265String st=br.readLine();
2266
2267System.out.println("The frames are: "+st);
2268
2269System.out.println("Enter the first damaged frame: ");
2270
2271s.close();
2272
2273s=new Socket("localhost",5555);
2274
2275Scanner scanner=new Scanner(System.in);
2276
2277String error=scanner.nextLine();
2278
2279DataOutputStream out=new DataOutputStream(s.getOutputStream());
2280
2281out.writeBytes(error);
2282
2283System.out.println("sent");
2284
2285s.close();
2286
2287Thread.sleep(100);
2288
2289}}
2290
2291catch(Exception e)
2292
2293{}
2294
2295}
2296
2297}
2298
2299-------------
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
23411. Distance vector
2342
2343import java.io.*;
2344
2345
2346
2347
2348
2349public class dist
2350
2351{
2352
2353
2354
2355 public static void main(String arg[]) throws IOException
2356
2357 {
2358
2359 int i,j,k,n,src;
2360
2361 int a[][]=new int[10][10];
2362
2363 String ch;
2364
2365 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
2366
2367 System.out.println("\n Enter the number of nodes = ");
2368
2369 n=Integer.parseInt(in.readLine());
2370
2371 for(i=1;i<=n;i++)
2372
2373 {
2374
2375 for(j=i;j<=n;j++)
2376
2377 {
2378
2379 if(i==j) a[i][j]=0;
2380
2381 else
2382
2383 {
2384
2385 System.out.println("\n Enter the distance between host : "+i+" and "+j);
2386
2387 a[i][j]=a[j][i]=Integer.parseInt(in.readLine());
2388
2389 }
2390
2391 }
2392
2393 }
2394
2395 System.out.print("\n");
2396
2397 for(i=1;i<=n;i++)
2398
2399 {
2400
2401 for(j=1;j<=n;j++) System.out.print(a[i][j]+" ");
2402
2403 System.out.print("\n");
2404
2405 }
2406
2407 for(k=1;k<=n;k++)
2408
2409 for(i=1;i<=n;i++)
2410
2411 for(j=1;j<=n;j++)
2412
2413
2414
2415
2416
2417 if(a[i][j]>a[i][k]+a[k][j])
2418
2419 a[i][j]=a[i][k]+a[k][j];
2420
2421 do
2422
2423 {
2424
2425 System.out.println("\n Enter the node to display the routing table : ");
2426
2427 src=Integer.parseInt(in.readLine());
2428
2429 for(j=1;j<=n;j++)
2430
2431 {
2432
2433 if(src!=j)
2434
2435 {
2436
2437 if(a[src][j]!=100) System.out.println("\n The shortest path from "+src+" to "+j+" is "+a[src][j]);
2438
2439 else System.out.println("\n There is no path from"+src+" to "+j);
2440
2441 }
2442
2443 }
2444
2445 System.out.println("\n Do u want to continue(yes/no) : ");
2446
2447 ch=in.readLine();
2448
2449 }while(ch.equals("yes"));
2450
2451
2452
2453 }
2454
2455}
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489// dijkstra
2490
2491// A Java program for Dijkstra's single source shortest path algorithm.
2492
2493// The program is for adjacency matrix representation of the graph
2494
2495import java.util.*;
2496
2497import java.lang.*;
2498
2499import java.io.*;
2500
2501
2502
2503class ShortestPath
2504
2505{
2506
2507 // A utility function to find the vertex with minimum distance value,
2508
2509 // from the set of vertices not yet included in shortest path tree
2510
2511 static final int V=9;
2512
2513 int minDistance(int dist[], Boolean sptSet[])
2514
2515 {
2516
2517 // Initialize min value
2518
2519 int min = Integer.MAX_VALUE, min_index=-1;
2520
2521
2522
2523 for (int v = 0; v < V; v++)
2524
2525 if (sptSet[v] == false && dist[v] <= min)
2526
2527 {
2528
2529 min = dist[v];
2530
2531 min_index = v;
2532
2533 }
2534
2535
2536
2537 return min_index;
2538
2539 }
2540
2541
2542
2543 // A utility function to print the constructed distance array
2544
2545 void printSolution(int dist[], int n)
2546
2547 {
2548
2549 System.out.println("Vertex Distance from Source");
2550
2551 for (int i = 0; i < V; i++)
2552
2553 System.out.println(i+" \t\t "+dist[i]);
2554
2555 }
2556
2557
2558
2559 // Funtion that implements Dijkstra's single source shortest path
2560
2561 // algorithm for a graph represented using adjacency matrix
2562
2563 // representation
2564
2565 void dijkstra(int graph[][], int src) {
2566
2567 int dist[] = new int[V]; // The output array. dist[i] will hold
2568
2569 // the shortest distance from src to i
2570
2571
2572
2573 // sptSet[i] will true if vertex i is included in shortest
2574
2575 // path tree or shortest distance from src to i is finalized
2576
2577 Boolean sptSet[] = new Boolean[V];
2578
2579
2580
2581 // Initialize all distances as INFINITE and stpSet[] as false
2582
2583 for (int i = 0; i < V; i++)
2584
2585 {
2586
2587 dist[i] = Integer.MAX_VALUE;
2588
2589 sptSet[i] = false;
2590
2591 }
2592
2593
2594
2595 // Distance of source vertex from itself is always 0
2596
2597 dist[src] = 0;
2598
2599
2600
2601 // Find shortest path for all vertices
2602
2603 for (int count = 0; count < V-1; count++)
2604
2605 {
2606
2607 // Pick the minimum distance vertex from the set of vertices
2608
2609 // not yet processed. u is always equal to src in first
2610
2611 // iteration.
2612
2613 int u = minDistance(dist, sptSet);
2614
2615
2616
2617 // Mark the picked vertex as processed
2618
2619 sptSet[u] = true;
2620
2621
2622
2623 // Update dist value of the adjacent vertices of the
2624
2625 // picked vertex.
2626
2627 for (int v = 0; v < V; v++)
2628
2629
2630
2631 // Update dist[v] only if is not in sptSet, there is an
2632
2633 // edge from u to v, and total weight of path from src to
2634
2635 // v through u is smaller than current value of dist[v]
2636
2637 if (!sptSet[v] && graph[u][v]!=0 &&
2638
2639 dist[u] != Integer.MAX_VALUE &&
2640
2641 dist[u]+graph[u][v] < dist[v])
2642
2643 dist[v] = dist[u] + graph[u][v];
2644
2645 }
2646
2647
2648
2649 // print the constructed distance array
2650
2651 printSolution(dist, V);
2652
2653 }
2654
2655
2656
2657 // Driver method
2658
2659 public static void main (String[] args)
2660
2661 {
2662
2663 /* Let us create the example graph discussed above */
2664
2665 int graph[][] = new int[][]{{0, 4, 0, 0, 0, 0, 0, 8, 0},
2666
2667 {4, 0, 8, 0, 0, 0, 0, 11, 0},
2668
2669 {0, 8, 0, 7, 0, 4, 0, 0, 2},
2670
2671 {0, 0, 7, 0, 9, 14, 0, 0, 0},
2672
2673 {0, 0, 0, 9, 0, 10, 0, 0, 0},
2674
2675 {0, 0, 4, 0, 10, 0, 2, 0, 0},
2676
2677 {0, 0, 0, 14, 0, 2, 0, 1, 6},
2678
2679 {8, 11, 0, 0, 0, 0, 1, 0, 7},
2680
2681 {0, 0, 2, 0, 0, 0, 6, 7, 0}
2682
2683 };
2684
2685 ShortestPath t = new ShortestPath();
2686
2687 t.dijkstra(graph, 0);
2688
2689 }
2690
2691}
2692
2693
2694
2695
2696
2697 Run on IDE
2698
2699
2700
2701Output:
2702
2703Vertex Distance from Source0 01 42 123 194 215 116 97
270488 14
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
27501. Sys ctl commands
2751
2752sysctl - configure kernel parameters at runtime
2753
2754SYNOPSIS
2755
2756
2757
2758
2759
2760sysctl [-n] [-e] variable ...
2761
2762sysctl [-n] [-e] -w variable=value ...
2763
2764sysctl [-n] [-e] -p <filename> (default /etc/sysctl.conf)
2765
2766sysctl [-n] [-e] -a
2767
2768sysctl [-n] [-e] -A
2769
2770DESCRIPTION
2771
2772
2773
2774
2775
2776sysctl is used to modify kernel parameters at runtime. The parameters available are those listed under /proc/sys/. Procfs is required for sysctl(8) support in
2777Linux. You can use sysctl(8) to both read and write sysctl data.
2778
2779PARAMETERS
2780
2781
2782
2783
2784
2785variable
2786
2787The name of a key to read from. An example is kernel.ostype. The '/' separator is also accepted in place of a '.'.
2788
2789variable=value
2790
2791To set a key, use the form variable=value, where variable is the key and value is the value to set it to. If the value contains quotes or characters which are
2792parsed by the shell, you may need to enclose the value in double quotes. This requires the -w parameter to use.
2793
2794-n
2795
2796Use this option to disable printing of the key name when printing values.
2797
2798-e
2799
2800Use this option to ignore errors about unknown keys.
2801
2802-w
2803
2804Use this option when you want to change a sysctl setting.
2805
2806-p
2807
2808Load in sysctl settings from the file specified or /etc/sysctl.conf if none given.
2809
2810-a
2811
2812Display all values currently available.
2813
2814-A
2815
2816Display all values currently available in table form.
2817
2818
2819
2820EXAMPLES
2821
2822
2823
2824
2825
2826/sbin/sysctl -a
2827
2828
2829
2830
2831
2832/sbin/sysctl -n kernel.hostname
2833
2834
2835
2836
2837
2838/sbin/sysctl -w kernel.domainname="example.com"
2839
2840
2841
2842
2843
2844/sbin/sysctl -p /etc/sysctl.conf
2845
2846
2847
2848
2849
2850sudo sysctl -p
2851
2852without giving a specific file, it will read from /etc
2853
2854/sysctl.conf by default.
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868TCP keep alive time
2869
2870
2871
2872
2873
2874TCP keepalive
2875
2876Transmission Control Protocol (TCP) keepalives are an optional feature, and if included must default to off. The keepalive packet contains null data. In an
2877Ethernet network, a keepalive frame length is 60 bytes, while the server response to this, also a null data frame, is 54 bytes.[3] There are three parameters
2878related to keepalive:
2879
2880
2881
2882
2883
2884Keepalive time is the duration between two keepalive transmissions in idle condition. TCP keepalive period is required to be configurable and by default is set to
2885no less than 2 hours.
2886
2887Keepalive interval is the duration between two successive keepalive retransmissions, if acknowledgement to the previous keepalive transmission is not received.
2888
2889Keepalive retry is the number of retransmissions to be carried out before declaring that remote end is not available.
2890
2891------------------------------------------------------------------
2892
2893
2894
2895
2896
2897Use sysctl -A to get a list of available kernel variables
2898
2899and grep this list for net.ipv4 settings (sysctl -A | grep net.ipv4).
2900
2901There should exist the following variables:
2902
2903- net.ipv4.tcp_keepalive_time - time of connection inactivity after which
2904
2905 the first keep alive request is sent
2906
2907- net.ipv4.tcp_keepalive_probes - number of keep alive requests retransmitted
2908
2909 before the connection is considered broken
2910
2911- net.ipv4.tcp_keepalive_intvl - time interval between keep alive probes
2912
2913
2914
2915
2916
2917You can manipulate with these settings using the following command:
2918
2919
2920
2921
2922
2923sysctl -w net.ipv4.tcp_keepalive_time=60 net.ipv4.tcp_keepalive_probes=3 net.ipv4.tcp_keepalive_intvl=10
2924
2925
2926
2927
2928
2929This sample command changes TCP keepalive timeout to 60 seconds with 3 probes,
2930
293110 seconds gap between each. With this, your application will detect dead TCP connections after 90 seconds (60 + 10 + 10 + 10).
2932
2933
2934
2935
2936
293711. Simulation of wired and wireless network
2938
29391. A) i. wired TCP
2940
2941#Create a simulator objectset ns [new Simulator]#Define different colors for data flows (for NAM)$ns color 1 Blue$ns color 2 Red#Open the NAM trace fileset nf
2942[open out.nam w]$ns namtrace-all $nf#Define a 'finish' procedureproc finish {} { global ns nf $ns flush-trace #Close the NAM trace file
2943close $nf #Execute NAM on the trace file exec nam out.nam & exit 0}#Create four nodesset n0 [$ns node]set n1 [$ns node]set n2 [$ns node]set
2944n3 [$ns node]#Create links between the nodes$ns duplex-link $n0 $n2 2Mb 10ms DropTail$ns duplex-link $n1 $n2 2Mb 10ms DropTail$ns duplex-link $n2 $n3 1.7Mb 20ms
2945DropTail#Set Queue Size of link (n2-n3) to 10$ns queue-limit $n2 $n3 10#Give node position (for NAM)$ns duplex-link-op $n0 $n2 orient right-down$ns duplex-link-op
2946$n1 $n2 orient right-up$ns duplex-link-op $n2 $n3 orient right#Monitor the queue for link (n2-n3). (for NAM)$ns duplex-link-op $n2 $n3 queuePos 0.5#Setup a TCP
2947connectionset tcp [new Agent/TCP]$tcp set class_ 2$ns attach-agent $n0 $tcpset sink [new Agent/TCPSink]$ns attach-agent $n3 $sink$ns connect $tcp $sink$tcp set
2948fid_ 1#Setup a FTP over TCP connectionset ftp [new Application/FTP]$ftp attach-agent $tcp$ftp set type_ FTP#Setup a UDP connectionset udp [new Agent/UDP]$ns
2949attach-agent $n1 $udpset null [new Agent/Null]$ns attach-agent $n3 $null$ns connect $udp $null$udp set fid_ 2#Setup a CBR over UDP connectionset cbr [new
2950Application/Traffic/CBR]$cbr attach-agent $udp$cbr set type_ CBR$cbr set packet_size_ 1000$cbr set rate_ 1mb$cbr set random_ false#Schedule events for the CBR and
2951FTP agents$ns at 0.1 "$cbr start"$ns at 1.0 "$ftp start"$ns at 4.0 "$ftp stop"$ns at 4.5 "$cbr stop"#Detach tcp and sink agents (not really necessary)$ns at 4.5
2952"$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"#Call the finish procedure after 5 seconds of simulation time$ns at 5.0 "finish"#Print CBR packet size and
2953intervalputs "CBR packet size = [$cbr set packet_size_]"puts "CBR interval = [$cbr set interval_]"#Run the simulation$ns run
2954
2955
2956
2957
2958
2959
2960
29611. A ) ii. Wired UDP
2962
2963set ns [new Simulator]
2964
2965
2966
2967
2968
2969set nf1 [open out.nam w]
2970
2971$ns namtrace-all $nf1
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981set n0 [$ns node]
2982
2983set n1 [$ns node]
2984
2985
2986
2987
2988
2989$ns duplex-link $n0 $n1 1Mb 25ms DropTail
2990
2991
2992
2993
2994
2995set udp0 [new Agent/UDP]
2996
2997$ns attach-agent $n0 $udp0
2998
2999
3000
3001
3002
3003set cbr0 [new Application/Traffic/CBR]
3004
3005$cbr0 set packetSize_ 500
3006
3007$cbr0 set interval_ 0.05
3008
3009$cbr0 attach-agent $udp0
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025set null0 [new Agent/Null]
3026
3027$ns attach-agent $n1 $null0
3028
3029
3030
3031
3032
3033
3034
3035$ns connect $udp0 $null0
3036
3037
3038
3039
3040
3041
3042
3043$udp0 set class_ 1
3044
3045$ns color 1 Blue
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057$ns at 0.3 "$cbr0 start"
3058
3059
3060
3061
3062
3063$ns at 4.0 "$cbr0 stop"
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079proc finish {} {
3080
3081 global ns nf1
3082
3083 $ns flush-trace
3084
3085 close $nf1
3086
3087 exec nam out.nam &
3088
3089 exit 0
3090
3091}
3092
3093
3094
3095
3096
3097$ns at 5.0 "finish"
3098
3099
3100
3101
3102
3103$ns run
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
31171. b wireless ns2
3118
3119#Copyright (c) 1997 Regents of the University of California.
3120
3121# All rights reserved.
3122
3123#
3124
3125# Redistribution and use in source and binary forms, with or without
3126
3127# modification, are permitted provided that the following conditions
3128
3129# are met:
3130
3131# 1. Redistributions of source code must retain the above copyright
3132
3133# notice, this list of conditions and the following disclaimer.
3134
3135# 2. Redistributions in binary form must reproduce the above copyright
3136
3137# notice, this list of conditions and the following disclaimer in the
3138
3139# documentation and/or other materials provided with the distribution.
3140
3141# 3. All advertising materials mentioning features or use of this software
3142
3143# must display the following acknowledgement:
3144
3145# This product includes software developed by the Computer Systems
3146
3147# Engineering Group at Lawrence Berkeley Laboratory.
3148
3149# 4. Neither the name of the University nor of the Laboratory may be used
3150
3151# to endorse or promote products derived from this software without
3152
3153# specific prior written permission.
3154
3155#
3156
3157# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
3158
3159# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3160
3161# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3162
3163# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3164
3165# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3166
3167# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3168
3169# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3170
3171# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3172
3173# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3174
3175# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3176
3177# SUCH DAMAGE.
3178
3179#
3180
3181# $Header: /nfs/jade/vint/CVSROOT/ns-2/tcl/ex/wireless-mitf.tcl,v 1.2 2000/08/30 00:10:45 haoboy Exp $
3182
3183#
3184
3185# Simple demo script for the new APIs to support multi-interface for
3186
3187# wireless node.
3188
3189#
3190
3191# Define options
3192
3193# Please note:
3194
3195# 1. you can still specify "channelType" in node-config right now:
3196
3197# set val(chan) Channel/WirelessChannel
3198
3199# $ns_ node-config ...
3200
3201# -channelType $val(chan)
3202
3203# ...
3204
3205# But we recommend you to use node-config in the way shown in this script
3206
3207# for your future simulations.
3208
3209#
3210
3211# 2. Because the ad-hoc routing agents do not support multiple interfaces
3212
3213# currently, this script can't generate anything interesting if you config
3214
3215# the interfaces of node 1 and 2 on different channels
3216
3217#
3218
3219# --Xuan Chen, USC/ISI, July 21, 2000
3220
3221#
3222
3223set val(chan) Channel/WirelessChannel ;#Channel Type
3224
3225set val(prop) Propagation/TwoRayGround ;# radio-propagation model
3226
3227set val(netif) Phy/WirelessPhy ;# network interface type
3228
3229set val(mac) Mac/802_11 ;# MAC type
3230
3231set val(ifq) Queue/DropTail/PriQueue ;# interface queue type
3232
3233set val(ll) LL ;# link layer type
3234
3235set val(ant) Antenna/OmniAntenna ;# antenna model
3236
3237set val(ifqlen) 50 ;# max packet in ifq
3238
3239set val(nn) 2 ;# number of mobilenodes
3240
3241set val(rp) DSDV ;# routing protocol
3242
3243#set val(rp) DSR ;# routing protocol
3244
3245set val(x) 500
3246
3247set val(y) 500
3248
3249
3250
3251
3252
3253# Initialize Global Variables
3254
3255set ns_ [new Simulator]
3256
3257set tracefd [open wireless_mitf.tr w]
3258
3259$ns_ trace-all $tracefd
3260
3261
3262
3263
3264
3265set namtrace [open wireless_mitf.nam w]
3266
3267$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)
3268
3269
3270
3271
3272
3273# set up topography object
3274
3275set topo [new Topography]
3276
3277
3278
3279
3280
3281$topo load_flatgrid $val(x) $val(y)
3282
3283
3284
3285
3286
3287# Create God
3288
3289create-god $val(nn)
3290
3291
3292
3293
3294
3295# New API to config node:
3296
3297# 1. Create channel (or multiple-channels);
3298
3299# 2. Specify channel in node-config (instead of channelType);
3300
3301# 3. Create nodes for simulations.
3302
3303
3304
3305
3306
3307# Create channel #1 and #2
3308
3309set chan_1_ [new $val(chan)]
3310
3311set chan_2_ [new $val(chan)]
3312
3313
3314
3315
3316
3317# Create node(0) "attached" to channel #1
3318
3319
3320
3321
3322
3323# configure node, please note the change below.
3324
3325$ns_ node-config -adhocRouting $val(rp) \
3326
3327 -llType $val(ll) \
3328
3329 -macType $val(mac) \
3330
3331 -ifqType $val(ifq) \
3332
3333 -ifqLen $val(ifqlen) \
3334
3335 -antType $val(ant) \
3336
3337 -propType $val(prop) \
3338
3339 -phyType $val(netif) \
3340
3341 -topoInstance $topo \
3342
3343 -agentTrace ON \
3344
3345 -routerTrace ON \
3346
3347 -macTrace ON \
3348
3349 -movementTrace OFF \
3350
3351 -channel $chan_1_
3352
3353
3354
3355
3356
3357set node_(0) [$ns_ node]
3358
3359
3360
3361
3362
3363# node_(1) can also be created with the same configuration, or with a different
3364
3365# channel specified.
3366
3367# Uncomment below two lines will create node_(1) with a different channel.
3368
3369# $ns_ node-config \
3370
3371# -channel $chan_2_
3372
3373set node_(1) [$ns_ node]
3374
3375
3376
3377
3378
3379$node_(0) random-motion 0
3380
3381$node_(1) random-motion 0
3382
3383
3384
3385
3386
3387for {set i 0} {$i < $val(nn)} {incr i} {
3388
3389 $ns_ initial_node_pos $node_($i) 20
3390
3391}
3392
3393
3394
3395
3396
3397#
3398
3399# Provide initial (X,Y, for now Z=0) co-ordinates for mobilenodes
3400
3401#
3402
3403$node_(0) set X_ 5.0
3404
3405$node_(0) set Y_ 2.0
3406
3407$node_(0) set Z_ 0.0
3408
3409
3410
3411
3412
3413$node_(1) set X_ 8.0
3414
3415$node_(1) set Y_ 5.0
3416
3417$node_(1) set Z_ 0.0
3418
3419
3420
3421
3422
3423#
3424
3425# Now produce some simple node movements
3426
3427# Node_(1) starts to move towards node_(0)
3428
3429#
3430
3431$ns_ at 3.0 "$node_(1) setdest 50.0 40.0 25.0"
3432
3433$ns_ at 3.0 "$node_(0) setdest 48.0 38.0 5.0"
3434
3435
3436
3437
3438
3439# Node_(1) then starts to move away from node_(0)
3440
3441$ns_ at 20.0 "$node_(1) setdest 490.0 480.0 30.0"
3442
3443
3444
3445
3446
3447# Setup traffic flow between nodes
3448
3449# TCP connections between node_(0) and node_(1)
3450
3451
3452
3453
3454
3455set tcp [new Agent/TCP]
3456
3457$tcp set class_ 2
3458
3459set sink [new Agent/TCPSink]
3460
3461$ns_ attach-agent $node_(0) $tcp
3462
3463$ns_ attach-agent $node_(1) $sink
3464
3465$ns_ connect $tcp $sink
3466
3467set ftp [new Application/FTP]
3468
3469$ftp attach-agent $tcp
3470
3471$ns_ at 3.0 "$ftp start"
3472
3473
3474
3475
3476
3477#
3478
3479# Tell nodes when the simulation ends
3480
3481#
3482
3483for {set i 0} {$i < $val(nn) } {incr i} {
3484
3485 $ns_ at 30.0 "$node_($i) reset";
3486
3487}
3488
3489$ns_ at 30.0 "stop"
3490
3491$ns_ at 30.01 "puts \"NS EXITING...\" ; $ns_ halt"
3492
3493proc stop {} {
3494
3495 global ns_ tracefd
3496
3497 $ns_ flush-trace
3498
3499 close $tracefd
3500
3501}
3502
3503
3504
3505
3506
3507puts "Starting Simulation..."
3508
3509$ns_ run
3510
3511
3512
3513
3514
3515
3516
3517
3518
35191. Performance evaluation
3520
3521
3522
3523
3524
352512 a:
3526
3527
3528
3529
3530
3531#Link failure checking program
3532
3533#Create a simulator object
3534
3535set ns [new Simulator]
3536
3537
3538
3539
3540
3541#Tell the simulator to use dynamic routing
3542
3543$ns rtproto DV
3544
3545
3546
3547
3548
3549#Open the nam trace file
3550
3551set nf [open out.nam w]
3552
3553$ns namtrace-all $nf
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563#Define a 'finish' procedure
3564
3565proc finish {} {
3566
3567 global ns nf
3568
3569 $ns flush-trace
3570
3571 #Close the trace file
3572
3573 close $nf
3574
3575 #Execute nam on the trace file
3576
3577 exec nam out.nam &
3578
3579 exit 0
3580
3581}
3582
3583
3584
3585
3586
3587#Create seven nodes
3588
3589for {set i 0} {$i < 7} {incr i} {
3590
3591 set n($i) [$ns node]
3592
3593}
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603#Create links between the nodes
3604
3605for {set i 0} {$i < 7} {incr i} {
3606
3607 $ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail
3608
3609}
3610
3611
3612
3613
3614
3615#Create a UDP agent and attach it to node n(0)
3616
3617set udp0 [new Agent/UDP]
3618
3619$ns attach-agent $n(0) $udp0
3620
3621
3622
3623
3624
3625# Create a CBR traffic source and attach it to udp0
3626
3627set cbr0 [new Application/Traffic/CBR]
3628
3629$cbr0 set packetSize_ 500
3630
3631$cbr0 set interval_ 0.005
3632
3633$cbr0 attach-agent $udp0
3634
3635
3636
3637
3638
3639#Create a Null agent (a traffic sink) and attach it to node n(3)
3640
3641set null0 [new Agent/Null]
3642
3643$ns attach-agent $n(3) $null0
3644
3645
3646
3647
3648
3649#Connect the traffic source with the traffic sink
3650
3651$ns connect $udp0 $null0
3652
3653
3654
3655
3656
3657#Schedule events for the CBR agent and the network dynamics
3658
3659$ns at 0.5 "$cbr0 start"
3660
3661$ns rtmodel-at 1.0 down $n(1) $n(2)
3662
3663$ns rtmodel-at 2.0 up $n(1) $n(2)
3664
3665$ns at 4.5 "$cbr0 stop"
3666
3667#Call the finish procedure after 5 seconds of simulation time
3668
3669$ns at 5.0 "finish"
3670
3671
3672
3673
3674
3675#Run the simulation
3676
3677$ns run
3678
3679
3680
3681
3682
3683
3684
3685
3686
368712 b:
3688
3689
3690
3691
3692
3693#performance evaluation(bandwidth settings) using xgraph
3694
3695set ns [new Simulator]
3696
3697
3698
3699
3700
3701$ns color 0 green
3702
3703
3704
3705
3706
3707set f0 [open out0.tr w]
3708
3709set f1 [open out1.tr w]
3710
3711set f2 [open out2.tr w]
3712
3713set f3 [open out3.nam w]
3714
3715$ns namtrace-all $f3
3716
3717
3718
3719
3720
3721set n0 [$ns node]
3722
3723set n1 [$ns node]
3724
3725set n2 [$ns node]
3726
3727set n3 [$ns node]
3728
3729set n4 [$ns node]
3730
3731
3732
3733
3734
3735$ns duplex-link $n0 $n3 1Mb 100ms DropTail
3736
3737$ns duplex-link $n1 $n3 1Mb 100ms DropTail
3738
3739$ns duplex-link $n2 $n3 1Mb 100ms DropTail
3740
3741$ns duplex-link $n3 $n4 1Mb 100ms DropTail
3742
3743
3744
3745
3746
3747proc finish {} {
3748
3749 global ns f0 f1 f2 f3
3750
3751
3752
3753 $ns flush-trace
3754
3755 close $f0
3756
3757 close $f1
3758
3759 close $f2
3760
3761 close $f3
3762
3763 exec nam out3.nam &
3764
3765
3766
3767 exec xgraph out0.tr out1.tr out2.tr -geometry 800x400 &
3768
3769 exit 0
3770
3771}
3772
3773
3774
3775
3776
3777proc attach-expoo-traffic { node sink size burst idle rate } {
3778
3779 set ns [Simulator instance]
3780
3781 set source [new Agent/UDP]
3782
3783 $ns attach-agent $node $source
3784
3785
3786
3787
3788
3789 set traffic [new Application/Traffic/Exponential]
3790
3791 $traffic set packetSize_ $size
3792
3793 $traffic set burst_time_ $burst
3794
3795 $traffic set idle_time_ $idle
3796
3797 $traffic set rate_ $rate
3798
3799
3800
3801 $traffic attach-agent $source
3802
3803
3804
3805 $ns connect $source $sink
3806
3807 return $traffic
3808
3809}
3810
3811
3812
3813
3814
3815proc record {} {
3816
3817 global sink0 sink1 sink2 f0 f1 f2
3818
3819 set ns [Simulator instance]
3820
3821 set time 0.5
3822
3823
3824
3825
3826
3827 set bw0 [$sink0 set bytes_]
3828
3829 set bw1 [$sink1 set bytes_]
3830
3831 set bw2 [$sink2 set bytes_]
3832
3833
3834
3835 set now [$ns now]
3836
3837
3838
3839
3840
3841 puts $f0 "$now [expr $bw0/$time*8/1000000]"
3842
3843 puts $f1 "$now [expr $bw1/$time*8/1000000]"
3844
3845 puts $f2 "$now [expr $bw2/$time*8/1000000]"
3846
3847
3848
3849 $sink0 set bytes_ 0
3850
3851 $sink1 set bytes_ 0
3852
3853 $sink2 set bytes_ 0
3854
3855
3856
3857 $ns at [expr $now+$time] "record"
3858
3859}
3860
3861
3862
3863
3864
3865set sink0 [new Agent/LossMonitor]
3866
3867set sink1 [new Agent/LossMonitor]
3868
3869set sink2 [new Agent/LossMonitor]
3870
3871$ns attach-agent $n4 $sink0
3872
3873$ns attach-agent $n4 $sink1
3874
3875$ns attach-agent $n4 $sink2
3876
3877
3878
3879
3880
3881set source0 [attach-expoo-traffic $n0 $sink0 200 2s 1s 100k]
3882
3883set source1 [attach-expoo-traffic $n1 $sink1 200 2s 1s 200k]
3884
3885set source2 [attach-expoo-traffic $n2 $sink2 200 2s 1s 300k]
3886
3887
3888
3889
3890
3891$ns at 0.0 "record"
3892
3893
3894
3895
3896
3897$ns at 10.0 "$source0 start"
3898
3899$ns at 10.0 "$source1 start"
3900
3901$ns at 10.0 "$source2 start"
3902
3903
3904
3905
3906
3907$ns at 50.0 "$source0 stop"
3908
3909$ns at 50.0 "$source1 stop"
3910
3911$ns at 50.0 "$source2 stop"
3912
3913
3914
3915
3916
3917$ns at 60.0 "finish"
3918
3919
3920
3921
3922
3923
3924
3925$ns run
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
397313. Aim:
3974
3975 To study and understand the architecture and topology adopted in SASTRA infrastructure.
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985DESCRIPTION:
3986
3987Central computing facility controls the IT infrastructure of over 2500 computer with latest software and operating system.
3988
3989It provides 24 hours access to 370MBPS internet level that is network though the computer
3990
3991Novell window and unix servers super computer linux cluster and blade servers with quadcore.Intel dell xenon processor,2.66GHz 8GB PCZ-3500.FDB DDR 2Ram 3AS 10K
39923FFR HDD with Raid providers a powerfil server architecture at SASTRA.
3993
3994It user the router which provider comprehensive scalable and serve network routing which is connected to bsnl and reliance 20Mbps.
3995
3996The forti gate firewall connected two modem,forti analyzer,DITZ,Mz.The forti gate firewall has Z features.
3997
3998SASTRA has three layered switch which is connected to the buildings.This zone is called militarized zone.
3999
4000 The unmanaged switches has no user configuration capability.
4001
4002The forti range from 1-126 class A,class B from 128-191, class C from 192-223, class D from 224-239 , class E from 240-254. Private IP for each class
4003
4004
4005
4006
4007
4008ADDITIONAL EXPERIMENTS
4009
4010QUEUE MONITORING
4011
4012set ns [new Simulator]
4013
4014#
4015
4016# Create a simple six node topology:
4017
4018#
4019
4020# s1 s3
4021
4022# \ /
4023
4024# 10Mb,2ms \ 1.5Mb,20ms / 10Mb,4ms
4025
4026# r1 --------- r2
4027
4028# 10Mb,3ms / \ 10Mb,5ms
4029
4030# / \
4031
4032# s2 s4
4033
4034#
4035
4036set node_(s1) [$ns node]
4037
4038set node_(s2) [$ns node]
4039
4040set node_(r1) [$ns node]
4041
4042set node_(r2) [$ns node]
4043
4044set node_(s3) [$ns node]
4045
4046set node_(s4) [$ns node]
4047
4048
4049
4050
4051
4052$ns duplex-link $node_(s1) $node_(r1) 10Mb 2ms DropTail
4053
4054$ns duplex-link $node_(s2) $node_(r1) 10Mb 3ms DropTail
4055
4056$ns duplex-link $node_(r1) $node_(r2) 1.5Mb 20ms RED
4057
4058$ns queue-limit $node_(r1) $node_(r2) 25
4059
4060$ns queue-limit $node_(r2) $node_(r1) 25
4061
4062$ns duplex-link $node_(s3) $node_(r2) 10Mb 4ms DropTail
4063
4064$ns duplex-link $node_(s4) $node_(r2) 10Mb 5ms DropTail
4065
4066
4067
4068
4069
4070$ns duplex-link-op $node_(s1) $node_(r1) orient right-down
4071
4072$ns duplex-link-op $node_(s2) $node_(r1) orient right-up
4073
4074$ns duplex-link-op $node_(r1) $node_(r2) orient right
4075
4076$ns duplex-link-op $node_(r1) $node_(r2) queuePos 0
4077
4078$ns duplex-link-op $node_(r2) $node_(r1) queuePos 0
4079
4080$ns duplex-link-op $node_(s3) $node_(r2) orient left-down
4081
4082$ns duplex-link-op $node_(s4) $node_(r2) orient left-up
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092set tcp1 [$ns create-connection TCP/Reno $node_(s1) TCPSink $node_(s3) 0]
4093
4094$tcp1 set window_ 15
4095
4096set tcp2 [$ns create-connection TCP/Reno $node_(s2) TCPSink $node_(s3) 1]
4097
4098$tcp2 set window_ 15
4099
4100set ftp1 [$tcp1 attach-source FTP]
4101
4102set ftp2 [$tcp2 attach-source FTP]
4103
4104
4105
4106
4107
4108# Tracing a queue
4109
4110set redq [[$ns link $node_(r1) $node_(r2)] queue]
4111
4112set tchan_ [open all.q w]
4113
4114$redq trace curq_
4115
4116$redq trace ave_
4117
4118$redq attach $tchan_
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128$ns at 0.0 "$ftp1 start"
4129
4130$ns at 3.0 "$ftp2 start"
4131
4132$ns at 10 "finish"
4133
4134
4135
4136
4137
4138# Define 'finish' procedure (include post-simulation processes)
4139
4140proc finish {} {
4141
4142 global tchan_
4143
4144 set awkCode {
4145
4146 {
4147
4148 if ($1 == "Q" && NF>2) {
4149
4150 print $2, $3 >> "temp.q";
4151
4152 set end $2
4153
4154 }
4155
4156 else if ($1 == "a" && NF>2)
4157
4158 print $2, $3 >> "temp.a";
4159
4160 }
4161
4162 }
4163
4164 set f [open temp.queue w]
4165
4166 puts $f "TitleText: red"
4167
4168 puts $f "Device: Postscript"
4169
4170
4171
4172 if { [info exists tchan_] } {
4173
4174 close $tchan_
4175
4176 }
4177
4178 exec rm -f temp.q temp.a
4179
4180 exec touch temp.a temp.q
4181
4182
4183
4184 exec awk $awkCode all.q
4185
4186
4187
4188 puts $f \"queue
4189
4190 exec cat temp.q >@ $f
4191
4192 puts $f \n\"ave_queue
4193
4194 exec cat temp.a >@ $f
4195
4196 close $f
4197
4198 exec xgraph -bb -tk -x time -y queue temp.queue &
4199
4200 exit 0
4201
4202}
4203
4204
4205
4206
4207
4208$ns run
4209
4210MULTICASTING
4211
4212set ns [new Simulator]
4213
4214$ns multicast
4215
4216
4217
4218
4219
4220set f [open out.tr w]
4221
4222$ns trace-all $f
4223
4224$ns namtrace-all [open out.nam w]
4225
4226
4227
4228
4229
4230$ns color 1 red
4231
4232# prune/graft packets
4233
4234$ns color 30 purple
4235
4236$ns color 31 green
4237
4238
4239
4240
4241
4242set n0 [$ns node]
4243
4244set n1 [$ns node]
4245
4246set n2 [$ns node]
4247
4248set n3 [$ns node]
4249
4250
4251
4252
4253
4254# Use automatic layout
4255
4256$ns duplex-link $n0 $n1 1.5Mb 10ms DropTail
4257
4258$ns duplex-link $n1 $n2 1.5Mb 10ms DropTail
4259
4260$ns duplex-link $n1 $n3 1.5Mb 10ms DropTail
4261
4262
4263
4264
4265
4266$ns duplex-link-op $n0 $n1 orient right
4267
4268$ns duplex-link-op $n1 $n2 orient right-up
4269
4270$ns duplex-link-op $n1 $n3 orient right-down
4271
4272$ns duplex-link-op $n0 $n1 queuePos 0.5
4273
4274
4275
4276
4277
4278set mrthandle [$ns mrtproto DM {}]
4279
4280
4281
4282
4283
4284set cbr0 [new Application/Traffic/CBR]
4285
4286set udp0 [new Agent/UDP]
4287
4288$cbr0 attach-agent $udp0
4289
4290$ns attach-agent $n1 $udp0
4291
4292$udp0 set dst_ 0x8001
4293
4294
4295
4296
4297
4298set cbr1 [new Application/Traffic/CBR]
4299
4300set udp1 [new Agent/UDP]
4301
4302$cbr1 attach-agent $udp1
4303
4304$udp1 set dst_ 0x8002
4305
4306$udp1 set class_ 1
4307
4308$ns attach-agent $n3 $udp1
4309
4310
4311
4312
4313
4314set rcvr [new Agent/LossMonitor]
4315
4316#$ns attach-agent $n3 $rcvr
4317
4318$ns at 1.2 "$n2 join-group $rcvr 0x8002"
4319
4320$ns at 1.25 "$n2 leave-group $rcvr 0x8002"
4321
4322$ns at 1.3 "$n2 join-group $rcvr 0x8002"
4323
4324$ns at 1.35 "$n2 join-group $rcvr 0x8001"
4325
4326
4327
4328
4329
4330$ns at 1.0 "$cbr0 start"
4331
4332$ns at 1.1 "$cbr1 start"
4333
4334
4335
4336
4337
4338$ns at 2.0 "finish"
4339
4340
4341
4342
4343
4344proc finish {} {
4345
4346 global ns
4347
4348 $ns flush-trace
4349
4350
4351
4352
4353
4354 puts "running nam..."
4355
4356 exec nam out.nam &
4357
4358 exit 0
4359
4360}
4361
4362
4363
4364
4365
4366$ns run
4367
4368CREATING NODES WITH DIFFERENT COLORS AND DIFFERENT SHAPES TO DENOTE HUB, ROUTER AND SIMPLE NODE
4369
4370#Create a simulator object
4371
4372set ns [new Simulator]
4373
4374
4375
4376
4377
4378#Define different colors for data flows (for NAM)
4379
4380$ns color 1 Blue
4381
4382$ns color 2 Red
4383
4384$ns color 3 Black
4385
4386
4387
4388
4389
4390#Open the NAM trace file
4391
4392set nf [open out.nam w]
4393
4394$ns namtrace-all $nf
4395
4396
4397
4398
4399
4400#Define a 'finish' procedure
4401
4402proc finish {} {
4403
4404 global ns nf
4405
4406 $ns flush-trace
4407
4408 #Close the NAM trace file
4409
4410 close $nf
4411
4412 #Execute NAM on the trace file
4413
4414 exec nam out.nam &
4415
4416 exit 0
4417
4418}
4419
4420
4421
4422
4423
4424#Create four nodes
4425
4426set n0 [$ns node]
4427
4428set n1 [$ns node]
4429
4430set n2 [$ns node]
4431
4432set n3 [$ns node]
4433
4434
4435
4436
4437
4438$n3 shape "square"
4439
4440$n3 color "black"
4441
4442
4443
4444
4445
4446$n0 shape "square"
4447
4448$n0 color "blue"
4449
4450
4451
4452
4453
4454#Create links between the nodes
4455
4456$ns duplex-link $n0 $n2 2Mb 10ms DropTail
4457
4458$ns duplex-link $n1 $n2 2Mb 10ms RED
4459
4460$ns duplex-link $n2 $n3 1.7Mb 20ms RED
4461
4462
4463
4464
4465
4466$ns duplex-link-op $n0 $n2 color "blue"
4467
4468$ns duplex-link-op $n0 $n2 label "cs-study"
4469
4470
4471
4472
4473
4474$ns duplex-link-op $n1 $n2 color "green"
4475
4476$ns duplex-link-op $n1 $n2 label "easylearning"
4477
4478
4479
4480
4481
4482#Set Queue Size of link (n2-n3) to 10
4483
4484$ns queue-limit $n2 $n3 10
4485
4486
4487
4488
4489
4490#Give node position (for NAM)
4491
4492$ns duplex-link-op $n0 $n2 orient right-down
4493
4494$ns duplex-link-op $n1 $n2 orient right-up
4495
4496$ns duplex-link-op $n2 $n3 orient right
4497
4498
4499
4500
4501
4502#Monitor the queue for link (n2-n3). (for NAM)
4503
4504$ns duplex-link-op $n2 $n3 queuePos 0.5
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514#Setup a TCP connection
4515
4516set tcp [new Agent/TCP]
4517
4518$tcp set class_ 3
4519
4520$ns attach-agent $n0 $tcp
4521
4522set sink [new Agent/TCPSink]
4523
4524$ns attach-agent $n3 $sink
4525
4526$ns connect $tcp $sink
4527
4528$tcp set fid_ 1
4529
4530
4531
4532
4533
4534#Setup a FTP over TCP connection
4535
4536set ftp [new Application/FTP]
4537
4538$ftp attach-agent $tcp
4539
4540$ftp set type_ FTP
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550#Setup a UDP connection
4551
4552set udp [new Agent/UDP]
4553
4554$ns attach-agent $n1 $udp
4555
4556set null [new Agent/Null]
4557
4558$ns attach-agent $n3 $null
4559
4560$ns connect $udp $null
4561
4562$udp set fid_ 2
4563
4564
4565
4566
4567
4568#Setup a CBR over UDP connection
4569
4570set cbr [new Application/Traffic/CBR]
4571
4572$cbr attach-agent $udp
4573
4574$cbr set type_ CBR
4575
4576$cbr set packet_size_ 1000
4577
4578$cbr set rate_ 1mb
4579
4580$cbr set random_ false
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590#Schedule events for the CBR and FTP agents
4591
4592$ns at 0.1 "$cbr start"
4593
4594$ns at 1.0 "$ftp start"
4595
4596$ns at 4.0 "$ftp stop"
4597
4598$ns at 4.5 "$cbr stop"
4599
4600
4601
4602
4603
4604#Detach tcp and sink agents (not really necessary)
4605
4606$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"
4607
4608
4609
4610
4611
4612#Call the finish procedure after 5 seconds of simulation time
4613
4614$ns at 5.0 "finish"
4615
4616
4617
4618
4619
4620#Print CBR packet size and interval
4621
4622puts "CBR packet size = [$cbr set packet_size_]"
4623
4624puts "CBR interval = [$cbr set interval_]"
4625
4626
4627
4628
4629
4630#Run the simulation
4631
4632$ns run