mirror of
https://github.com/guilhermewerner/redes
synced 2025-06-15 22:45:49 +00:00
Initial chat room
This commit is contained in:
111
src/main/java/org/example/ClientApp.java
Normal file
111
src/main/java/org/example/ClientApp.java
Normal file
@ -0,0 +1,111 @@
|
||||
package org.example;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.Socket;
|
||||
|
||||
public class ClientApp extends JFrame {
|
||||
private static final String SERVER_IP = "localhost";
|
||||
private static final int SERVER_PORT = 1234;
|
||||
|
||||
private JTextArea chatArea;
|
||||
private JTextField messageField;
|
||||
private JButton sendButton;
|
||||
|
||||
private Socket socket;
|
||||
private BufferedReader in;
|
||||
private PrintWriter out;
|
||||
|
||||
public ClientApp() {
|
||||
setTitle("Chat Client");
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setSize(400, 300);
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
chatArea = new JTextArea();
|
||||
chatArea.setEditable(false);
|
||||
add(new JScrollPane(chatArea), BorderLayout.CENTER);
|
||||
|
||||
JPanel bottomPanel = new JPanel();
|
||||
bottomPanel.setLayout(new BorderLayout());
|
||||
|
||||
messageField = new JTextField();
|
||||
bottomPanel.add(messageField, BorderLayout.CENTER);
|
||||
|
||||
sendButton = new JButton("Send");
|
||||
sendButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
sendMessage();
|
||||
}
|
||||
});
|
||||
bottomPanel.add(sendButton, BorderLayout.EAST);
|
||||
|
||||
add(bottomPanel, BorderLayout.SOUTH);
|
||||
|
||||
setVisible(true);
|
||||
|
||||
connectToServer();
|
||||
startListening();
|
||||
}
|
||||
|
||||
private void connectToServer() {
|
||||
try {
|
||||
socket = new Socket(SERVER_IP, SERVER_PORT);
|
||||
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
out = new PrintWriter(socket.getOutputStream(), true);
|
||||
chatArea.append("Connected to server\n");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void startListening() {
|
||||
Thread thread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
String serverMessage;
|
||||
while ((serverMessage = in.readLine()) != null) {
|
||||
chatArea.append("Server: " + serverMessage + "\n");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
in.close();
|
||||
out.close();
|
||||
socket.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
}
|
||||
|
||||
private void sendMessage() {
|
||||
String message = messageField.getText().trim();
|
||||
if (!message.isEmpty()) {
|
||||
out.println(message);
|
||||
messageField.setText("");
|
||||
chatArea.append("You: " + message + "\n");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
new ClientApp();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package org.example;
|
||||
|
||||
// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
|
||||
// then press Enter. You can now see whitespace characters in your code.
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// Press Alt+Enter with your caret at the highlighted text to see how
|
||||
// IntelliJ IDEA suggests fixing it.
|
||||
System.out.println("Hello and welcome!");
|
||||
|
||||
// Press Shift+F10 or click the green arrow button in the gutter to run the code.
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
|
||||
// Press Shift+F9 to start debugging your code. We have set one breakpoint
|
||||
// for you, but you can always add more by pressing Ctrl+F8.
|
||||
System.out.println("i = " + i);
|
||||
}
|
||||
}
|
||||
}
|
92
src/main/java/org/example/ServerApp.java
Normal file
92
src/main/java/org/example/ServerApp.java
Normal file
@ -0,0 +1,92 @@
|
||||
package org.example;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ServerApp {
|
||||
private static final int PORT = 1234;
|
||||
private List<ClientHandler> clients;
|
||||
|
||||
public ServerApp() {
|
||||
clients = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void start() {
|
||||
try {
|
||||
ServerSocket serverSocket = new ServerSocket(PORT);
|
||||
System.out.println("Server started on port " + PORT);
|
||||
|
||||
while (true) {
|
||||
Socket clientSocket = serverSocket.accept();
|
||||
System.out.println("New client connected: " + clientSocket);
|
||||
|
||||
ClientHandler clientHandler = new ClientHandler(clientSocket);
|
||||
clients.add(clientHandler);
|
||||
clientHandler.start();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void broadcastMessage(String message, ClientHandler sender) {
|
||||
for (ClientHandler client : clients) {
|
||||
if (client != sender) {
|
||||
client.sendMessage(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void removeClient(ClientHandler client) {
|
||||
clients.remove(client);
|
||||
}
|
||||
|
||||
private class ClientHandler extends Thread {
|
||||
private Socket clientSocket;
|
||||
private BufferedReader in;
|
||||
private PrintWriter out;
|
||||
|
||||
public ClientHandler(Socket socket) {
|
||||
this.clientSocket = socket;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
||||
out = new PrintWriter(clientSocket.getOutputStream(), true);
|
||||
|
||||
String clientMessage;
|
||||
while ((clientMessage = in.readLine()) != null) {
|
||||
broadcastMessage(clientMessage, this);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
in.close();
|
||||
out.close();
|
||||
clientSocket.close();
|
||||
removeClient(this);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void sendMessage(String message) {
|
||||
out.println(message);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ServerApp server = new ServerApp();
|
||||
server.start();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user