Skip to content
Snippets Groups Projects
Commit d5104b61 authored by Leonard's avatar Leonard
Browse files

server done

parent a6be7621
No related branches found
No related tags found
No related merge requests found
package com.galaxytrucker.galaxytruckerreloaded.Communication; package com.galaxytrucker.galaxytruckerreloaded.Communication;
import lombok.NonNull;
import java.net.Socket; import java.net.Socket;
public class Client { public class Client {
/** Client socket for network communication */
private Socket socket; private Socket socket;
/** ClientControllerCommunicator for logic handling */
private ClientControllerCommunicator clientControllerCommunicator; private ClientControllerCommunicator clientControllerCommunicator;
/** /**
...@@ -25,20 +29,14 @@ public class Client { ...@@ -25,20 +29,14 @@ public class Client {
/** Constructor /** Constructor
* Loop for receiving Data * Loop for receiving Data
* */ * */
public Client(){ public Client(@NonNull String ipAddress, @NonNull int port) throws IllegalArgumentException{
clientControllerCommunicator = new ClientControllerCommunicator(); clientControllerCommunicator = new ClientControllerCommunicator();
// Connect to server
try { try {
// Send login request this.socket = new Socket(ipAddress,port);
// receive true/false
// send ship request
//clientControllerCommunicator.setClientShip();
} }
catch (Exception e){ catch (Exception e){
e.printStackTrace();
} throw new IllegalArgumentException("Couldn't initialize connection to server");
while (true){
sendAndReceive();
} }
} }
} }
package com.galaxytrucker.galaxytruckerreloaded.Server;
import java.net.Socket;
public class ClientHandler implements Runnable {
/** Client socket */
private Socket clientSocket;
/** The server */
public Server server;
/** ServerServiceCommunicator */
private ServerServiceCommunicator serverServiceCommunicator;
/** Constructor
* @param clientSocket - the client's socket
* @param server - the server */
public ClientHandler(Socket clientSocket,Server server){
this.clientSocket = clientSocket;
this.server = server;
}
/** Run */
public void run(){
}
}
package com.galaxytrucker.galaxytruckerreloaded.Server; package com.galaxytrucker.galaxytruckerreloaded.Server;
import com.galaxytrucker.galaxytruckerreloaded.Communication.Client;
import lombok.NonNull;
import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
/** /**
* This class creates the game server and handles storing the data * This class creates the game server and handles storing the data
*/ */
public class Server { public class Server implements Runnable{
/** Server socket for network communication */
private ServerSocket serverSocket;
/** Server thread */
private Thread serverThread;
/** Server service communicator */ /** ServerServiceCommunicator */
private ServerServiceCommunicator serverServiceCommunicator; private ServerServiceCommunicator serverServiceCommunicator;
/** Server port */
private int port;
/** Is the server running? */
private boolean running = true;
/** Main method */ /** Main method */
public static void main(String[] args){} public static void main(String[] args){
Server server = new Server();
server.setPort(5050);
server.serverServiceCommunicator = new ServerServiceCommunicator();
new Thread(server).start();
try {
Thread.sleep(1000);
}
catch (Exception f){
System.out.println(f);
}
Client client = new Client("localhost",5050);
}
/** Client handler /** Start server on specified port
* @param socket - the client's socket */ * @param port - the port to bind */
private void clientHandler(Socket socket){ private void bindPort(int port) {
try {
this.serverSocket = new ServerSocket(port);
}
catch (Exception e){
e.printStackTrace();
}
}
/** Is the server running? */
public synchronized boolean isRunning(){
return this.running;
}
/** Kill server */
public synchronized void killServer(){
try {
this.serverSocket.close();
this.running = false;
}
catch (Exception e){
e.printStackTrace();
}
} }
/** Receive some data from the client and return a response /** Get the serverServiceCommunicator */
* @param socket - the client socket */ public synchronized ServerServiceCommunicator getServerServiceCommunicator(){
private void receiveAndSendData(Socket socket){ return this.serverServiceCommunicator;
} }
/** Run the server */
public void run(){
synchronized (this){
this.serverThread = Thread.currentThread();
}
bindPort(this.port);
System.out.println("Server initialized on " + serverSocket.getInetAddress().getHostAddress() + ":" + this.port + ", listening for connections...");
while (isRunning()){
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
System.out.println("Accepted new connection from "+ clientSocket.getInetAddress().getHostAddress());
}
catch (Exception e){
e.printStackTrace();
}
Server server = this;
new Thread(
new ClientHandler(clientSocket,server)
).start();
}
}
/** Set the server port
* @param port - the server port */
public void setPort(int port){
this.port = port;
}
} }
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