hi, i am a newbie to game network programming. recently i am doing a multiplayer game project. The networking part works fine initially. I have a host server to broadcast the game messages out and a game client to receive and inteprete the messages. It works fine when the game is in progress. But when i integrate the ‘out-game’ menus (the menu screen with options like “Start Game”, “Join Game”, etc), there seems to be a problem in the message receiving at my client side when the game is started from the ‘out-game’ menu. here’s a brief description of how the thingie goes:
- The player logs into the game.
- He can either host a game (something like CS) or to join an existing game that hasnt started.
- after hosting or joining a game, the player will be directed to a ‘game lounge’ where everyone is gathered before the game actually starts.
- once the host is happy with the number of people in each team, he can start the game by pressing the start button.
- the client at the host’s side will send a game start message to the hostserver (the host’s side) which will in turn broadcast to every client connected to the host. when the clients receive this msg, the game lounge screen will be closed and the game screen will be opened.
Everything up to this point is alright. But when my game started, the client doesnt seems to receive any message, though it is sending messages to the server. here are the respective code snipplets:
GameClient (when the game start message is received):
[quote] if (clientID.compareTo(id)==0){
test = new GameTest();
test.setPlayer(gamelounge.menumanager.getPlayer());
test.setPlayerPosition(position);
test.setGamemap(gamelounge.menumanager.getGamemap());
gamelounge.dispose();
test.initSystem();
setScene(test.getScene());
sendInGameOpponentMessage(test.getPlayer().getUserID(),test.getPlayer().getIntTeam(),test.getPlayerObject().getPosition(),test.getPlayer().getintAgility(),test.getPlayer().getintMarksmanship());
test.run();
}
[/quote]
The GameClient’s message receiving thread:
[quote] public void run()
{
int messageLength = 0;
String message = “”;
// read messages until server close the connection
try {
// process messages sent from server
do {
readBuffer.clear();
socketChannel.read( readBuffer );
readBuffer.flip();
CharBuffer charMessage = charSet.decode( readBuffer );
message = charMessage.toString().trim();
System.out.println("Server msg: " + message);
messageListener.messageReceived(message);
} while ( true ); // keep receiving messages
} // end try
// catch problems reading from server
catch ( IOException ioException ) {
if ( ioException instanceof ClosedByInterruptException )
System.out.println( "socket channel closed" );
else {
ioException.printStackTrace();
try {
socketChannel.close();
System.out.println( "socket channel closed" );
}
catch ( IOException exception ) {
exception.printStackTrace();
}
}
} // end catch
} // end method run
[/quote]
The hostserver’s message sending code:
[quote] public void writeMessage(String message) throws IOException {
Socket socket;
SocketChannel socketChannel;
System.out.println("Sending: " + message);
// echo message back to all connected clients
for (int i = 0; i < sockets.size(); i++) {
socket = (Socket) sockets.elementAt(i);
socketChannel = socket.getChannel();
// send message to client
try {
// convert message to bytes in charSet
writeBuffer = charSet.encode(message);
// write message to socketChannel
socketChannel.write(writeBuffer);
}
// process problems sending object
catch (IOException ioException) {
ioException.printStackTrace();
socketChannel.close();
sockets.remove(socket);
clientList.remove(i);
}
} // end for
} // end method writeMessage
[/quote]
hope some kind souls out there can give me a helping hand coz my deadline is on friday. thanks ;D
Note: the out-game menu is done in javax.swing while the game itself is done using opengl (lwjgl apis). I am using NIO for the networking