1) The player logs into the game.
2) He can either host a game (something like CS) or to join an existing game that hasnt started.
3) after hosting or joining a game, the player will be directed to a 'game lounge' where everyone is gathered before the game actually starts.
4) once the host is happy with the number of people in each team, he can start the game by pressing the start button.
5) 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();
}
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();
}
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
{
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
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
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
hope some kind souls out there can give me a helping hand coz my deadline is on friday. thanks

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


