Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static class Capitalizer extends Thread {
- private Socket socket;
- private int clientNumber;
- public Capitalizer(Socket socket, int clientNumber) {
- this.socket = socket;
- this.clientNumber = clientNumber;
- log("New connection with client# " + clientNumber + " at " + socket);
- }
- /**
- * Services this thread's client by first sending the
- * client a welcome message then repeatedly reading strings
- * and sending back the capitalized version of the string.
- */
- public void run() {
- try {
- // Decorate the streams so we can send characters
- // and not just bytes. Ensure output is flushed
- // after every newline.
- BufferedReader in = new BufferedReader(
- new InputStreamReader(socket.getInputStream()));
- PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
- // Send a welcome message to the client.
- out.println("Hello, you are client #" + clientNumber + ".");
- out.println("Enter a line with only a period to quitn");
- // Get messages from the client, line by line; return them
- // capitalized
- while (true) {
- String input = in.readLine();
- if (input == null || input.equals(".")) {
- break;
- }
- out.println(solveCube(input));
- }
- } catch (IOException e) {
- log("Error handling client# " + clientNumber + ": " + e);
- } finally {
- try {
- socket.close();
- } catch (IOException e) {
- log("Couldn't close a socket, what's going on?");
- }
- log("Connection with client# " + clientNumber + " closed");
- }
- }
- /**
- * Logs a simple message. In this case we just write the
- * message to the server applications standard output.
- */
- private void log(String message) {
- System.out.println(message);
- }
- }
- private static class Connecter extends Thread {
- /**
- * Services this thread's client by first sending the
- * client a welcome message then repeatedly reading strings
- * and sending back the capitalized version of the string.
- */
- public void run() {
- try {
- System.out.println("The capitalization server is running.");
- int clientNumber = 0;
- ServerSocket listener = new ServerSocket(38300);
- try {
- while (true) {
- new Capitalizer(listener.accept(), clientNumber++).start();
- }
- } finally {
- listener.close();
- }
- } catch (IOException ex) {
- Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- private final Runnable connectToServer = new Thread()
- {
- @Override
- public void run()
- {
- try
- {// Get the server address from a dialog box.
- String serverAddress = "192.168.0.23";
- // Make connection and initialize streams
- Socket socket = new Socket(serverAddress, 38300);
- in = new BufferedReader(
- new InputStreamReader(socket.getInputStream()));
- out = new PrintWriter(socket.getOutputStream(), true);
- // Consume the initial welcoming messages from the server
- for (int i = 0; i < 3; i++) {
- System.out.println(in.readLine());
- }
- solveCube();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- };
- private final Runnable initializeConnection = new Thread()
- {
- @Override
- public void run()
- {
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(generateCubeString());
- out.println(generateCubeString());
- String response ="";
- try {
- response = in.readLine();
- if (response == null || response.equals("")) {
- System.exit(0);
- }
- } catch (IOException ex) {
- }
- if (response.contains("Error")) {
- } else {
- solveCubeAnimate(response);
- }
- System.out.println(response);
- final String finalResponse = response;
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- textView.setText(finalResponse);
- }
- });
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement