Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1.  
  2. import com.sun.corba.se.spi.activation.Server;
  3. import javafx.application.Application;
  4. import javafx.event.ActionEvent;
  5. import javafx.event.EventHandler;
  6. import javafx.scene.Scene;
  7. import javafx.scene.control.Button;
  8. import javafx.scene.layout.StackPane;
  9. import javafx.stage.Stage;
  10.  
  11. import java.io.*;
  12. import java.net.ServerSocket;
  13. import java.net.Socket;
  14.  
  15. public class Client extends Application {
  16.     public static void main(String[] args){
  17.         launch(args);
  18.     }
  19.     @Override
  20.     public void start(Stage primaryStage){
  21.         primaryStage.setTitle("Hello world!");
  22.         Button btn = new Button();
  23.         btn.setText("Run Client");
  24.  
  25.         btn.setOnAction(new EventHandler<ActionEvent>() {
  26.             @Override
  27.             public void handle(ActionEvent event) {
  28.                 try{runClient();}catch (Exception e){System.out.println("Exception Occurred, Server is down.");}
  29.             }
  30.         });
  31.  
  32.  
  33.         StackPane root = new StackPane();
  34.         root.getChildren().addAll(btn);
  35.         primaryStage.setScene(new Scene(root, 500, 500));
  36.         primaryStage.show();
  37.     }
  38.  
  39.  
  40.  
  41.     public void runClient() throws Exception {
  42.         String sentence;
  43.         String modSentence;
  44.         BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
  45.         Socket clientSocket = new Socket("localhost", 6789);
  46.         DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
  47.         BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  48.         sentence = inFromUser.readLine();
  49.         outToServer.writeBytes(sentence + "\n");
  50.         modSentence = inFromServer.readLine();
  51.         System.out.println("From Server: " + modSentence);
  52.         clientSocket.close();
  53.     }
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement