Skip to content
Snippets Groups Projects
Commit 7fa132f9 authored by Elrabu's avatar Elrabu
Browse files

Update: TestServer can handle Multiple running instances.

parent 053bfaed
No related branches found
No related tags found
2 merge requests!35Update: Added more Stuff to be transported over the server.,!2Merge of Multiplayer Feature into development branch
......@@ -6,9 +6,13 @@ public class ConnectionHandling {
public static void main(String[] args) throws IOException {
Client client = new Client();
client.startConnection("localhost", 45371);
String response = client.sendMessage("hello");
System.out.println(response);
client.startConnection("localhost", 4444);
String resp1 = client.sendMessage("hello");
System.out.println(resp1);
Client client1 = new Client();
client1.startConnection("localhost", 4444);
String resp2 = client1.sendMessage("hello");
System.out.println(resp2);
}
}
......@@ -8,48 +8,78 @@ import java.net.*;
import java.io.*;
public class Server {
private ServerSocket serverSocket;
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
private static final Logger log = LogManager.getLogger(ConnectionHandler.class);
public static void main(String[] args) throws IOException {
Server server = new Server();
server.start(45371);
server.start(4444);
}
public void start(int port) throws IOException {
serverSocket = new ServerSocket(port);
clientSocket = serverSocket.accept();
//Data that is Outputed from the server
out = new PrintWriter(clientSocket.getOutputStream(), true);
//Data that is inputed to the server
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String greeting = in.readLine();
String message = "Your Message was: " + greeting;
if ("hello".equals(greeting)) {
out.println("hello client " + message);
}
else {
out.println("unrecognised greeting " + message);
}
while (true)
new EchoClientHandler(serverSocket.accept()).start();
}
public void stop() throws IOException {
in.close();
out.close();
clientSocket.close();
serverSocket.close();
}
private static class EchoClientHandler extends Thread {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
public EchoClientHandler(Socket socket) {
this.clientSocket = socket;
}
public void run() {
try {
out = new PrintWriter(clientSocket.getOutputStream(), true);
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
} catch (IOException e) {
throw new RuntimeException(e);
}
String inputLine;
try {
while ((inputLine = in.readLine()) != null) {
System.out.println("Received message from client: " + inputLine);
try {
if ((inputLine = in.readLine()) == null) break;
} catch (IOException e) {
throw new RuntimeException(e);
}
if (".".equals(inputLine)) {
out.println("bye");
break;
}
out.println(inputLine);
out.flush();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
in.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
out.close();
try {
clientSocket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment