Index, added simple code for connection.

This commit is contained in:
maciejrusek
2026-04-06 14:20:52 +02:00
commit 5d9417011c
10 changed files with 265 additions and 0 deletions

View File

@@ -0,0 +1,84 @@
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;
public class ChatServer implements Runnable {
private int port = 9000;
private ArrayList<Socket> clientSockets = new ArrayList<Socket>();
public ChatServer() {
}
public ChatServer(int port) {
this.port = port;
}
private User getOrCreateUser(String username) {
return new User(username);
}
private void SendMessage(User user, String msg) {
try {
for (Socket client : clientSockets) {
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
out.println(user.getUsername() + " " + msg);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void handleClient(Socket socket) {
try {
clientSockets.add(socket);
String msg;
User user = null;
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println("Login: /login username");
while ((msg = in.readLine()) != null) {
if (user != null) {
SendMessage(user, msg);
}
if (user == null) {
if (msg.toLowerCase().startsWith("/login")) {
String username = msg.substring(6).strip();
user = getOrCreateUser(username);
}
}
}
} catch (IOException e) {
clientSockets.remove(socket);
e.printStackTrace();
}
}
@Override
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(port);
while (true) {
Socket clientSocket = serverSocket.accept();
Thread thread = new Thread(() -> handleClient(clientSocket));
thread.start();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

62
src/main/java/Client.java Normal file
View File

@@ -0,0 +1,62 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class Client {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
public void startConnection(String ip, int port) throws IOException {
clientSocket = new Socket(ip, port);
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}
public void sendMessage(String message) throws IOException {
out.println(message);
}
public String loadMessage() throws IOException {
String resp = in.readLine();
return resp;
}
public void stopConnection() throws IOException {
in.close();
out.close();
clientSocket.close();
}
public static void main(String[] args) throws IOException {
Client client = new Client();
client.startConnection("127.0.0.1", 9000);
String msg;
Scanner scanner = new Scanner(System.in);
Thread thread = new Thread(() -> {
try {
String resp;
while (true) {
resp = client.loadMessage();
System.out.println(resp);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
});
thread.start();
while (true) {
msg = scanner.nextLine();
client.sendMessage(msg);
}
// client.stopConnection();
}
}

10
src/main/java/Main.java Normal file
View File

@@ -0,0 +1,10 @@
public class Main {
public static void main(String[] args) {
ChatServer chatServer = new ChatServer(9000);
Thread serverThread = new Thread(chatServer);
serverThread.start();
}
}

16
src/main/java/User.java Normal file
View File

@@ -0,0 +1,16 @@
public class User {
private String username;
public User(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}