Question: Converting java code to python server code package cpcs371_project; import java.net.*; import java.io.*; public class Group5_server { private Socket socket; private ServerSocket server; private DataInputStream

Converting java code to python

server code

package cpcs371_project; import java.net.*; import java.io.*; public class Group5_server { private Socket socket; private ServerSocket server; private DataInputStream clientInputStream; private DataOutputStream clientOutputStream; private int port; private void initServer() throws IOException { server = new ServerSocket(port); System.out.println("Server started"); System.out.println("Waiting for a client ..."); } private void listenForClients() throws IOException { socket = server.accept(); clientInputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream())); clientOutputStream = new DataOutputStream(socket.getOutputStream()); System.out.println("A client has connected"); } public Group5_server(int port) { try { this.port = port; initServer(); listenForClients(); readClientInput(); System.out.println("Closing connection"); closeConnections(); } catch (BindException error) { System.err.println("A server is already running on the same port!"); System.exit(2); } catch (SocketException error) { System.err.println("The client has disconnected"); System.exit(0); } catch (IOException error) { System.err.println(error); System.err.println("Something went wrong with IO"); System.exit(1); } } private void closeConnections() throws IOException { socket.close(); clientInputStream.close(); clientOutputStream.close(); } private void readClientInput() throws IOException { // reads message from client until "n" is sent String clientInput = ""; while (!clientInput.equals("n")) { String streamInput = clientInputStream.readUTF(); if (streamInput.equals("n")) { break; } String[] splitClientInput = streamInput.split(","); int numberOfOccurrences = findNumberOfOccurrences(splitClientInput[0], splitClientInput[1].charAt(0)); sendClientTheResult(numberOfOccurrences); } clientOutputStream.writeUTF("Thank you!"); } private void sendClientTheResult(int numberOfOccurrences) throws IOException { clientOutputStream.writeInt(numberOfOccurrences); } private int findNumberOfOccurrences(String string, char characterToMatch) { int length = string.length(); int occurrence = 0; for (int i = 0; i < length; i++) { if (string.charAt(i) == characterToMatch) { occurrence++; } } return occurrence; } public static void main(String args[]) { Group5_server server = new Group5_server(5000); } }

---------------------------------------------------------------------------------------------------------------------

Converting java code to python

client code

package cpcs371_project; import java.net.*; import java.io.*; public class Group5_client { private Socket socket; private DataInputStream userInput; private DataInputStream serverOutput; private DataOutputStream serverInput; private String address; private int port; public Group5_client(String address, int port) { try { this.address = address; this.port = port; connect(); } catch (UnknownHostException error) { System.err.println(error); System.err.println("The connection cannot be established. The IP/port you have provided is not correct. The connection could not be "); System.exit(1); } catch (IOException error) { System.err.println(error); System.err.println("The connection cannot be established. The server you're trying to connect to might not be avaliable"); System.exit(2); } readUserInput(); // close the connection closeConnection(); } private void connect() throws IOException { socket = new Socket(this.address, this.port); if (socket.isConnected()) { userInput = new DataInputStream(System.in); serverOutput = new DataInputStream(socket.getInputStream()); serverInput = new DataOutputStream(socket.getOutputStream()); System.out.println("Connected to the server."); } } private void readUserInput() { String charToSearch; String line = ""; char repeat = 'y'; while (repeat != 'n' && !socket.isClosed()) { try { charToSearch = readChar(); line = readSentence(); serverInput.writeUTF(String.format("%s,%s", line, charToSearch)); readServerOutput(); if ( continueReading().equals("n")) { waitForConnectionToClose(); repeat = 'n'; } } catch (SocketException error) { System.err.println("The connection was shutdown from the server side."); System.exit(2); } catch (IOException i) { System.out.println(i); } } } private void waitForConnectionToClose() throws IOException { serverInput.writeUTF("n"); System.out.println(serverOutput.readUTF()); } private void readServerOutput() throws IOException { System.out.printf("The number of Occurrences are: %d ", serverOutput.readInt()); } private String continueReading() throws IOException { String choice = ""; while (true) { System.out.printf("Want to repeat?(Y/N): "); choice = userInput.readLine().toLowerCase(); if (choice.equals("y") || choice.equals("n")) { return choice; } System.out.println("Incorrect input, enter either (Y/N)"); } } private String readChar() throws IOException { while (true) { System.out.printf("Enter a Character to be searched: "); String line = userInput.readLine(); if (line.length() > 1 || line.equals("")) { System.err.println("You've entered more than one character! Please try again."); continue; } return line.substring(0, 1); } } private String readSentence() throws IOException { while (true) { System.out.printf("Enter a String: "); String line = userInput.readLine(); if (line.length() == 0) { System.err.println("You did not enter any string! Please try again."); continue; } return line; } } private void closeConnection() { try { userInput.close(); serverOutput.close(); socket.close(); } catch (IOException error) { System.out.println(error); } } public static void main(String args[]) { Group5_client client = new Group5_client("127.0.0.1", 5000); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!