Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- class TCPServer
- {
- public static void main(String[] args) throws IOException
- {
- ServerSocket serverSocket = null;
- ServerSocket secondServerSocket = null;
- serverSocket = new ServerSocket(10007);
- secondServerSocket = new ServerSocket(10008);
- Socket clientSocket = null;
- Socket secondClientSocket = null;
- clientSocket = serverSocket.accept();
- secondClientSocket = secondServerSocket.accept();
- PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
- BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
- PrintWriter secondOut = new PrintWriter(secondClientSocket.getOutputStream(), true);
- BufferedReader secondIn = new BufferedReader(new InputStreamReader(secondClientSocket.getInputStream()));
- String inputLine, secondInputLine;
- System.out.println("Connection Establisted");
- while ((inputLine = in.readLine()) != null)
- {
- int binary[] = new int[inputLine.length()];
- for (int i = 0; i < inputLine.length(); i++)
- {
- binary[i] = inputLine.charAt(i) - '0';
- }
- boolean isBinary = true;
- for (int i = 0; i < binary.length; i++)
- {
- if (binary[i] < 0 || binary[i] > 1)
- {
- isBinary = false;
- }
- }
- if (inputLine.equals("Bye."))
- {
- break;
- }
- else if (!isBinary)
- {
- System.out.println ("Server: " + inputLine);
- out.println("Needs to be binary code.");
- secondOut.println("Needs to be binary code.");
- isBinary = true;
- }
- else if (binary.length != 11)
- {
- out.println("Your binary message needs to be exactly 11 bits long.");
- secondOut.println("Your binary message needs to be exactly 11 bits long.");
- }
- else
- {
- System.out.println ("Server: " + inputLine);
- if (checkP1(binary) && checkP2(binary) && checkP3(binary) && checkP4(binary))
- {
- out.println("No error detected. The original message is " + binary[0] + binary[1]
- + binary[2] + binary[4] + binary[5] + binary[6] + binary[8]);
- secondOut.println("No error detected. The original message is " + binary[0] + binary[1]
- + binary[2] + binary[4] + binary[5] + binary[6] + binary[8]);
- }
- else
- {
- int problemBit = 0;
- if (!checkP1(binary))
- {
- problemBit += 1;
- }
- if (!checkP2(binary))
- {
- problemBit += 2;
- }
- if (!checkP3(binary))
- {
- problemBit += 4;
- }
- if (!checkP4(binary))
- {
- problemBit += 8;
- }
- out.println("There was an error detected in your hamming code. The error is in bit " +
- problemBit + ".");
- secondOut.println("There was an error detected in your hamming code. The error is in bit " +
- problemBit + ".");
- }
- }
- }
- out.close();
- in.close();
- clientSocket.close();
- serverSocket.close();
- secondOut.close();
- secondIn.close();
- secondClientSocket.close();
- secondServerSocket.close();
- }
- public static boolean checkP1(int binary[])
- {
- boolean parityCheck;
- int count = 0;
- count = binary[10] + binary[8] + binary[6] + binary[4] + binary[2] + binary[0];
- if (count % 2 == 0)
- {
- parityCheck = true;
- }
- else
- {
- parityCheck = false;
- }
- return parityCheck;
- }
- public static boolean checkP2(int binary[])
- {
- boolean parityCheck;
- int count = 0;
- count = binary[9] + binary[8] + binary[5] + binary[4] + binary[1] + binary[0];
- if (count % 2 == 0)
- {
- parityCheck = true;
- }
- else
- {
- parityCheck = false;
- }
- return parityCheck;
- }
- public static boolean checkP3(int binary[])
- {
- boolean parityCheck;
- int count = 0;
- count = binary[7] + binary[6] + binary[5] + binary[4];
- if (count % 2 == 0)
- {
- parityCheck = true;
- }
- else
- {
- parityCheck = false;
- }
- return parityCheck;
- }
- public static boolean checkP4(int binary[])
- {
- boolean parityCheck;
- int count = 0;
- count = binary[3] + binary[2] + binary[1] + binary[0];
- if (count % 2 == 0)
- {
- parityCheck = true;
- }
- else
- {
- parityCheck = false;
- }
- return parityCheck;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement