mirror of
https://github.com/guilhermewerner/java-chat
synced 2025-06-16 12:14:17 +00:00
Initial commit
This commit is contained in:
12
.editorconfig
Normal file
12
.editorconfig
Normal file
@ -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
|
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
@ -0,0 +1 @@
|
||||
* text=auto eol=lf
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
*.class
|
21
LICENSE.txt
Normal file
21
LICENSE.txt
Normal file
@ -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.
|
42
Source/Client.java
Normal file
42
Source/Client.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
84
Source/Server.java
Normal file
84
Source/Server.java
Normal file
@ -0,0 +1,84 @@
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
|
||||
public class Server {
|
||||
private ServerSocket serverSocket;
|
||||
private List<ClientHandler> 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user