Advertisement
d1i2p3a4k5

cn

Oct 27th, 2015
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.64 KB | None | 0 0
  1. BIT STUFFING
  2.  
  3.  
  4. import java.util.*;
  5.  
  6. public class BitStuff {
  7.  
  8. static Scanner sc = new Scanner (System.in);
  9. static int i,j;
  10.  
  11. public static void main(String[] args) {
  12. System.out.print("Enter no. of bits in data word ");
  13. int n = sc.nextInt();
  14. System.out.println("Enter data word");
  15. int a[] = new int[n];
  16. for (i=0; i<n; i++)
  17. a[i] = sc.nextInt();
  18. int c=0; //counter
  19. int b[] = new int[10*n];
  20. System.out.println("Processing");
  21. for (i=0,j=0; i<a.length; i++,j++) {
  22. b[j] = a[i];
  23. if (a[i] == 1)
  24. c++;
  25. else c=0;
  26. if (c == 5)
  27. //something to be done
  28. b[++j] = 0;
  29. }
  30.  
  31. System.out.println("Bit Stuffed Data is");
  32. System.out.print("01111110| "); //SOF
  33. for (i=0; i<j; i++)
  34. System.out.print(b[i]+" ");
  35. System.out.print("|01111110"); //EOF
  36. }
  37.  
  38. }
  39.  
  40.  
  41.  
  42. CHECKSUM
  43.  
  44.  
  45.  
  46. import java.util.*;
  47. import java.lang.Math;
  48.  
  49. public class CheckSum {
  50.  
  51. static
  52. Scanner sc= new Scanner(System.in);
  53.  
  54. public static void main(String[] args) {
  55. Random rnd = new Random();
  56. int dw[],bi[],di[],a[],b[]; //array
  57. int i,k=0,l=0,temp; //variable
  58. int ebn; //error bit
  59. int c1=0,c2=20,c3=21; //counter
  60. int sum,sum1,d1,d2,d3,d4,d5,d6,d8; //storing decimal values
  61. double nd8=0; //store decimal value
  62. dw = new int[25];
  63. bi = new int[25];
  64. di = new int[25];
  65. a = new int[25];
  66. b = new int[25];
  67. System.out.println("Sender Side:");
  68. //accepting data
  69.  
  70. System.out.print("Enter 20 Bit (DW) Data: ");
  71. for(i=0; i<20; i++)
  72. dw[i] = sc.nextInt();
  73.  
  74. //displaying data
  75. System.out.print("Your 20 Bit Data Is : ");
  76. for(i=0;i<=19;i++)
  77. System.out.print(+dw[i]+" ");
  78.  
  79. System.out.println();
  80. System.out.println("Applying Checksum Method...");
  81. //binary to decimal conversion of 20 bit data
  82. d1=(dw[0]*8)+(dw[1]*4)+(dw[2]*2)+(dw[3]*1);
  83. d2=(dw[4]*8)+(dw[5]*4)+(dw[6]*2)+(dw[7]*1);
  84. d3=(dw[8]*8)+(dw[9]*4)+(dw[10]*2)+(dw[11]*1);
  85. d4=(dw[12]*8)+(dw[13]*4)+(dw[14]*2)+(dw[15]*1);
  86. d5=(dw[16]*8)+(dw[17]*4)+(dw[18]*2)+(dw[19]*1);
  87. sum=d1+d2+d3+d4+d5;
  88. // decimal to binary conversion of 20 bit sum
  89. while(sum> 0) {
  90. bi[k++]=sum%2;
  91. sum=sum/2;
  92. }
  93.  
  94. //spliting array bits
  95. for(i=3;i>=0;i--)
  96. a[i]=bi[i];
  97.  
  98.  
  99. for(i=k-1;i>=4;i--) {
  100. b[i]=bi[i];
  101. c1=c1+1;
  102. }
  103.  
  104. //converting splitted array from data to decimal
  105. d4=(a[3]*8)+(a[2]*4)+(a[1]*2)+(a[0]*1);
  106. c1=c1-1;
  107. for(i=k-1;i>=4 &&c1>=0;i--) {
  108. nd8=nd8+(b[i]*Math.pow(2,c1));
  109. c1=c1-1;
  110. }
  111.  
  112. d8=(int)nd8;
  113. //rounding off
  114. sum1=d4+d8;
  115. temp=sum1;
  116. //convert rounded data from decimal to binary
  117. while(sum1> 0) {
  118. di[l++]=sum1%2;
  119. sum1=sum1/2;
  120. }
  121.  
  122. if(temp>7)
  123. {
  124. for(i=l-1;i>=0;i--) {
  125. dw[c2]=di[i];
  126. c2=c2+1;
  127. }
  128. }
  129.  
  130. if(temp<=7) {
  131. dw[20]=0;
  132. for(i=l-1;i>=0;i--) {
  133. dw[c3]=di[i];
  134. c3=c3+1;
  135. }
  136. }
  137.  
  138. //1s complement
  139. if(dw[20]==1)
  140. dw[20]=0;
  141. else dw[20]=1;
  142.  
  143. if(dw[21]==1)
  144. dw[21]=0;
  145. else dw[21]=1;
  146.  
  147. if(dw[22]==1)
  148. dw[22]=0;
  149. else dw[22]=1;
  150.  
  151. if(dw[23]==1)
  152. dw[23]=0;
  153. else dw[23]=1;
  154.  
  155. System.out.println("Data transmitted Receiver");
  156.  
  157. //introducing error
  158.  
  159. ebn = (rnd.nextInt(250))/5;
  160. if(ebn<=25)
  161. if(dw[ebn]==1)
  162. dw[ebn]=0;
  163. else dw[ebn]=1;
  164.  
  165. //receiver side
  166. System.out.println();
  167. System.out.println("Receiver Side");
  168. System.out.println("Data Received Is : ");
  169.  
  170. for(i=0;i<=23;i++)
  171. System.out.print(dw[i]+" ");
  172.  
  173.  
  174. //converting 25 bit data from binary to decimal conversion
  175. d1 = (dw[0]*8)+(dw[1]*4)+(dw[2]*2)+(dw[3]*1);
  176. d2 = (dw[4]*8)+(dw[5]*4)+(dw[6]*2)+(dw[7]*1);
  177. d3 = (dw[8]*8)+(dw[9]*4)+(dw[10]*2)+(dw[11]*1);
  178. d4 = (dw[12]*8)+(dw[13]*4)+(dw[14]*2)+(dw[15]*1);
  179. d5 = (dw[16]*8)+(dw[17]*4)+(dw[18]*2)+(dw[19]*1);
  180. d6 = (dw[20]*8)+(dw[21]*4)+(dw[22]*2)+(dw[23]*1);
  181. sum = d1+d2+d3+d4+d5+d6;
  182.  
  183. //converting 25 bit sum from decimal to binary conversion
  184. k=0; //resetting bits
  185. l=0; //resetting bits
  186. while(sum> 0) {
  187. bi[k++] = sum%2;
  188. sum = sum/2;
  189. }
  190.  
  191. //spliting array bits
  192. for(i=3;i>=0;i--)
  193. a[i]=bi[i];
  194.  
  195. c1=0;//resetting counter
  196. nd8=0;
  197. sum1=0;
  198. for(i=k-1;i>=4;i--) {
  199. b[i]=bi[i];
  200. c1=c1+1;
  201. }
  202.  
  203. //converting splitted array from data to decimal
  204. d4=(a[3]*8)+(a[2]*4)+(a[1]*2)+(a[0]*1);
  205. c1=c1-1;
  206.  
  207. for (i=k-1; i>=4 && c1>=0; i--) {
  208. nd8 = nd8+(b[i]*Math.pow(2,c1));
  209. c1 = c1-1;
  210. }
  211.  
  212. d8 = (int)nd8;
  213.  
  214. sum1 = d4+d8;
  215.  
  216. while (sum1> 0) {
  217.  
  218. di[l++] = sum1%2;
  219. sum1 = sum1/2;
  220. }
  221.  
  222. //checking error
  223. System.out.println();
  224. System.out.println("Verifying Using Checksum Method...");
  225. System.out.println("Checking For Error: ");
  226.  
  227. if (di[3]==1 &&di[2]==1 &&di[1]==1 &&di[0]==1)
  228. System.out.println("NO ERROR");
  229. else System.out.println("ERROR");
  230.  
  231. }
  232. }
  233.  
  234.  
  235.  
  236.  
  237.  
  238. CLIENT
  239.  
  240.  
  241.  
  242. import java.io.*;
  243. import java.net.*;
  244. public class Client {
  245. public static void main(String[] args) {
  246. // declaration section:
  247. // smtpClient: our client socket
  248. // os: output stream
  249. // is: input stream
  250. Socket smtpSocket = null;
  251. DataOutputStream os = null;
  252. DataInputStream is = null;
  253. // Initialization section:
  254. // Try to open a socket on port 25
  255. // Try to open input and output streams
  256. try {
  257. smtpSocket = new Socket("hostname", 25);
  258. os = new DataOutputStream(smtpSocket.getOutputStream());
  259. is = new DataInputStream(smtpSocket.getInputStream());
  260. } catch (UnknownHostException e) {
  261. System.err.println("Don't know about host: hostname");
  262. } catch (IOException e) {
  263. System.err.println("Couldn't get I/O for the connection to: hostname");
  264. }
  265. // If everything has been initialized then we want to write some data
  266. // to the socket we have opened a connection to on port 25
  267. if (smtpSocket != null && os != null && is != null) {
  268. try {
  269. // The capital string before each colon has a special meaning to SMTP
  270. // you may want to read the SMTP specification, RFC1822/3
  271. os.writeBytes("HELO\n");
  272. os.writeBytes("MAIL From: k3is@fundy.csd.unbsj.ca\n");
  273. os.writeBytes("RCPT To: k3is@fundy.csd.unbsj.ca\n");
  274. os.writeBytes("DATA\n");
  275. os.writeBytes("From: k3is@fundy.csd.unbsj.ca\n");
  276. os.writeBytes("Subject: testing\n");
  277. os.writeBytes("Hi there\n"); // message body
  278. os.writeBytes("\n.\n");
  279. os.writeBytes("QUIT");
  280. // keep on reading from/to the socket till we receive the "Ok" from SMTP,
  281. // once we received that then we want to break.
  282. String responseLine;
  283. while ((responseLine = is.readLine()) != null) {
  284. System.out.println("Server: " + responseLine);
  285. if (responseLine.indexOf("Ok") != -1) {
  286. break;
  287. }
  288. }
  289. // clean up:
  290. // close the output stream
  291. // close the input stream
  292. // close the socket
  293. os.close();
  294. is.close();
  295. smtpSocket.close();
  296. } catch (UnknownHostException e) {
  297. System.err.println("Trying to connect to unknown host: " + e);
  298. } catch (IOException e) {
  299. System.err.println("IOException: " + e);
  300. }
  301. }
  302. }
  303. }
  304.  
  305.  
  306. CRC
  307.  
  308.  
  309. import java.util.*;
  310.  
  311. class Crc {
  312.  
  313. static Scanner sc = new Scanner(System.in);
  314.  
  315. public static void main(String args[]) {
  316. int m,g[],n,d[],z[],r[],msb,i,j,k;
  317. System.out.print("Enter no. of bits in data word : ");
  318. n=sc.nextInt();
  319. System.out.print("Enter no. of bits in generator function : ");
  320. m=sc.nextInt();
  321. d=new int[n+m];
  322. g=new int[m];
  323. System.out.print("Enter data word : ");
  324. for(i=0;i<n;i++)
  325. d[i]=sc.nextInt();
  326. System.out.print("Enter bits in the generator function: ");
  327.  
  328. for(j=0;j<m;j++)
  329. g[j]=sc.nextInt();
  330.  
  331. for(i=0;i<m-1;i++)
  332. d[n+i]=0;
  333. r=new int[m+n];
  334.  
  335. for(i=0;i<m;i++)
  336. r[i]=d[i];
  337. z=new int[m];
  338.  
  339. for(i=0;i<m;i++)
  340. z[i]=0;
  341.  
  342. for(i=0;i<n;i++)
  343. {
  344. k=0;
  345. msb = r[i];
  346. for (j=i;j<m+i;j++)
  347. {
  348. if (msb==0)
  349. r[j] = xor(r[j],z[k]);
  350. else r[j]=xor(r[j],g[k]);
  351. k++;
  352. }
  353. r[m+i] = d[m+i];
  354. }
  355.  
  356. System.out.print("\nThe bits in the remainder are : ");
  357.  
  358. for(i=n;i<n+m-1;i++)
  359. {
  360. d[i] = r[i];
  361. System.out.print(d[i]+"");
  362. }
  363.  
  364. System.out.print("\nThe Crc code is : ");
  365.  
  366. for(i=0;i<n+m-1;i++)
  367. System.out.print(d[i]);
  368. }
  369.  
  370. public static int xor(int x,int y)
  371. {
  372. if (x==y)
  373. return(0);
  374. else return(1);
  375. }
  376. }
  377.  
  378.  
  379. DIJKSTRAS
  380.  
  381.  
  382.  
  383. import java.util.*;
  384.  
  385. public class DijkstraAlgorithm {
  386.  
  387. private int distances[];
  388.  
  389. private Set<Integer> settled;
  390.  
  391. private Set<Integer> unsettled;
  392.  
  393. private int number_of_nodes;
  394.  
  395. private int adjacencyMatrix[][];
  396.  
  397. public DijkstraAlgorithm(int number_of_nodes) {
  398. this.number_of_nodes = number_of_nodes;
  399. distances = new int[number_of_nodes + 1];
  400. settled = new HashSet<Integer>();
  401. unsettled = new HashSet<Integer>();
  402. adjacencyMatrix = new int[number_of_nodes + 1][number_of_nodes + 1];
  403. }
  404.  
  405. public void DijkstraAlgorithm(int adjacency_matrix[][], int source) {
  406. int evaluationNode;
  407. for (int i = 1; i <= number_of_nodes; i++)
  408. for (int j = 1; j <= number_of_nodes; j++)
  409. adjacencyMatrix[i][j] = adjacency_matrix[i][j];
  410.  
  411. for (int i = 1; i <= number_of_nodes; i++)
  412. distances[i] = Integer.MAX_VALUE;
  413.  
  414. unsettled.add(source);
  415. distances[source] = 0;
  416.  
  417. while (!unsettled.isEmpty()) {
  418. evaluationNode = getNodeWithMinimumDistanceFromUnsettled();
  419. unsettled.remove(evaluationNode);
  420. settled.add(evaluationNode);
  421. evaluateNeighbours(evaluationNode);
  422. }
  423. }
  424.  
  425. private int getNodeWithMinimumDistanceFromUnsettled() {
  426. int min ;
  427. int node = 0;
  428. Iterator<Integer> iterator = unsettled.iterator();
  429. node = iterator.next();
  430. min = distances[node];
  431. for (int i = 1; i <= distances.length; i++) {
  432. if (unsettled.contains(i)) {
  433. if (distances[i] <= min) {
  434. min = distances[i];
  435. node = i;
  436. }
  437. }
  438. }
  439. return node;
  440. }
  441.  
  442. private void evaluateNeighbours(int evaluationNode)
  443. {
  444. int edgeDistance = -1;
  445. int newDistance = -1;
  446. for (int destinationNode = 1; destinationNode <= number_of_nodes; destinationNode++)
  447. {
  448. if (!settled.contains(destinationNode))
  449. {
  450. if (adjacencyMatrix[evaluationNode][destinationNode] != Integer.MAX_VALUE)
  451. {
  452. edgeDistance = adjacencyMatrix[evaluationNode][destinationNode];
  453. newDistance = distances[evaluationNode] + edgeDistance;
  454. if (newDistance < distances[destinationNode])
  455. distances[destinationNode] = newDistance;
  456. unsettled.add(destinationNode);
  457. }
  458. }
  459. }
  460. }
  461.  
  462. public static void main(String... arg)
  463. {
  464. Scanner scan = new Scanner(System.in);
  465. int adjacency_matrix[][];
  466. int number_of_vertices;
  467. int source = 0;
  468. try
  469. {
  470. System.out.println("Enter the number of vertices");
  471. number_of_vertices = scan.nextInt();
  472. adjacency_matrix = new int[number_of_vertices + 1][number_of_vertices + 1];
  473. System.out.println("Enter the Weighted Matrix for the graph");
  474. for (int i = 1; i <= number_of_vertices; i++)
  475. {
  476. for (int j = 1; j <= number_of_vertices; j++)
  477. {
  478. adjacency_matrix[i][j] = scan.nextInt();
  479. if (i == j)
  480. {
  481. adjacency_matrix[i][j] = 0;
  482. continue;
  483. }
  484.  
  485. if (adjacency_matrix[i][j] == 0)
  486. adjacency_matrix[i][j] = Integer.MAX_VALUE;
  487. }
  488. }
  489.  
  490. System.out.println("Enter the source ");
  491. source = scan.nextInt();
  492. DijkstraAlgorithm dijkstrasAlgorithm = new DijkstraAlgorithm(number_of_vertices);
  493. dijkstrasAlgorithm.DijkstraAlgorithm(adjacency_matrix, source);
  494. System.out.println("The Shorted Path to all nodes are ");
  495.  
  496. for (int i = 1; i <= dijkstrasAlgorithm.distances.length - 1; i++)
  497. {
  498. System.out.println(source + " to " + i + " is "+ dijkstrasAlgorithm.distances[i]);
  499. }
  500. }
  501.  
  502. catch (InputMismatchException inputMismatch)
  503. {
  504. System.out.println("Wrong Input Format");
  505. }
  506.  
  507. scan.close();
  508. }
  509. }
  510.  
  511.  
  512. SOCKET Client
  513.  
  514.  
  515.  
  516. import java.net.*;
  517. import java.io.*;
  518.  
  519. public class GreetingClient
  520. {
  521. public static void main(String [] args)
  522. {
  523. String serverName = args[0];
  524. int port = Integer.parseInt(args[1]);
  525. try
  526. {
  527. System.out.println("Connecting to " + serverName +
  528. " on port " + port);
  529. Socket client = new Socket(serverName, port);
  530. System.out.println("Just connected to "
  531. + client.getRemoteSocketAddress());
  532. OutputStream outToServer = client.getOutputStream();
  533. DataOutputStream out = new DataOutputStream(outToServer);
  534. out.writeUTF("Hello from "
  535. + client.getLocalSocketAddress());
  536. InputStream inFromServer = client.getInputStream();
  537. DataInputStream in =
  538. new DataInputStream(inFromServer);
  539. System.out.println("Server says " + in.readUTF());
  540. client.close();
  541. }catch(IOException e)
  542. {
  543. e.printStackTrace();
  544. }
  545. }
  546. }
  547.  
  548.  
  549.  
  550.  
  551. SOCKET Server
  552.  
  553.  
  554.  
  555. import java.net.*;
  556. import java.io.*;
  557.  
  558. public class GreetingServer extends Thread
  559. {
  560. private ServerSocket serverSocket;
  561.  
  562. public GreetingServer(int port) throws IOException
  563. {
  564. serverSocket = new ServerSocket(port);
  565. serverSocket.setSoTimeout(10000);
  566. }
  567.  
  568. public void run()
  569. {
  570. while(true)
  571. {
  572. try
  573. {
  574. System.out.println("Waiting for client on port " +
  575. serverSocket.getLocalPort() + "...");
  576. Socket server = serverSocket.accept();
  577. System.out.println("Just connected to "
  578. + server.getRemoteSocketAddress());
  579. DataInputStream in =
  580. new DataInputStream(server.getInputStream());
  581. System.out.println(in.readUTF());
  582. DataOutputStream out =
  583. new DataOutputStream(server.getOutputStream());
  584. out.writeUTF("Thank you for connecting to "
  585. + server.getLocalSocketAddress() + "\nGoodbye!");
  586. server.close();
  587. }catch(SocketTimeoutException s)
  588. {
  589. System.out.println("Socket timed out!");
  590. break;
  591. }catch(IOException e)
  592. {
  593. e.printStackTrace();
  594. break;
  595. }
  596. }
  597. }
  598. public static void main(String [] args)
  599. {
  600. int port = Integer.parseInt(args[0]);
  601. try
  602. {
  603. Thread t = new GreetingServer(port);
  604. t.start();
  605. }catch(IOException e)
  606. {
  607. e.printStackTrace();
  608. }
  609. }
  610. }
  611.  
  612.  
  613.  
  614. HAMMING
  615.  
  616.  
  617.  
  618. import java.util.*;
  619.  
  620.  
  621. public class Hamming
  622. {
  623. static Scanner sc = new Scanner(System.in);
  624.  
  625. static int set_parity_bit(int a[])
  626. {
  627. int count=0;
  628. int i;
  629. int l=a.length;
  630. for( i=0;i<l;i++)
  631. if(a[i]==1)
  632. count++;
  633. if((count%2)==0)
  634. return 0;
  635. else
  636. return 1;
  637. }
  638.  
  639. public static void main(String args[])
  640. {
  641. System.out.println("This is hamming code error detection and correction using EVEN parity");
  642. System.out.println();
  643. System.out.println("Enter 4 data bits.D4 D3 D2 D1");
  644. int n=4;
  645. int i;
  646. int d[] = new int[4];
  647. for(i=n-1;i>=0;i--)
  648. {
  649. System.out.println("Enter the value of D"+(i+1));
  650. d[i]=sc.nextInt();
  651. }
  652. int k=0;
  653. while(Math.pow(2,k)<(n+k+1))
  654. k++;
  655. System.out.println();
  656. System.out.println(k+" parity bits are required for the transmission of data bits.");
  657. //int p[]=new int[k];
  658. int h[]=new int[n+k+1];
  659. for(i=0;i<7;i++)
  660. h[i]=-1;
  661. int count=0;
  662. int c=2;
  663. while(count<4)
  664. {
  665. c++;
  666. if(c==4)
  667. continue;
  668. h[c]=d[count];
  669. count++;
  670. }
  671. int p1[]={h[1],h[3],h[5],h[7]};
  672. int p2[]={h[2],h[3],h[6],h[7]};
  673. int p3[]={h[4],h[5],h[6],h[7]};
  674. int parity[]=new int[3];
  675. parity[0]=set_parity_bit(p1);
  676. parity[1]=set_parity_bit(p2);
  677. parity[2]=set_parity_bit(p3);
  678. h[1]=parity[0];
  679. h[2]=parity[1];
  680. h[4]=parity[2];
  681. System.out.println("\nSENDER:");
  682. System.out.print("\nThe data bits entered are: ");
  683. for(i=3;i>=0;i--)
  684. System.out.print(d[i]+" ");
  685. System.out.println("\nThe Parity bits are: ");
  686. for( i=2;i>=0;i--)
  687. System.out.println("Value of P"+(i+1)+" is "+parity[i]+" ");
  688. System.out.print("\nThe Hamming code is as follows :-\nD4 D3 D2 P3 D1 P2 P1 :");
  689. for( i=(n+k);i>0;i--)
  690. System.out.print(h[i]+" ");
  691. System.out.println();
  692. System.out.println("\nEnter the hamming code with error at any position of your choice.\nNOTE: ENTER A SPACE AFTER EVERY BIT POSITION.\nError should be present only at one bit position");
  693. for(i=7;i>0;i--)
  694. h[i] = sc.nextInt();
  695. int p4[] = {h[1],h[3],h[5],h[7]};
  696. int p5[] = {h[2],h[3],h[6],h[7]};
  697. int p6[] = {h[4],h[5],h[6],h[7]};
  698. parity[0] = set_parity_bit(p4);
  699. parity[1] = set_parity_bit(p5);
  700. parity[2] = set_parity_bit(p6);
  701. int position=(int)(parity[2]*Math.pow(2,2)+parity[1]*Math.pow(2,1)+parity[0]*Math.pow(2,0));
  702. System.out.println("\nRECEIVER:");
  703. System.out.println("Error is detected at position "+position+" at the receiving end.");
  704. System.out.println("Correcting the error.... ");
  705. if(h[position]==1)
  706. h[position]=0;
  707. else h[position]=1;
  708. System.out.print("The correct code is ");
  709. for(i=7;i>0;i--)
  710. System.out.print(h[i]+" ");
  711. }
  712. }
  713.  
  714.  
  715.  
  716. UDPCLIENT
  717.  
  718.  
  719. import java.io.*;
  720. import java.net.*;
  721.  
  722. class UDPClient
  723. {
  724. public static void main(String args[]) throws Exception
  725. {
  726. BufferedReader inFromUser =
  727. new BufferedReader(new InputStreamReader(System.in));
  728. DatagramSocket clientSocket = new DatagramSocket();
  729. InetAddress IPAddress = InetAddress.getByName("localhost");
  730. byte[] sendData = new byte[1024];
  731. byte[] receiveData = new byte[1024];
  732. String sentence = inFromUser.readLine();
  733. sendData = sentence.getBytes();
  734. DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 1234);
  735. clientSocket.send(sendPacket);
  736. DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
  737. clientSocket.receive(receivePacket);
  738. String modifiedSentence = new String(receivePacket.getData());
  739. System.out.println("FROM SERVER:" + modifiedSentence);
  740. clientSocket.close();
  741. }
  742.  
  743.  
  744.  
  745. UDP SERVER
  746.  
  747.  
  748.  
  749. import java.io.*;
  750. import java.net.*;
  751. class UDPServer
  752. {
  753. public static void main(String args[]) throws Exception
  754. {
  755. DatagramSocket serverSocket = new DatagramSocket();
  756. byte[] receiveData = new byte[1024];
  757. byte[] sendData = new byte[1024];
  758. while(true)
  759. {
  760. DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
  761. serverSocket.receive(receivePacket);
  762. String sentence = new String( receivePacket.getData());
  763. System.out.println("RECEIVED: " + sentence);
  764. InetAddress IPAddress = receivePacket.getAddress();
  765. int port = receivePacket.getPort();
  766. String capitalizedSentence = sentence.toUpperCase();
  767. sendData = capitalizedSentence.getBytes();
  768. DatagramPacket sendPacket =
  769. new DatagramPacket(sendData, sendData.length, IPAddress, port);
  770. serverSocket.send(sendPacket);
  771. }
  772. }
  773. } }
  774.  
  775.  
  776.  
  777.  
  778. NS2
  779.  
  780.  
  781.  
  782. set ns [new Simulator]
  783.  
  784. #Define different colors for data flows (for NAM)
  785. $ns color 1 blue
  786. $ns color 2 Red
  787.  
  788. #Open the NAM trace file
  789. set nf [open out.nam w]
  790. $ns namtrace-all $nf
  791.  
  792. #Define a 'finish' procedure
  793. proc finish {} {
  794. global ns nf
  795. $ns flush-trace
  796. #Close the NAM trace file
  797. close $nf
  798. #Execute NAM on the trace file
  799. exec nam out.nam &
  800. exit 0
  801. }
  802.  
  803. #Create four nodes
  804. set n0 [$ns node]
  805. set n1 [$ns node]
  806. set n2 [$ns node]
  807. set n3 [$ns node]
  808. set n4 [$ns node]
  809. set n5 [$ns node]
  810. set n6 [$ns node]
  811.  
  812.  
  813. #Create links between the nodes
  814. $ns duplex-link $n0 $n1 50Mb 10ms DropTail
  815. $ns duplex-link $n0 $n2 100Mb 10ms DropTail
  816. $ns duplex-link $n1 $n5 50Mb 10ms DropTail
  817. $ns duplex-link $n1 $n3 50Mb 10ms DropTail
  818. $ns duplex-link $n2 $n3 100Mb 10ms DropTail
  819. $ns duplex-link $n5 $n4 50Mb 10ms DropTail
  820. $ns duplex-link $n3 $n4 100Mb 10ms DropTail
  821. $ns duplex-link $n4 $n6 50Mb 10ms DropTail
  822.  
  823.  
  824. #Set Queue Size of link (n2-n3) to 10
  825. $ns queue-limit $n1 $n5 2
  826. $ns queue-limit $n1 $n3 2
  827. $ns queue-limit $n3 $n4 2
  828. $ns queue-limit $n4 $n6 2
  829.  
  830. #Give node position (for NAM)
  831. $ns duplex-link-op $n0 $n1 orient right-up
  832. $ns duplex-link-op $n0 $n2 orient right-down
  833. $ns duplex-link-op $n1 $n5 orient right
  834. $ns duplex-link-op $n1 $n3 orient right-down
  835. $ns duplex-link-op $n2 $n3 orient right
  836. $ns duplex-link-op $n5 $n4 orient right-down
  837. $ns duplex-link-op $n3 $n4 orient right-up
  838. $ns duplex-link-op $n1 $n5 orient right
  839. $ns duplex-link-op $n4 $n6 orient right
  840.  
  841.  
  842.  
  843. #Monitor the queue for link (n2-n3). (for NA)
  844. $ns duplex-link-op $n1 $n5 queuePos 1
  845. $ns duplex-link-op $n3 $n4 queuePos 1
  846. $ns duplex-link-op $n4 $n6 queuePos 1
  847.  
  848.  
  849. #Setup a TCP connection
  850. set tcp [new Agent/TCP]
  851. $tcp set class_ 2
  852. $ns attach-agent $n0 $tcp
  853. set sink [new Agent/TCPSink]
  854. $ns attach-agent $n6 $sink
  855. $ns connect $tcp $sink
  856. $tcp set fid_ 1
  857.  
  858. #Setup a FTP over TCP connection
  859. set ftp [new Application/FTP]
  860. $ftp attach-agent $tcp
  861. $ftp set type_ FTP
  862.  
  863.  
  864. #Setup a UDP connection
  865. set udp [new Agent/UDP]
  866. $ns attach-agent $n0 $udp
  867. set null [new Agent/Null]
  868. $ns attach-agent $n6 $null
  869. $ns connect $udp $null
  870. $udp set fid_ 2
  871.  
  872. #Setup a CBR over UDP connection
  873. set cbr [new Application/Traffic/CBR]
  874. $cbr attach-agent $udp
  875. $cbr set type_ CBR
  876. $cbr set packet_size_ 10000
  877. $cbr set rate_ 1mb
  878. $cbr set random_ false
  879.  
  880.  
  881. #Schedule events for the CBR and FTP agents
  882. $ns at 0.1 "$ftp start"
  883. $ns at 1.0 "$cbr start"
  884. $ns at 3.5 "$cbr stop"
  885. $ns at 4.0 "$ftp stop"
  886.  
  887.  
  888.  
  889. #Call the finish procedure after 5 seconds of simulation time
  890. $ns at 4.7 "finish"
  891.  
  892. #Print CBR packet size and interval
  893. puts "CBR packet size = [$cbr set packet_size_]"
  894. puts "CBR interval = [$cbr set interval_]"
  895.  
  896. #Run the simulation
  897. $ns run
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement