Advertisement
Eternoseeker

Clientside selective repeat

Apr 17th, 2023 (edited)
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | Source Code | 0 0
  1. import java.lang.System;
  2. import java.net.*;
  3. import java.io.*;
  4. import java.util.Random;
  5.  
  6. public class clients {
  7.     static Socket connection;
  8.  
  9.     public static void main(String a[]) throws SocketException {
  10.         try {
  11.             int v[] = new int[10];
  12.             int n = 0;
  13.             Random rands = new Random();
  14.             int rand = 0;
  15.              
  16.             InetAddress addr = InetAddress.getByName("Localhost");
  17.             System.out.println(addr);
  18.             connection = new Socket(addr, 8011);
  19.             DataOutputStream out = new DataOutputStream(
  20.                     connection.getOutputStream());
  21.             DataInputStream in = new DataInputStream(
  22.                     connection.getInputStream());
  23.             int p = in.read();
  24.             System.out.println("No of frame is:" + p);
  25.  
  26.             for (int i = 0; i < p; i++) {
  27.                 v[i] = in.read();
  28.                 System.out.println(v[i]);
  29.             }
  30.             rand = rands.nextInt(p);//FRAME NO. IS RANDOMLY GENERATED          
  31.             v[rand] = -1;
  32.             for (int i = 0; i < p; i++){
  33.                     System.out.println("Received frame is: " + v[i]);
  34.  
  35.             }
  36.             for (int i = 0; i < p; i++)
  37.                 if (v[i] == -1) {
  38.                     System.out.println("Request to retransmit from packet no "
  39.                             + (i+1) + " again!!");
  40.                     n = i;
  41.                     out.write(n);
  42.                     out.flush();
  43.                 }
  44.             System.out.println();
  45.                 v[n] = in.read();
  46.                 System.out.println("Received frame is: " + v[n]);
  47.             System.out.println("quiting");
  48.         }
  49.         catch (Exception e) {
  50.             System.out.println(e);
  51.         }
  52.  
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement