Game server/client issue

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1912 Views - Last Post: 05 February 2011 - 07:16 PM Rate Topic: -----

#1 Maverick89   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 03-November 10

Game server/client issue

Posted 05 February 2011 - 09:03 AM

Hello everyone,
I'm having a bit of a problem with some send/recieve methods in a game I'm making. The server side works without a problem, it's sending what it's suppose to and the client is receiving, but when a client tries to send coordinates of it's panel to the server side (it's in the same while loop as the receive part) it just stops there and I don't know why. Maybe you can clear it up for me. I have also tried to give the variable a fixed value, just to see if it will send it and it doesn't. It just stops at the same place. Please help, I'm kinda in a rush. I have only 5 days to finish it and this is the only part left. Here is the code :

The server part :

public static void main(String[] argv) throws Exception {
		
		int ballX;
		int ballY;
		int serverPanel;
		int clientPanel;
		ServerSocket welcomeSocket = new ServerSocket(8812);
		System.out.println("Server : Port open, awaiting clients");
		Socket connectionSocket = welcomeSocket.accept();
		
		Main serverGame = new Main (2,"server");
		serverGame.setVisible(true);

		ObjectOutputStream outToClient = new ObjectOutputStream(connectionSocket.getOutputStream());
		ObjectInputStream inFromClient = new ObjectInputStream (connectionSocket.getInputStream());
		while (true) {
			ballX = serverGame.getCoordsFromPanelBallX();
			ballY = serverGame.getCoordsFromPanelBallY();
			serverPanel = serverGame.getCoordsServerPanel();
			
			outToClient.writeInt(ballX);
			outToClient.writeInt(ballY);
			outToClient.writeInt(serverPanel);
			
			clientPanel = inFromClient.readInt();
			serverGame.setServerPanelCoords(clientPanel);
			
		}



Client without the send part :

public class TestClient {
public static void main (String [] argv) throws Exception {
		
		int ballX;
		int ballY;
		int serverPanel;
		int clientY ;
		System.out.println("Client : opening socket");
		Socket clientSocket = new Socket ("localhost", 8812);
		System.out.println("Client : Connected to server");
		System.out.println("Client : Awaiting data from server");
		
			Main gameClient = new Main (2, "client");
			gameClient.setVisible(true);
		
		ObjectInputStream inFromServer = new ObjectInputStream (clientSocket.getInputStream());
		ObjectOutputStream outToServer = new ObjectOutputStream(clientSocket.getOutputStream());
		while (true) {
			ballX = inFromServer.readInt();
			ballY = inFromServer.readInt();
			serverPanel = inFromServer.readInt();
			System.out.println(ballX +"        "+ballY);
			gameClient.setBallCoordsInClient(ballX, ballY);
			System.out.println(serverPanel);
			gameClient.setClientPanelCoords(serverPanel);
			
			
			

			
}
		
	}
}



Client with send part :

public class TestClient {
public static void main (String [] argv) throws Exception {
		
		int ballX;
		int ballY;
		int serverPanel;
		int clientY ;
		System.out.println("Client : opening socket");
		Socket clientSocket = new Socket ("localhost", 8812);
		System.out.println("Client : Connected to server");
		System.out.println("Client : Awaiting data from server");
		
			Main gameClient = new Main (2, "client");
			gameClient.setVisible(true);
		
		ObjectInputStream inFromServer = new ObjectInputStream (clientSocket.getInputStream());
		ObjectOutputStream outToServer = new ObjectOutputStream(clientSocket.getOutputStream());
		while (true) {
			clientY = gameClient.getClientPanel();
			outToServer.writeInt(clientY);
			ballX = inFromServer.readInt();
			ballY = inFromServer.readInt();
			serverPanel = inFromServer.readInt();
			System.out.println(ballX +"        "+ballY);
			gameClient.setBallCoordsInClient(ballX, ballY);
			System.out.println(serverPanel);
			gameClient.setClientPanelCoords(serverPanel);
			
			
			

			
}
		
	}
}



Thanks for the help in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Game server/client issue

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Game server/client issue

Posted 05 February 2011 - 12:12 PM

Seems to me that your I/O are not synchronized

The server loops doing:
- 3 writes of an int from client
- 1 read of an int from client

The client loops doing:
- 3 reads on and int from the server
Was This Post Helpful? 1
  • +
  • -

#3 Maverick89   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 03-November 10

Re: Game server/client issue

Posted 05 February 2011 - 12:21 PM

So how can I fix it ? :/
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Game server/client issue

Posted 05 February 2011 - 12:24 PM

View PostMaverick89, on 05 February 2011 - 02:21 PM, said:

So how can I fix it ? :/

Your client will have to send an int to the server so it won't get stuck on that line
clientPanel = inFromClient.readInt(); 


waiting for the client to send it an int
Was This Post Helpful? 1
  • +
  • -

#5 Maverick89   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 03-November 10

Re: Game server/client issue

Posted 05 February 2011 - 12:29 PM

Isn't it doing that already ? Or maybe I jsut don't understand the problem completely :/ Could you edit my code a bit so it will bypass the issue ?
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Game server/client issue

Posted 05 February 2011 - 12:40 PM

Exchange of informations between a client and a server should be synchronized

For each write performed by the server the client must perform a read
For each write performed by the client the server must perform a read

In your case
   Server           Client
    W int            R int
    W int            R int
    W int            R int
    R int            R int (second loop)


both client and server are waiting a message from the other one
Was This Post Helpful? 2
  • +
  • -

#7 Maverick89   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 03-November 10

Re: Game server/client issue

Posted 05 February 2011 - 12:41 PM

Oh so I have to make them paralel, If my client sends something the server should read it in the next line and vice versa, right ?
Was This Post Helpful? 0
  • +
  • -

#8 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Game server/client issue

Posted 05 February 2011 - 12:44 PM

View PostMaverick89, on 05 February 2011 - 02:41 PM, said:

Oh so I have to make them paralel, If my client sends something the server should read it in the next line and vice versa, right ?

You've got it :) paralel if you want to call it that way
Was This Post Helpful? 0
  • +
  • -

#9 Maverick89   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 03-November 10

Re: Game server/client issue

Posted 05 February 2011 - 12:45 PM

Thank you mate, you saved my ass for the second time since I started this project :) I will try this solution tonight and will report on the outcome. Once again thank you !
Was This Post Helpful? 0
  • +
  • -

#10 Maverick89   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 03-November 10

Re: Game server/client issue

Posted 05 February 2011 - 03:15 PM

I sorted it out a bit, but again I have the same problem. This is the new code :

Server :

public class TestServer {
	public static void main(String[] argv) throws Exception {
		
		int ballX;
		int ballY;
		int serverPanel;
		int clientPanel;
		ServerSocket welcomeSocket = new ServerSocket(8812);
		System.out.println("Server : Port open, awaiting clients");
		Socket connectionSocket = welcomeSocket.accept();
		
		Main serverGame = new Main (2,"server");
		serverGame.setVisible(true);

		ObjectOutputStream outToClient = new ObjectOutputStream(connectionSocket.getOutputStream());
		ObjectInputStream inFromClient = new ObjectInputStream (connectionSocket.getInputStream());
		while (true) {
			
			ballX = serverGame.getCoordsFromPanelBallX();
			ballY = serverGame.getCoordsFromPanelBallY();
			serverPanel = serverGame.getCoordsServerPanel();		
			outToClient.writeInt(ballX);
			outToClient.writeInt(ballY);
			outToClient.writeInt(serverPanel);
			clientPanel = inFromClient.readInt();
			serverGame.setServerPanelCoords(clientPanel);
			
			
		}			
		}
		

}


Client :

public class TestClient {
public static void main (String [] argv) throws Exception {
		
		int ballX;
		int ballY;
		int serverPanel;
		int clientY ;
		System.out.println("Client : opening socket");
		Socket clientSocket = new Socket ("localhost", 8812);
		System.out.println("Client : Connected to server");
		System.out.println("Client : Awaiting data from server");
		
			Main gameClient = new Main (2, "client");
			gameClient.setVisible(true);
		
		ObjectInputStream inFromServer = new ObjectInputStream (clientSocket.getInputStream());
		ObjectOutputStream outToServer = new ObjectOutputStream(clientSocket.getOutputStream());
		while (true) {
			
			ballX = inFromServer.readInt();
			ballY = inFromServer.readInt();
			serverPanel = inFromServer.readInt();
			clientY = gameClient.getClientPanel();
			outToServer.writeInt(clientY);
			gameClient.setBallCoordsInClient(ballX, ballY);
			gameClient.setClientPanelCoords(serverPanel);
			
			
}
		
	}
}

Was This Post Helpful? 0
  • +
  • -

#11 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Game server/client issue

Posted 05 February 2011 - 03:58 PM

Add a System.out.println() in all your methods:
- outToClient()
- outToServer()
- inFromClient()
- inFromServer()

you will see where you go out of synch
Was This Post Helpful? 0
  • +
  • -

#12 Maverick89   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 03-November 10

Re: Game server/client issue

Posted 05 February 2011 - 04:17 PM

When I kill those last 2 lines where the client sends info everything works perfectly. When it's on, nothing works, not even the first line. I don't know what to do :( My best bet is,I can send you the whole thing if you have time and patience to take a look ...
Was This Post Helpful? 0
  • +
  • -

#13 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Game server/client issue

Posted 05 February 2011 - 04:23 PM

View PostMaverick89, on 05 February 2011 - 06:17 PM, said:

:( My best bet is,I can send you the whole thing if you have time and patience to take a look ...

This would defeat the pedagogic approach of this forum :)
Put your println() in and I'll be glad to help you analysing the output
Was This Post Helpful? 0
  • +
  • -

#14 Maverick89   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 03-November 10

Re: Game server/client issue

Posted 05 February 2011 - 04:25 PM

There is no output, like it died on the first line xD Here is the client code where I added the lines, I got nothing as a result, not even 1 number printed out :/ :

while (true) {
			
			ballX = inFromServer.readInt();
			System.out.println(ballX);
			ballY = inFromServer.readInt();
			System.out.println(ballY);
			serverPanel = inFromServer.readInt();
			System.out.println(serverPanel);
			clientY = gameClient.getClientPanel();
			System.out.println(clientY);
			outToServer.writeInt(clientY);
			System.out.println("Sent clientY");
			gameClient.setBallCoordsInClient(ballX, ballY);
			gameClient.setClientPanelCoords(serverPanel);
			
			
}


when I put "clientY = gameClient.getClientPanel();
System.out.println(clientY);
outToServer.writeInt(clientY);"
under commentaries it works like a charm...

This post has been edited by Maverick89: 05 February 2011 - 04:26 PM

Was This Post Helpful? 0
  • +
  • -

#15 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Game server/client issue

Posted 05 February 2011 - 04:48 PM

Put the println() in these methods
- outToClient()
- outToServer()
- inFromClient()
- inFromServer()
:online2long:
not in the code that calls them
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2