I have developed a client-server game (called "SET") in java.
During the debugging process I ran into a very awkward problem:
If i run both the client and the server on the same machine (client connects to localhost), the game works fantastically (also if i run the server and a lot of clients).
But, if i run the client and the server on 2 separate machines, then both the client and the server hang in thir Inputstream readLine method.
I will mention that i'm using writeBytes method to write the data, but i always finish a data line with \n (as mentioned, the systems works perfectly on 1 machine !)
The architecture of the system is as follows:
public SetServer(){
this.deck = initCardsList();
Collections.shuffle(this.deck);
shownCards = new Card[12];
for(int i = 0; i<12; i++){
Card c = this.deck.removeFirst();
shownCards[i]=c;
}
while(!isSetOnTable()){
Card c = this.deck.removeFirst();
this.deck.addLast(shownCards[0]);
shownCards[0]=c;
}
playersQueue = new LinkedList<String>();
clients = new LinkedList<ServerOperation>();
try{
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true)
{
if(currNumOfPlayers<5){
System.out.println("Waiting for connection...");
Socket connectionSocket = welcomeSocket.accept();
String line = (new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()))).readLine();
currNumOfPlayers++;
playersQueue.addLast(line);
ServerOperation client = new ServerOperation(connectionSocket,this, line);
clients.add(client);
Thread t = new Thread(client);
t.start(); //<--- This thread listens to client's request
notifyPlayersAdded(line,new DataOutputStream(connectionSocket.getOutputStream())); //<-----This method sends 3 lines of data
}
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
The ServerOperation's run method:
public void run(){
if(socket==null){
return;
}
else{
try{
// this.out.writeBytes("NewConnection:" + this.parent.STATUS+"\n");
// this.parent.notifyCards();
while(true){
String line = this.br.readLine();
String command = line.split(":")[0];
if(command.equals("SetRequest")){
String[] cards = line.substring(line.indexOf(':')+1).split(" ");
Card c1,c2,c3;
c1 = new Card(
CardShape.values()[(int)(cards[0].charAt(3)-'0')],
CardNumber.values()[(int)(cards[0].charAt(2)-'0')],
CardFill.values()[(int)(cards[0].charAt(1)-'0')],
CardColor.values()[(int)(cards[0].charAt(0)-'0')]);
c2 = new Card(
CardShape.values()[(int)(cards[1].charAt(3)-'0')],
CardNumber.values()[(int)(cards[1].charAt(2)-'0')],
CardFill.values()[(int)(cards[1].charAt(1)-'0')],
CardColor.values()[(int)(cards[1].charAt(0)-'0')]);
c3 = new Card(
CardShape.values()[(int)(cards[2].charAt(3)-'0')],
CardNumber.values()[(int)(cards[2].charAt(2)-'0')],
CardFill.values()[(int)(cards[2].charAt(1)-'0')],
CardColor.values()[(int)(cards[2].charAt(0)-'0')]);
this.parent.checkIfSetAndNotify(c1,c2,c3,this.playerId);
}
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
Client's code:
public SetClient()
{
String sentence;
this.myCards = new LinkedList<Card>();
try{
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
clientSocket = new Socket("10.0.0.3", 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
outToServer.flush();
String name = sayHelloAndGetUserName();
outToServer.writeBytes(name);
outToServer.flush();
sentence = inFromServer.readLine();
sayWelcome(sentence);
ClientOperation clnOper = new ClientOperation(clientSocket,this);
Thread t = new Thread(clnOper);
t.start(); //<---this thread listens to the server for messages (suppose
//to catch the first 3 lines. In practice, catches only the first one.
while(!GameOver)
{
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //<-----listens to the user's input
String line = br.readLine();
String[] choices = line.split(" ");
int c1 = Integer.parseInt(choices[0]);
int c2 = Integer.parseInt(choices[1]);
int c3 = Integer.parseInt(choices[2]);
sendSetMessage(outToServer,c1,c2,c3);
}
catch(Exception ex){
System.out.println("Error listening to system.in...");
}
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
ClientOperation's code:
public void run()
{
if(socket==null){
return;
}
else{
try{
this.br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
String line = this.br.readLine();
String command = line.split(":")[0];
String value = line.split(":")[1];
if (command.equalsIgnoreCase("Deck"))
{
ConvertStringToCards(value);
this.parent.PopulateCards(shownCards);
}
else if (command.equalsIgnoreCase("Points"))
{
System.out.println("Points: " + value);
}
else if(command.equalsIgnoreCase("NewPlayer")){
System.out.println(value + " has joined the game !\n");
}
else if(command.equalsIgnoreCase("Hint")){
this.parent.printHint(ConvertStringToHint(value));
}
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}
In general, what could be the reason that a client server project will work perfectly on 1 machine, but will hang in 2 separate machines?
P.S. when I am debugging step by step the client and the server on separate machines, it also works perfectly.
Thanks in advance,
If it's not an issue of localhost, then most likely if check for firewall if it is present disable and check again, secondly run netstat to check whether your server is listening on all interfaces not only on loopback.
On linux you can try:
it will probably also work in windows power shell. If server is running/listening on your external ip and client still cannot connect and firewall is disable install wireshark and look what is going on on the network.