Commit c96f69bf authored by 9731087's avatar 9731087

Main, Server & ClientHandler added

parent 6e8db573
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class ClientHandler {
private Socket socket;
private DataOutputStream dataOutputStream;
private DataInputStream dataInputStream;
public ClientHandler(Socket client) throws Exception {
if (client == null)
throw new Exception("client can't be null");
this.socket = client;
dataOutputStream = new DataOutputStream(client.getOutputStream());
dataInputStream = new DataInputStream(client.getInputStream());
run();
}
private void handleIncomingMessages(String message) {
//TODO: implement this method
System.out.println(message);
}
public void run() {
while (true) {
try {
String input = dataInputStream.readUTF();
handleIncomingMessages(input);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private static final int PORT = 4321;
private ServerSocket serverSocket;
public Server() throws IOException {
serverSocket = new ServerSocket(PORT);
while (true) {
Socket client = serverSocket.accept();
try {
ClientHandler clientHandler = new ClientHandler(client);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment