Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import java.util.Scanner;
- import java.io.*;
- import java.net.*;
- import javafx.application.Application;
- import javafx.fxml.FXMLLoader;
- import javafx.scene.Cursor;
- import javafx.scene.control.Hyperlink;
- import javafx.scene.control.skin.LabeledSkinBase;
- import javafx.scene.image.Image;
- import javafx.scene.image.ImageView;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.StackPane;
- import javafx.scene.control.TextField;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.layout.VBox;
- import javafx.stage.Stage;
- import org.w3c.dom.html.HTMLObjectElement;
- import javax.swing.text.TabExpander;
- import java.io.FileInputStream;
- public class MyServer extends Application {
- public static void main(String[] args) {
- launch(args);
- }
- String data;
- Socket socket;
- @Override
- public void start(Stage primaryStage) throws Exception {
- //starting with a Logo***************************************
- FileInputStream inputStream = new FileInputStream("/home/ahsanul/Desktop/java programs/server/logo.jpg");
- Image image = new Image(inputStream);
- ImageView imageview = new ImageView(image);
- //All the Labels***********************************************
- Label label1 = new Label("Write the text : ");
- Label label2 = new Label("You have a message : ");
- Label message = new Label();
- Label gap1 = new Label();
- Label gap2 = new Label();
- Label gap0 = new Label();
- //All Textfields**********************************************
- TextField text1 = new TextField();
- //All buttons**************************************************
- Button button1 = new Button("Send!");
- Button recieve = new Button("Active Status!");
- //fix height and width****************************************
- gap1.setPrefHeight(150);
- gap2.setPrefWidth(310);
- text1.setPrefWidth(380);
- gap0.setPrefWidth(50);
- //Event handler part******************************************
- button1.setOnAction(e->{
- data = text1.getText();
- int port = 6666;
- try(ServerSocket serverSocket = new ServerSocket(port)) {
- System.out.println("server is waiting on " + port);
- socket = serverSocket.accept();
- System.out.println("client is connected");
- OutputStream output = socket.getOutputStream();
- PrintWriter writer = new PrintWriter(output ,true);
- writer.println(data);
- }
- catch(IOException ex){
- System.out.println(ex);
- }
- });
- recieve.setOnAction(e->{
- String hostname = "localhost";
- int port = 8888;
- try(Socket socket = new Socket(hostname ,port)){
- System.out.println("Connected!");
- InputStream input = socket.getInputStream();
- BufferedReader reader = new BufferedReader( new InputStreamReader(input));
- String data = reader.readLine();
- System.out.println(data);
- message.setText(data);
- }
- catch(IOException ex){
- System.out.println(ex);
- }
- });
- //All the Horaizontal box**************************************
- HBox line0 = new HBox(gap0 ,imageview);
- HBox line1 = new HBox(recieve);
- HBox line2 = new HBox(label2);
- HBox line3 = new HBox(message);
- HBox line4 = new HBox(gap1);
- HBox line5 = new HBox(text1);
- HBox line6 = new HBox(gap2 ,button1);
- //putting all box in the vartical box***************************
- VBox vbox = new VBox(line0 ,line1 ,line2 ,line3 ,line4 ,line5 ,line6);
- Scene scene = new Scene(vbox); //putting vartical box in the scene
- //Interface main part********************************************
- Stage stage = new Stage();
- stage.setTitle("CSEDU Massenger!"); //Title of the interface
- stage.setHeight(600);
- stage.setWidth(400);
- stage.setScene(scene); //putting scene on the interface
- stage.show(); //Displaying the interface
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement