1

We are making an somewhat RTS networked game in java. i have this main server that accepts other players which has the serversocket. Then on our game when you created your own game room i filtered all the players that has joined my room.then when the game starts the creator of the room should be the host. should i be still using my main server or should i establish a new serversocket for those who are connected to my room? and 1 more thing should a inputstream.readObject() what for an message to go for another loop?or it continuously looping? here is the sample code snippet for the inputstream.

 public void run() {
    while (running) {
        try {
            inStream = new ObjectInputStream(client.getInputStream());
            command = (String) inStream.readObject();

            Thread.sleep(10);
        }//try
        catch (Exception e) {
            e.printStackTrace();
        }//catch
    }//while
}//run

////accepting new client

while (running) {
        try {
            clientConnecting = serverSocket.accept();
            new TCPServerHandle(clientConnecting).start();
                            Thread.sleep(10);
        }//try
        catch (Exception e) {
            e.printStackTrace();
        }//catch
   }//while
the bosxcz
  • 91
  • 3
  • 13

2 Answers2

0

Using blocking I/O with Object I/O streams aren't optimal conditions for an RTS because you don't want other clients to wait during the login process of another client. You might be thinking that you could just multi-thread everything to avoid the wait but it wouldn't make much of a difference because there are still blocking read/write operations. Also, with Object I/O streams, all objects sent have to be serialized first (known as serialization), which could be a pretty lengthy process depending on your user-base. If there are a lot of players, literally every millisecond counts. You should use non-blocking I/O (such as NIO) along with ByteBuffers. I would suggest looking at an NIO tutorial instead, this is a very detailed tutorial on how to make a simple server-client application.

Josh M
  • 11,611
  • 7
  • 39
  • 49
  • thanks Josh for a quick reply. ill be starting to read the tutorial. for my 2nd question is it possible to have another serversocket(different port) even if iam still connected to another serversocket? – the bosxcz Aug 29 '13 at 17:13
  • Sure, I wouldn't see why not. I know that if you try creating another server (with the same port) it'll throw an exception. But why would you need to create more than 1 `ServerSocket`? – Josh M Aug 29 '13 at 17:14
  • the first serversocket will hold all the players in the lobby and the second serversocket will be for the host in the game. I have read some articles about implementing network in RTS games they said that a combination of TCP and UDP would be great since there are important data(current life etc) and not that important data(position). using this method(code snippet above) is the only way i know to implement TCP or am i wrong? – the bosxcz Aug 29 '13 at 17:20
  • Yes that is possible. Incoming packets are demultiplexed to the correct process, using port numbers. You can set multiple TCPServerSockets to listen and accept incoming connection requests on different ports. EDIT: I don't see any code where you implement a TCP connection? – Daniel Mac Aug 29 '13 at 17:22
  • this is the snippet of accepting new clients. and the code snippet above is the receive of data. but it is looping continuously. should it suppose to wait for a data to be receive?but it happens to loop and i have an EOF exception – the bosxcz Aug 29 '13 at 17:26
  • That is the standard way of creating a TCPServer yes. – Daniel Mac Aug 29 '13 at 17:30
  • i get an error in inputstream it says java.io.EOFException at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2298) it seems to loop even if i doesnt send any data to it – the bosxcz Aug 29 '13 at 17:32
0

You could deffinitely create a second ServerSocket for the "party" to communicate with the host. Incoming packets are demultiplexed to the correct process, using port numbers. You can set multiple TCPServerSockets to listen and accept incoming connection requests on different ports.

ServerSocket welcomeSocket = new ServerSocket(portNumber);
Socket clientSocket = welcomeSocket.accept();

And yes, it is in many cases more efficient to use a combination of TCP and UDP, because as you mention some data is more critical than other. UDP only provides a best effort service, where packets can get lost. If you want to setup a UDP socket:

DatagramSocket UDPSocket = new DatagramSocket();
Daniel Mac
  • 400
  • 2
  • 14
  • thanks for the answer @Daniel Mac, i already got my UDP networking. i am having a hard time with TCP. could i connect to my own serversocket? because i want the host to be a client as well since he/she is a player too. i can't seem to find where im wrong at(code snippet of inputstream). it seems looping even if there is no data received that is why i have an EOF exception.have any idea why it is continuously looping in the inputstream? – the bosxcz Aug 29 '13 at 17:43
  • Yes, you can receive packets from your own ServerSocket. Im a little baffled the way you are handling imcoming data packets on your server. Have a look at this example: http://systembash.com/content/a-simple-java-tcp-server-and-tcp-client/ – Daniel Mac Aug 29 '13 at 17:47
  • im sorry about my structure.i am still stuck at inputStream. but thanks for your answers. – the bosxcz Aug 29 '13 at 17:52