├── .idea
├── .gitignore
├── copyright
│ ├── manee.xml
│ └── profiles_settings.xml
├── libraries
│ └── colors_1_4.xml
├── misc.xml
├── modules.xml
└── vcs.xml
├── GUI chat Application.iml
├── LICENSE
├── README.md
├── lib
├── colors-1.4.jar
├── controlsfx-8.40.12.jar
├── fontawesomefx-fontawesome-4.7.0-9.1.2.jar
├── jfoenix-8.0.7.jar
└── jfoenix-8.0.8.jar
├── out
└── production
│ └── GUI chat Application
│ ├── assets
│ └── Untitled design (1).png
│ ├── client
│ ├── Client$1.class
│ ├── Client.class
│ ├── Controller$1.class
│ ├── Controller$2.class
│ ├── Controller$3.class
│ ├── Controller.class
│ ├── Main.class
│ └── sample.fxml
│ ├── lib
│ ├── colors-1.4.jar
│ ├── controlsfx-8.40.12.jar
│ ├── fontawesomefx-fontawesome-4.7.0-9.1.2.jar
│ ├── jfoenix-8.0.7.jar
│ └── jfoenix-8.0.8.jar
│ └── server
│ ├── Controller$1.class
│ ├── Controller$2.class
│ ├── Controller$3.class
│ ├── Controller.class
│ ├── Main.class
│ ├── Server$1.class
│ ├── Server.class
│ └── sample.fxml
└── src
├── assets
└── Untitled design (1).png
├── client
├── Client.java
├── Controller.java
├── Main.java
└── sample.fxml
├── lib
├── colors-1.4.jar
├── controlsfx-8.40.12.jar
├── fontawesomefx-fontawesome-4.7.0-9.1.2.jar
├── jfoenix-8.0.7.jar
└── jfoenix-8.0.8.jar
└── server
├── Controller.java
├── Main.java
├── Server.java
└── sample.fxml
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 | # Datasource local storage ignored files
5 | /dataSources/
6 | /dataSources.local.xml
7 | # Editor-based HTTP Client requests
8 | /httpRequests/
9 |
--------------------------------------------------------------------------------
/.idea/copyright/manee.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.idea/libraries/colors_1_4.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/GUI chat Application.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 Maneesha Gunawardhana
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ### Technology Stack - Java , JavaFx , Socket Programming
2 |
3 | ## java socket programming
4 |
5 | Socket programming in Java is used for communication between the applications that are running on different JRE. It can be either connection-oriented or connectionless. On the whole, a socket is a way to establish a connection between a client and a server.
6 |
7 |
8 |
9 |
what is a Socket ?
10 |
11 | A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes are used to represent the connection between a client program and a server program. The java.net package provides two classes--Socket and ServerSocket--that implement the client side of the connection and the server side of the connection, respectively.
12 |
13 |
14 |
15 | ## Definition:
16 |
17 | "A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to."
18 |
19 |
20 | - Oracle docs -
21 |
22 |
23 |
24 | 
25 |
26 |
27 |
28 |
29 |
30 |
31 | ### (★) ɪꜰ ʏᴏᴜ ʜᴀᴠᴇ ᴀɴʏ ʙᴜɢꜱ ᴏʀ ɪꜱꜱᴜᴇꜱ , ɪꜰ ʏᴏᴜ ᴡᴀɴᴛ ᴛᴏ ᴇxᴘʟᴀɪɴ ᴍʏ ᴄᴏᴅᴇ ᴏʀ ɪꜰ ʏᴏᴜ ɴᴇᴇᴅ ʜᴇʟᴘ ᴛᴏ ᴅᴇᴠᴇʟᴏᴘ ʏᴏᴜʀ ᴘʀᴏᴊᴇᴄᴛꜱ ᴘʟᴇᴀꜱᴇ ᴄᴏɴᴛᴀᴄᴛ ᴍᴇ ᴏɴ (★) 👇 mnshgnwardhana622@gmail.com
32 |
33 |
34 |
35 |
36 |
37 |
38 | 
39 | 
40 | 
41 | 
42 | 
43 |
44 |
45 |
46 |
47 |
48 |
49 | ## © 2022 ɢᴜɴᴀᴡᴀʀᴅʜᴀɴᴀ,ɪɴᴄ. ᴀʟʟ ʀɪɢʜᴛꜱ ʀᴇꜱᴇʀᴠᴇᴅ.
50 |
51 |
52 |
--------------------------------------------------------------------------------
/lib/colors-1.4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/lib/colors-1.4.jar
--------------------------------------------------------------------------------
/lib/controlsfx-8.40.12.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/lib/controlsfx-8.40.12.jar
--------------------------------------------------------------------------------
/lib/fontawesomefx-fontawesome-4.7.0-9.1.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/lib/fontawesomefx-fontawesome-4.7.0-9.1.2.jar
--------------------------------------------------------------------------------
/lib/jfoenix-8.0.7.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/lib/jfoenix-8.0.7.jar
--------------------------------------------------------------------------------
/lib/jfoenix-8.0.8.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/lib/jfoenix-8.0.8.jar
--------------------------------------------------------------------------------
/out/production/GUI chat Application/assets/Untitled design (1).png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/out/production/GUI chat Application/assets/Untitled design (1).png
--------------------------------------------------------------------------------
/out/production/GUI chat Application/client/Client$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/out/production/GUI chat Application/client/Client$1.class
--------------------------------------------------------------------------------
/out/production/GUI chat Application/client/Client.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/out/production/GUI chat Application/client/Client.class
--------------------------------------------------------------------------------
/out/production/GUI chat Application/client/Controller$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/out/production/GUI chat Application/client/Controller$1.class
--------------------------------------------------------------------------------
/out/production/GUI chat Application/client/Controller$2.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/out/production/GUI chat Application/client/Controller$2.class
--------------------------------------------------------------------------------
/out/production/GUI chat Application/client/Controller$3.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/out/production/GUI chat Application/client/Controller$3.class
--------------------------------------------------------------------------------
/out/production/GUI chat Application/client/Controller.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/out/production/GUI chat Application/client/Controller.class
--------------------------------------------------------------------------------
/out/production/GUI chat Application/client/Main.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/out/production/GUI chat Application/client/Main.class
--------------------------------------------------------------------------------
/out/production/GUI chat Application/client/sample.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
--------------------------------------------------------------------------------
/out/production/GUI chat Application/lib/colors-1.4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/out/production/GUI chat Application/lib/colors-1.4.jar
--------------------------------------------------------------------------------
/out/production/GUI chat Application/lib/controlsfx-8.40.12.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/out/production/GUI chat Application/lib/controlsfx-8.40.12.jar
--------------------------------------------------------------------------------
/out/production/GUI chat Application/lib/fontawesomefx-fontawesome-4.7.0-9.1.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/out/production/GUI chat Application/lib/fontawesomefx-fontawesome-4.7.0-9.1.2.jar
--------------------------------------------------------------------------------
/out/production/GUI chat Application/lib/jfoenix-8.0.7.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/out/production/GUI chat Application/lib/jfoenix-8.0.7.jar
--------------------------------------------------------------------------------
/out/production/GUI chat Application/lib/jfoenix-8.0.8.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/out/production/GUI chat Application/lib/jfoenix-8.0.8.jar
--------------------------------------------------------------------------------
/out/production/GUI chat Application/server/Controller$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/out/production/GUI chat Application/server/Controller$1.class
--------------------------------------------------------------------------------
/out/production/GUI chat Application/server/Controller$2.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/out/production/GUI chat Application/server/Controller$2.class
--------------------------------------------------------------------------------
/out/production/GUI chat Application/server/Controller$3.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/out/production/GUI chat Application/server/Controller$3.class
--------------------------------------------------------------------------------
/out/production/GUI chat Application/server/Controller.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/out/production/GUI chat Application/server/Controller.class
--------------------------------------------------------------------------------
/out/production/GUI chat Application/server/Main.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/out/production/GUI chat Application/server/Main.class
--------------------------------------------------------------------------------
/out/production/GUI chat Application/server/Server$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/out/production/GUI chat Application/server/Server$1.class
--------------------------------------------------------------------------------
/out/production/GUI chat Application/server/Server.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/out/production/GUI chat Application/server/Server.class
--------------------------------------------------------------------------------
/out/production/GUI chat Application/server/sample.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
--------------------------------------------------------------------------------
/src/assets/Untitled design (1).png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/src/assets/Untitled design (1).png
--------------------------------------------------------------------------------
/src/client/Client.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Developed by - mGunawardhana
3 | * Contact email - mrgunawardhana27368@gmail.com
4 | * what's app - 071 - 9043372
5 | */
6 |
7 | package client;
8 |
9 | import javafx.scene.layout.VBox;
10 | import server.Controller;
11 |
12 | import java.io.*;
13 | import java.net.ServerSocket;
14 | import java.net.Socket;
15 |
16 | public class Client {
17 | private ServerSocket serverSocket;
18 | private Socket socket;
19 | private BufferedReader bufferedReader;
20 | private BufferedWriter bufferedWriter;
21 |
22 | public Client(Socket socket) {
23 | try {
24 | this.socket = socket;
25 | this.bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
26 | this.bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
27 | } catch (IOException e) {
28 | System.out.println("Error creating client");
29 | e.printStackTrace();
30 | closeEverything(socket,bufferedReader,bufferedWriter);
31 | }
32 | }
33 |
34 | public void sendMessageToServer(String messageToServer){
35 | try {
36 | bufferedWriter.write(messageToServer);
37 | bufferedWriter.newLine();
38 | bufferedWriter.flush();
39 | } catch (IOException e) {
40 | System.out.println("Error Sending message to client");
41 | e.printStackTrace();
42 | }
43 | }
44 |
45 | public void receiveMessageFromServer(VBox vBox){
46 | new Thread(new Runnable() {
47 | @Override
48 | public void run() {
49 | while (socket.isConnected()) {
50 |
51 | try {
52 | String messageFromServer = bufferedReader.readLine();
53 | Controller.addLabel(messageFromServer, vBox);
54 | } catch (IOException e) {
55 | System.out.println("Error receiving message from the client");
56 | closeEverything(socket,bufferedReader,bufferedWriter);
57 | break;
58 | }
59 | }
60 | }
61 | }).start();
62 | }
63 |
64 | public void closeEverything(Socket socket, BufferedReader bufferedReader, BufferedWriter bufferedWriter) {
65 | try {
66 | if (bufferedReader != null) {
67 | bufferedReader.close();
68 | }
69 | if (bufferedWriter != null) {
70 | bufferedWriter.close();
71 | }
72 | if (socket != null) {
73 | socket.close();
74 | }
75 | } catch (IOException e) {
76 | e.printStackTrace();
77 | }
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/src/client/Controller.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Developed by - mGunawardhana
3 | * Contact email - mrgunawardhana27368@gmail.com
4 | * what's app - 071 - 9043372
5 | */
6 |
7 | package client;
8 |
9 | import com.jfoenix.controls.JFXButton;
10 | import com.sun.xml.internal.messaging.saaj.soap.impl.TextImpl;
11 | import javafx.application.Platform;
12 | import javafx.beans.Observable;
13 | import javafx.beans.value.ChangeListener;
14 | import javafx.beans.value.ObservableValue;
15 | import javafx.event.ActionEvent;
16 | import javafx.event.EventHandler;
17 | import javafx.fxml.FXML;
18 | import javafx.fxml.Initializable;
19 | import javafx.geometry.Insets;
20 | import javafx.geometry.Pos;
21 | import javafx.scene.control.ScrollPane;
22 | import javafx.scene.control.TextField;
23 | import javafx.scene.layout.HBox;
24 | import javafx.scene.layout.VBox;
25 | import javafx.scene.paint.Color;
26 | import javafx.scene.text.Text;
27 | import javafx.scene.text.TextFlow;
28 | import server.Server;
29 |
30 | import java.io.IOException;
31 | import java.net.Socket;
32 | import java.net.URL;
33 | import java.util.ResourceBundle;
34 |
35 | public class Controller implements Initializable {
36 | @FXML
37 | public JFXButton button_send;
38 |
39 | @FXML
40 | public TextField tf_message;
41 |
42 | @FXML
43 | public ScrollPane sp_main;
44 |
45 | @FXML
46 | public VBox vbox_messages;
47 | private Client client;
48 |
49 |
50 | @Override
51 | public void initialize(URL location, ResourceBundle resources) {
52 |
53 | try{
54 | client = new Client(new Socket("localhost",1234));
55 | System.out.println("Connected to Server");
56 | }catch (IOException e){}
57 |
58 | vbox_messages.heightProperty().addListener(new ChangeListener() {
59 | @Override
60 | public void changed(ObservableValue extends Number> observable, Number oldValue, Number newValue) {
61 | sp_main.setVvalue((Double) newValue);
62 | }
63 | });
64 |
65 | // client.recieveMessageFromServer(vbox_messages);
66 |
67 | client.receiveMessageFromServer(vbox_messages);
68 | button_send.setOnAction(new EventHandler() {
69 | @Override
70 | public void handle(ActionEvent event) {
71 | String messageToSend = tf_message.getText();
72 | if(!messageToSend.isEmpty()){
73 | HBox hBox= new HBox();
74 | hBox.setAlignment(Pos.CENTER_RIGHT);
75 |
76 | hBox.setPadding(new Insets(5,5,5,10));
77 |
78 |
79 | javafx.scene.text.Text text = new javafx.scene.text.Text(messageToSend);
80 | TextFlow textFlow = new TextFlow(text);
81 |
82 |
83 | textFlow.setStyle("-fx-color: rgb(239,242,255);" +
84 | "-fx-background-color: rgb(15,125,242);" +
85 | " -fx-background-radius: 20px");
86 |
87 |
88 |
89 | textFlow.setPadding(new Insets(5, 10, 5, 10));
90 | text.setFill(Color.color(0.934, 0.945, 0.996));
91 |
92 | hBox.getChildren().add(textFlow);
93 | vbox_messages.getChildren().add(hBox);
94 |
95 | client.sendMessageToServer(messageToSend);
96 | tf_message.clear();
97 | }
98 | }
99 | });
100 | }
101 |
102 | public static void addLabel(String msgFromServer, VBox vBox){
103 | HBox hBox = new HBox();
104 | hBox.setAlignment(Pos.CENTER_LEFT);
105 | hBox.setPadding(new Insets(5,5,5,10));
106 |
107 | javafx.scene.text.Text text = new Text(msgFromServer);
108 | TextFlow textFlow = new TextFlow(text);
109 | textFlow.setStyle("-fx-background-color: rgb(233,233,235);" +
110 | " -fx-background-radius: 20px");
111 |
112 | textFlow.setPadding(new Insets(5,10,5,10));
113 | hBox.getChildren().add(textFlow);
114 |
115 | Platform.runLater(new Runnable() {
116 | @Override
117 | public void run() {
118 | vBox.getChildren().add(hBox);
119 |
120 | }
121 | });
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/src/client/Main.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Developed by - mGunawardhana
3 | * Contact email - mrgunawardhana27368@gmail.com
4 | * what's app - 071 - 9043372
5 | */
6 |
7 | package client;
8 |
9 | import javafx.application.Application;
10 | import javafx.fxml.FXMLLoader;
11 | import javafx.scene.Parent;
12 | import javafx.scene.Scene;
13 | import javafx.stage.Stage;
14 |
15 | import java.io.IOException;
16 |
17 | public class Main extends Application {
18 |
19 | public static void main(String[] args) {
20 | launch(args);
21 | }
22 |
23 | @Override
24 | public void start(Stage primaryStage) throws IOException {
25 | Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
26 | primaryStage.setTitle("Client");
27 | primaryStage.setScene(new Scene(root,600,600));
28 | primaryStage.show();
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/client/sample.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
--------------------------------------------------------------------------------
/src/lib/colors-1.4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/src/lib/colors-1.4.jar
--------------------------------------------------------------------------------
/src/lib/controlsfx-8.40.12.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/src/lib/controlsfx-8.40.12.jar
--------------------------------------------------------------------------------
/src/lib/fontawesomefx-fontawesome-4.7.0-9.1.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/src/lib/fontawesomefx-fontawesome-4.7.0-9.1.2.jar
--------------------------------------------------------------------------------
/src/lib/jfoenix-8.0.7.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/src/lib/jfoenix-8.0.7.jar
--------------------------------------------------------------------------------
/src/lib/jfoenix-8.0.8.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mgunawardhana/Chat-Application-for-learn-socket-programming/48138a04a1af593924ee18f738c266d43b2e6da1/src/lib/jfoenix-8.0.8.jar
--------------------------------------------------------------------------------
/src/server/Controller.java:
--------------------------------------------------------------------------------
1 | package server;
2 | /*
3 | * Developed by - mGunawardhana
4 | * Contact email - mrgunawardhana27368@gmail.com
5 | * what's app - 071 - 9043372
6 | */
7 |
8 | import com.jfoenix.controls.JFXButton;
9 | import javafx.application.Platform;
10 | import javafx.beans.value.ChangeListener;
11 | import javafx.beans.value.ObservableValue;
12 | import javafx.event.ActionEvent;
13 | import javafx.event.EventHandler;
14 | import javafx.fxml.FXML;
15 | import javafx.fxml.Initializable;
16 | import javafx.geometry.Insets;
17 | import javafx.geometry.Pos;
18 | import javafx.scene.control.ScrollPane;
19 | import javafx.scene.control.TextField;
20 | import javafx.scene.layout.HBox;
21 | import javafx.scene.layout.VBox;
22 | import javafx.scene.paint.Color;
23 | import javafx.scene.text.Text;
24 | import javafx.scene.text.TextFlow;
25 |
26 | import java.io.IOException;
27 | import java.net.ServerSocket;
28 | import java.net.URL;
29 | import java.util.ResourceBundle;
30 |
31 | public class Controller implements Initializable {
32 |
33 | @FXML
34 | public JFXButton button_send;
35 |
36 | @FXML
37 | public TextField tf_message;
38 |
39 | @FXML
40 | public ScrollPane sp_main;
41 |
42 | @FXML
43 | public VBox vbox_messages;
44 | private Server server;
45 |
46 | @Override
47 | public void initialize(URL location, ResourceBundle resources) {
48 | try {
49 | server = new Server(new ServerSocket(1234));
50 | } catch (IOException e) {
51 | e.printStackTrace();
52 | System.out.println("Error creating server");
53 | }
54 |
55 | vbox_messages.heightProperty().addListener(new ChangeListener() {
56 | @Override
57 | public void changed(ObservableValue extends Number> observable, Number oldValue, Number newValue) {
58 | sp_main.setVvalue((Double) newValue);
59 | }
60 | });
61 |
62 | // server.recieveMessageFromClient(vbox_messages);
63 |
64 | server.receiveMessageFromClient(vbox_messages);
65 |
66 | button_send.setOnAction(new EventHandler() {
67 | @Override
68 | public void handle(ActionEvent event) {
69 | String messageToSend = tf_message.getText();
70 | if (!messageToSend.isEmpty()) {
71 | HBox hBox = new HBox();
72 | hBox.setAlignment(Pos.CENTER_RIGHT);
73 | hBox.setPadding(new Insets(5, 5, 5, 10));
74 |
75 | Text text = new Text(messageToSend);
76 | TextFlow textFlow = new TextFlow(text);
77 |
78 | textFlow.setStyle("-fx-color: rgb(239,242,255);" +
79 | "-fx-background-color: rgb(15,125,242);" +
80 | " -fx-background-radius: 20px");
81 |
82 |
83 | textFlow.setPadding(new Insets(5, 10, 5, 10));
84 | text.setFill(Color.color(0.934, 0.945, 0.996));
85 |
86 | hBox.getChildren().add(textFlow);
87 | vbox_messages.getChildren().add(hBox);
88 |
89 | server.sendMessageToClient(messageToSend);
90 | tf_message.clear();
91 | }
92 | }
93 |
94 |
95 |
96 | });
97 | }
98 |
99 | public static void addLabel (String messageFromClient, VBox vbox){
100 | HBox hBox = new HBox();
101 | hBox.setAlignment(Pos.CENTER_LEFT);
102 | hBox.setPadding(new Insets(5,5,5,10));
103 |
104 | Text text = new Text(messageFromClient);
105 | TextFlow textFlow = new TextFlow(text);
106 | textFlow.setStyle("-fx-background-color: rgb(233,233,235);" +
107 | " -fx-background-radius: 20px");
108 |
109 | textFlow.setPadding(new Insets(5,10,5,10));
110 | hBox.getChildren().add(textFlow);
111 |
112 | Platform.runLater(new Runnable() {
113 | @Override
114 | public void run() {
115 | vbox.getChildren().add(hBox);
116 | }
117 | });
118 | }
119 | }
120 |
--------------------------------------------------------------------------------
/src/server/Main.java:
--------------------------------------------------------------------------------
1 | package server;
2 | /*
3 | * Developed by - mGunawardhana
4 | * Contact email - mrgunawardhana27368@gmail.com
5 | * what's app - 071 - 9043372
6 | */
7 |
8 | import javafx.application.Application;
9 | import javafx.fxml.FXMLLoader;
10 | import javafx.scene.Parent;
11 | import javafx.scene.Scene;
12 | import javafx.stage.Stage;
13 |
14 | import java.io.IOException;
15 |
16 | public class Main extends Application {
17 |
18 | public static void main(String[] args) {
19 | launch(args);
20 | }
21 |
22 | @Override
23 | public void start(Stage primaryStage) throws IOException {
24 | Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
25 | primaryStage.setTitle("server");
26 | primaryStage.setScene(new Scene(root,600,600));
27 | primaryStage.show();
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/server/Server.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Developed by - mGunawardhana
3 | * Contact email - mrgunawardhana27368@gmail.com
4 | * what's app - 071 - 9043372
5 | */
6 |
7 | package server;
8 |
9 | import javafx.scene.layout.VBox;
10 |
11 | import java.io.*;
12 | import java.net.ServerSocket;
13 | import java.net.Socket;
14 |
15 | public class Server {
16 |
17 | private ServerSocket serverSocket;
18 | private Socket socket;
19 | private BufferedReader bufferedReader;
20 | private BufferedWriter bufferedWriter;
21 |
22 | public Server(ServerSocket serverSocket) {
23 |
24 | try {
25 | this.serverSocket = serverSocket;
26 | this.socket = serverSocket.accept();
27 |
28 | this.bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
29 | this.bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
30 | } catch (IOException e) {
31 | System.out.println("Error Creating Server");
32 | closeEverything(socket, bufferedReader, bufferedWriter);
33 | }
34 | }
35 |
36 | public void sendMessageToClient(String messageToClient) {
37 | try {
38 | bufferedWriter.write(messageToClient);
39 | bufferedWriter.newLine();
40 | bufferedWriter.flush();
41 | } catch (IOException e) {
42 | System.out.println("Error Sending message to client");
43 | e.printStackTrace();
44 |
45 | }
46 | }
47 |
48 | public void receiveMessageFromClient(VBox vBox) {
49 | new Thread(new Runnable() {
50 | @Override
51 | public void run() {
52 | while (socket.isConnected()) {
53 |
54 | try {
55 | String messageFromClient = bufferedReader.readLine();
56 | Controller.addLabel(messageFromClient, vBox);
57 | } catch (IOException e) {
58 | System.out.println("Error receiving message from the client");
59 | closeEverything(socket,bufferedReader,bufferedWriter);
60 | break;
61 | }
62 | }
63 | }
64 | }).start();
65 | }
66 |
67 |
68 | public void closeEverything(Socket socket, BufferedReader bufferedReader, BufferedWriter bufferedWriter) {
69 | try {
70 | if (bufferedReader != null) {
71 | bufferedReader.close();
72 | }
73 | if (bufferedWriter != null) {
74 | bufferedWriter.close();
75 | }
76 | if (socket != null) {
77 | socket.close();
78 | }
79 | } catch (IOException e) {
80 | e.printStackTrace();
81 | }
82 | }
83 |
84 | }
85 |
--------------------------------------------------------------------------------
/src/server/sample.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
--------------------------------------------------------------------------------