https://github.com/vasylishchuk/chatconnect_server
A simple Java-based chat server implementation that supports basic client-server communication. The server listens for incoming client connections, handles message exchange, and supports multiple clients simultaneously. Ideal for understanding socket programming and building real-time messaging applications.
https://github.com/vasylishchuk/chatconnect_server
java server socket study-project
Last synced: 12 months ago
JSON representation
A simple Java-based chat server implementation that supports basic client-server communication. The server listens for incoming client connections, handles message exchange, and supports multiple clients simultaneously. Ideal for understanding socket programming and building real-time messaging applications.
- Host: GitHub
- URL: https://github.com/vasylishchuk/chatconnect_server
- Owner: VasylIshchuk
- Created: 2024-06-08T17:07:47.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2025-04-04T20:31:02.000Z (about 1 year ago)
- Last Synced: 2025-05-17T04:11:16.008Z (about 1 year ago)
- Topics: java, server, socket, study-project
- Language: Java
- Homepage:
- Size: 19.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README(example simple client).md
Awesome Lists containing this project
README
package org.umcs.client;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public void connect(int port) throws IOException {
Socket clientSocket = new Socket("localhost",port);
while(true) {
ClientHandler is responsible for connecting to the server and represents the client that receives the message
ClientHandler clientHandler = new ClientHandler(clientSocket);
Thread thread = new Thread(clientHandler);
thread.start();
Represents the client that sends the message
Scanner reader = new Scanner(System.in);
String message;
while((message = reader.nextLine())!= null){
clientHandler.send(message);
if (message.equals("/exit")) System.exit(0);
}
}
}
}
package org.umcs.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ClientHandler implements Runnable{
BufferedReader reader;
PrintWriter writer;
public ClientHandler(Socket clientSocket) throws IOException {
reader = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()
));
writer = new PrintWriter(clientSocket.getOutputStream(),true);
}
@Override
public void run() {
String message;
try {
while((message = reader.readLine())!= null) System.out.println(message);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void send(String message){
writer.println(message);
}
}