commit 1026fedbab62c9d608ce64579697dda486840f72 Author: Werner Date: Fri Mar 31 13:13:00 2023 -0300 Initial commit diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..88df879 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +end_of_line = lf +indent_style = space +indent_size = 4 +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6313b56 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6b468b6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..4b939d7 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 GuilhermeWerner + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..805a838 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# JavaChat diff --git a/Source/Client.java b/Source/Client.java new file mode 100644 index 0000000..ed9ecf9 --- /dev/null +++ b/Source/Client.java @@ -0,0 +1,42 @@ +import java.io.*; +import java.net.*; + +public class Client { + private Socket clientSocket; + private PrintWriter writer; + private BufferedReader reader; + + public Client(String serverAddress, int serverPort) throws IOException { + clientSocket = new Socket(serverAddress, serverPort); + writer = new PrintWriter(clientSocket.getOutputStream(), true); + reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + } + + public void start() throws IOException { + System.out.println("Connected to server: " + clientSocket.getRemoteSocketAddress()); + + new Thread(() -> { + try { + String inputLine; + + while ((inputLine = reader.readLine()) != null) { + System.out.println(inputLine); + } + } catch (IOException e) { + System.out.println("Error receiving message: " + e); + } + }).start(); + + BufferedReader stdio = new BufferedReader(new InputStreamReader(System.in)); + String message; + + while ((message = stdio.readLine()) != null) { + writer.println(message); + } + } + + public static void main(String[] args) throws IOException { + Client client = new Client("localhost", 4321); + client.start(); + } +} diff --git a/Source/Server.java b/Source/Server.java new file mode 100644 index 0000000..b56815c --- /dev/null +++ b/Source/Server.java @@ -0,0 +1,84 @@ +import java.io.*; +import java.net.*; +import java.util.*; + +public class Server { + private ServerSocket serverSocket; + private List clients; + + public Server(int port) throws IOException { + serverSocket = new ServerSocket(port); + clients = new ArrayList<>(); + } + + public void start() throws IOException { + System.out.println("Listening on port: " + serverSocket.getLocalPort()); + + while (true) { + Socket clientSocket = serverSocket.accept(); + System.out.println("Client connected: " + clientSocket.getRemoteSocketAddress()); + + ClientHandler handler = new ClientHandler(clientSocket, this); + clients.add(handler); + + new Thread(handler).start(); + } + } + + public void broadcast(String message, ClientHandler excludeClient) { + for (ClientHandler client : clients) { + if (client != excludeClient) { + client.sendMessage(message); + } + } + } + + public void removeClient(ClientHandler client) { + clients.remove(client); + } + + public static void main(String[] args) throws IOException { + Server server = new Server(4321); + server.start(); + } +} + +class ClientHandler implements Runnable { + private Socket clientSocket; + private Server server; + private PrintWriter writer; + private BufferedReader reader; + + public ClientHandler(Socket clientSocket, Server server) throws IOException { + this.clientSocket = clientSocket; + this.server = server; + writer = new PrintWriter(clientSocket.getOutputStream(), true); + reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + } + + public void run() { + try { + String message; + + while ((message = reader.readLine()) != null) { + server.broadcast(message, this); + } + } catch (IOException e) { + System.out.println("Error handling client: " + e); + } finally { + server.removeClient(this); + + try { + reader.close(); + writer.close(); + clientSocket.close(); + } catch (IOException e) { + System.out.println("Error disconnecting: " + e); + } + } + } + + public void sendMessage(String message) { + writer.println(message); + } +}