Advertisement
Guest User

ulala messenger

a guest
Feb 24th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.50 KB | None | 0 0
  1. package sample;
  2.  
  3. import java.util.Scanner;
  4.  
  5. import java.io.*;
  6. import java.net.*;
  7. import javafx.application.Application;
  8. import javafx.fxml.FXMLLoader;
  9. import javafx.scene.Cursor;
  10. import javafx.scene.control.Hyperlink;
  11. import javafx.scene.control.skin.LabeledSkinBase;
  12. import javafx.scene.image.Image;
  13. import javafx.scene.image.ImageView;
  14. import javafx.scene.layout.HBox;
  15. import javafx.scene.layout.StackPane;
  16. import javafx.scene.control.TextField;
  17. import javafx.scene.Scene;
  18. import javafx.scene.control.Button;
  19. import javafx.scene.control.Label;
  20. import javafx.scene.layout.VBox;
  21. import javafx.stage.Stage;
  22. import org.w3c.dom.html.HTMLObjectElement;
  23.  
  24. import javax.swing.text.TabExpander;
  25. import java.io.FileInputStream;
  26.  
  27. public class MyServer extends Application {
  28.  
  29.     public static void main(String[] args) {
  30.         launch(args);
  31.     }
  32.  
  33.     String  data;
  34.     Socket socket;
  35.  
  36.     @Override
  37.     public void start(Stage primaryStage) throws Exception {
  38.  
  39.         //starting with a Logo***************************************
  40.  
  41.         FileInputStream inputStream = new FileInputStream("/home/ahsanul/Desktop/java programs/server/logo.jpg");
  42.  
  43.         Image image = new Image(inputStream);
  44.  
  45.         ImageView imageview = new ImageView(image);
  46.  
  47.  
  48.  
  49.  
  50.         //All the Labels***********************************************
  51.  
  52.         Label  label1 = new Label("Write the text : ");
  53.         Label label2 = new Label("You have a message : ");
  54.         Label message = new Label();
  55.         Label gap1 = new Label();
  56.         Label gap2 = new Label();
  57.         Label gap0 = new Label();
  58.  
  59.  
  60.  
  61.  
  62.         //All Textfields**********************************************
  63.  
  64.         TextField text1 = new TextField();
  65.  
  66.  
  67.  
  68.         //All buttons**************************************************
  69.  
  70.         Button button1 = new Button("Send!");
  71.         Button recieve = new Button("Active Status!");
  72.  
  73.  
  74.  
  75.         //fix height and width****************************************
  76.  
  77.         gap1.setPrefHeight(150);
  78.         gap2.setPrefWidth(310);
  79.         text1.setPrefWidth(380);
  80.         gap0.setPrefWidth(50);
  81.  
  82.  
  83.         //Event handler part******************************************
  84.  
  85.         button1.setOnAction(e->{
  86.  
  87.             data = text1.getText();
  88.  
  89.             int port = 6666;
  90.  
  91.             try(ServerSocket serverSocket = new ServerSocket(port)) {
  92.  
  93.                 System.out.println("server is waiting on " + port);
  94.  
  95.                 socket = serverSocket.accept();
  96.  
  97.                 System.out.println("client is connected");
  98.                 OutputStream output = socket.getOutputStream();
  99.  
  100.                 PrintWriter writer = new PrintWriter(output ,true);
  101.  
  102.  
  103.                 writer.println(data);
  104.  
  105.             }
  106.             catch(IOException ex){
  107.  
  108.                 System.out.println(ex);
  109.             }
  110.  
  111.         });
  112.  
  113.  
  114.         recieve.setOnAction(e->{
  115.  
  116.             String hostname = "localhost";
  117.  
  118.             int port = 8888;
  119.  
  120.             try(Socket  socket = new Socket(hostname ,port)){
  121.  
  122.  
  123.                 System.out.println("Connected!");
  124.  
  125.                 InputStream input = socket.getInputStream();
  126.  
  127.                 BufferedReader reader = new BufferedReader( new InputStreamReader(input));
  128.  
  129.                 String data = reader.readLine();
  130.  
  131.                 System.out.println(data);
  132.  
  133.                 message.setText(data);
  134.  
  135.  
  136.             }
  137.             catch(IOException ex){
  138.  
  139.                 System.out.println(ex);
  140.             }
  141.  
  142.         });
  143.  
  144.  
  145.         //All the Horaizontal box**************************************
  146.  
  147.         HBox line0 = new HBox(gap0 ,imageview);
  148.         HBox line1 = new HBox(recieve);
  149.         HBox line2 = new HBox(label2);
  150.         HBox line3 = new HBox(message);
  151.         HBox line4 = new HBox(gap1);
  152.         HBox line5 = new HBox(text1);
  153.         HBox line6 = new HBox(gap2 ,button1);
  154.  
  155.  
  156.  
  157.  
  158.         //putting all box in the vartical box***************************
  159.  
  160.         VBox vbox = new VBox(line0 ,line1 ,line2 ,line3 ,line4 ,line5 ,line6);
  161.  
  162.         Scene scene = new Scene(vbox);          //putting vartical box in the scene
  163.  
  164.  
  165.  
  166.         //Interface main part********************************************
  167.         Stage stage = new Stage();
  168.         stage.setTitle("CSEDU Massenger!");     //Title of the interface
  169.         stage.setHeight(600);
  170.         stage.setWidth(400);
  171.         stage.setScene(scene);                 //putting scene on the interface
  172.         stage.show();                          //Displaying the interface
  173.  
  174.  
  175.  
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement