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

Update: X and Y can now be stored in the server class.

parent c86bbcca
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
......@@ -21,12 +21,12 @@ public class Client {
}
public int sendX(int number) throws IOException {
out.println(number);
out.println("x:" + number);
return number;
}
public int sendY(int number) throws IOException {
out.println(number);
out.println("y:" + number);
return number;
}
......
......@@ -7,12 +7,12 @@ public class ConnectionHandling {
public static void main(String[] args) throws IOException, InterruptedException {
Client client = new Client();
client.startConnection("localhost", 4444);
client.startConnection("192.168.0.105", 4444);
for (int i = 0; i < 10; i++){
int x = client.sendX(1);
int x = client.sendX(1+i);
System.out.println(x);
int y = client.sendX(0);
int y = client.sendY(0+i);
System.out.println(y);
TimeUnit.SECONDS.sleep(1);
......
......@@ -2,21 +2,24 @@ package de.hdm_stuttgart.battlearena.Model.Multiplayer.TestServer;
import java.net.*;
import java.io.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Server {
private ServerSocket serverSocket;
private static BufferedReader px;
private static BufferedReader py;
private static int px;
private static int py;
public static void main(String[] args) throws IOException {
Server server = new Server();
server.start("localhost",4444);
server.start("192.168.0.105",4444);
}
public void start(String lolcalhost, int port) throws IOException {
serverSocket = new ServerSocket(port);
while (true)
new EchoClientHandler(serverSocket.accept()).start();
new ServerHandler(serverSocket.accept()).start();
}
public void stop() throws IOException {
......@@ -25,54 +28,46 @@ public class Server {
// EchoClientHandler class in the Server
private static class EchoClientHandler extends Thread {
private static class ServerHandler extends Thread {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
public EchoClientHandler(Socket socket) {
public ServerHandler(Socket socket) {
this.clientSocket = socket;
}
public void run() {
try {
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
BufferedReader inputLine = in;
px = in;
executorService.scheduleAtFixedRate(() -> {
try {
BufferedReader localIn = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter localOut = new PrintWriter(clientSocket.getOutputStream(), true);
while (true) {
System.out.println("Received message from client: " + px.readLine());
String inputLine = localIn.readLine();
if (inputLine.equals(0)) {
out.println("bye");
out.flush();
System.out.println("Sent response to client: bye");
break;
}
if (inputLine != null) {
if (inputLine.startsWith("x:")) {
int receivedX = Integer.parseInt(inputLine.substring(2));
px = receivedX;
out.println(px);
out.flush();
System.out.println("Sent response to client: " + px.readLine());
}
} catch (SocketException e) {
System.out.println("Client connection reset.");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
out.close();
clientSocket.close();
} else if (inputLine.startsWith("y:")) {
int receivedY = Integer.parseInt(inputLine.substring(2));
py = receivedY;
}
localOut.println(inputLine);
localOut.flush();
System.out.println("Current X: " + px + " Current Y: " + py);
System.out.println("Sent response to client: " + inputLine);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}, 0, 250, TimeUnit.MILLISECONDS);
}
}
}
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