Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.36 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. class TCPServer
  5. {
  6.  public static void main(String[] args) throws IOException
  7.     {
  8.   ServerSocket serverSocket = null;
  9.   ServerSocket secondServerSocket = null;
  10.      serverSocket = new ServerSocket(10007);
  11.      secondServerSocket = new ServerSocket(10008);
  12.      Socket clientSocket = null;
  13.      Socket secondClientSocket = null;
  14.      clientSocket = serverSocket.accept();
  15.      secondClientSocket = secondServerSocket.accept();
  16.      PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
  17.      BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  18.      
  19.      PrintWriter secondOut = new PrintWriter(secondClientSocket.getOutputStream(), true);
  20.      BufferedReader secondIn = new BufferedReader(new InputStreamReader(secondClientSocket.getInputStream()));
  21.      
  22.      String inputLine, secondInputLine;
  23.      System.out.println("Connection Establisted");
  24.      while ((inputLine = in.readLine()) != null)
  25.      {
  26.       int binary[] = new int[inputLine.length()];
  27.    
  28.    for (int i = 0; i < inputLine.length(); i++)
  29.    {
  30.     binary[i] = inputLine.charAt(i) - '0';
  31.    }
  32.    
  33.    boolean isBinary = true;
  34.    
  35.    for (int i = 0; i < binary.length; i++)
  36.    {
  37.     if (binary[i] < 0 || binary[i] > 1)
  38.     {
  39.      isBinary = false;
  40.     }
  41.    }
  42.  
  43.    if (inputLine.equals("Bye."))
  44.       {
  45.           break;
  46.       }
  47.    else if (!isBinary)
  48.    {
  49.     System.out.println ("Server: " + inputLine);
  50.     out.println("Needs to be binary code.");
  51.     secondOut.println("Needs to be binary code.");
  52.     isBinary = true;
  53.    }
  54.    else if (binary.length != 11)
  55.    {
  56.     out.println("Your binary message needs to be exactly 11 bits long.");
  57.     secondOut.println("Your binary message needs to be exactly 11 bits long.");
  58.    }
  59.       else
  60.       {
  61.        System.out.println ("Server: " + inputLine);
  62.        if (checkP1(binary) && checkP2(binary) && checkP3(binary) && checkP4(binary))
  63.        {
  64.         out.println("No error detected. The original message is " + binary[0] + binary[1]
  65.            + binary[2] + binary[4] + binary[5] + binary[6] + binary[8]);
  66.         secondOut.println("No error detected. The original message is " + binary[0] + binary[1]
  67.           + binary[2] + binary[4] + binary[5] + binary[6] + binary[8]);
  68.        }
  69.        else
  70.        {
  71.         int problemBit = 0;
  72.         if (!checkP1(binary))
  73.         {
  74.          problemBit += 1;
  75.         }
  76.         if (!checkP2(binary))
  77.         {
  78.          problemBit += 2;
  79.         }
  80.         if (!checkP3(binary))
  81.         {
  82.          problemBit += 4;
  83.         }
  84.         if (!checkP4(binary))
  85.         {
  86.          problemBit += 8;
  87.         }
  88.         out.println("There was an error detected in your hamming code. The error is in bit " +
  89.            problemBit + ".");
  90.         secondOut.println("There was an error detected in your hamming code. The error is in bit " +
  91.           problemBit + ".");
  92.        }
  93.       }
  94.      }
  95.      
  96.      out.close();
  97.      in.close();
  98.      clientSocket.close();
  99.      serverSocket.close();
  100.      secondOut.close();
  101.      secondIn.close();
  102.      secondClientSocket.close();
  103.      secondServerSocket.close();
  104.     }
  105.  
  106.  public static boolean checkP1(int binary[])
  107.  {
  108.   boolean parityCheck;
  109.   int count = 0;
  110.  
  111.   count = binary[10] + binary[8] + binary[6] + binary[4] + binary[2] + binary[0];
  112.  
  113.   if (count % 2 == 0)
  114.   {
  115.    parityCheck = true;
  116.   }
  117.   else
  118.   {
  119.    parityCheck = false;
  120.   }
  121.  
  122.   return parityCheck;
  123.  }
  124.  
  125.  public static boolean checkP2(int binary[])
  126.  {
  127.   boolean parityCheck;
  128.   int count = 0;
  129.  
  130.   count = binary[9] + binary[8] + binary[5] + binary[4] + binary[1] + binary[0];
  131.  
  132.   if (count % 2 == 0)
  133.   {
  134.    parityCheck = true;
  135.   }
  136.   else
  137.   {
  138.    parityCheck = false;
  139.   }
  140.  
  141.   return parityCheck;
  142.  }
  143.  
  144.  public static boolean checkP3(int binary[])
  145.  {
  146.   boolean parityCheck;
  147.   int count = 0;
  148.  
  149.   count = binary[7] + binary[6] + binary[5] + binary[4];
  150.  
  151.   if (count % 2 == 0)
  152.   {
  153.    parityCheck = true;
  154.   }
  155.   else
  156.   {
  157.    parityCheck = false;
  158.   }
  159.  
  160.   return parityCheck;
  161.  }
  162.  
  163.  public static boolean checkP4(int binary[])
  164.  {
  165.   boolean parityCheck;
  166.   int count = 0;
  167.  
  168.   count = binary[3] + binary[2] + binary[1] + binary[0];
  169.  
  170.   if (count % 2 == 0)
  171.   {
  172.    parityCheck = true;
  173.   }
  174.   else
  175.   {
  176.    parityCheck = false;
  177.   }
  178.  
  179.   return parityCheck;
  180.  }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement