I'm working on creating a multiplayer game, with the client in ActionScript and the server written in Java. I'm using XMLSocket
in the client to connect to the server. The server code looks something like:
ServerSocket server = new ServerSocket(port);
while(true)
{
Socket client = server.accept();
ClientHandlingProcess child = new ClientHandlingProcess(client);
child.start(); // starts new thread for client
}
The client handling process looks something like:
while(true)
{
byte[] arr = new byte[1024];
if(socket.getInputStream().read(arr) == -1)
{
break;
}
handleInputMethod(arr);
while(!message_queue.isEmpty())
{
socket.getOutputStream().write(message_queue.get(0).getBytes());
message_queue.remove(0);
}
}
The issue is that the read
method is blocking, meaning I can't send any data while I'm waiting for data to come in. What would be a good way to fix this? Have two processes: one for reading and one for writing, have the client send empty requests when it has no proper messages to send, or is there another way I can proceed?