So no methods within the player class will run unless I call those methods from another class ? or call them in the constructor ? That kind of makes sense. Ill have to see where the player class is being used.
Server Class:
package mypackage;
//Import area
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Server {
private static ServerSocket server; // Define the server socket
private static boolean run = true; //Continuously run server loop Switch
private static HashMap<String, Player> players = new HashMap <String, Player>();// create an array list to store all players
//use threads
public static int player_count = 0; //holds player count
public static void main(String[] args){
try {
System.out.println("Starting Server...");
server = new ServerSocket(1234,10); //New server socket object
System.out.println("Server started on port: " + Integer.toString(server.getLocalPort()));
do{
Socket newConnection = server.accept();
String name = "Guest"+player_count;//Gives each new player a unique name(change later)
Player p = new Player(name,newConnection); //Create a new Player instance and set the socket to it
System.out.println("New player connected: " + name);
players.put(name,p); //add the new player instance to the player HashMap
}while (run);
} catch (IOException e) {}
}
}
Player class:
package mypackage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketTimeoutException;
public class Player {
private DataOutputStream output;//define a data output that we will later use to send messages to the client
private DataInputStream input; //define a data input stream that we will later us to read messages from the client;
private Socket socket;// define a socket
private boolean alive = true; //define a boolean to keep the player class running
private String name; //define a string that will hold the player name
public Player(String nm, Socket sck) //constructor or method ?
{
socket =sck; //set the received socket to the player class
name = nm;// set the player name
try {
socket.setSoTimeout(30000);//Set the socket timeout to 30 seconds
output = new DataOutputStream(socket.getOutputStream()); //get the data output stream and set it to output
output.flush();//flush away all the bytes on the data output stream
input = new DataInputStream(socket.getInputStream()); //get the data input stream and set it to input
} catch(IOException e){}
}
public void run()
{
do
{ //start a do loop while alive is true
checkForMessages();//check for messages
}
while (alive);
}
private void checkForMessages()
{
byte received;
try
{
received = input.readByte();//read the header from the message
}catch(IOException ex){alive = false; return;}
if (received == -34)
{ //header start ?
try {
input.skipBytes(11); //ignore the header
received = input.readByte(); //message ID
switch (received)
{
case 0:
break;
}
}catch (SocketTimeoutException ex)
{
//if we get a socket timoue display it and free the player's instance
System.out.println("Disconnected "+name+ "due to timeout.");
alive =false;
}catch (IOException ex){
ex.printStackTrace();
}
}
}
}
This is all the server code, Is the run method being called anywhere ? I don't see it being called.. from my understanding or from what you've said the methods must be called somewhere before it is run.