0

EDIT I have it working now thanks to the comments below. I also explained what I fixed in the comments. Thanks for the help guys.

Im working on a multiplayer game in java. It's coming along pretty well so far, but Im having an issue with the server sending information to the client. The process should be that, the server receives information from the client and interprets what it's supposed to do. In this case, the client sends a chat message to the server split with commas. "chat,Bob,the message is here." At this point in time, the server should essentially send back that same information to the client that sent the message. Somehow, along the way though, the ByteBuffer which is what is housing the information gets corrupted?

The following is the pertinent code for the server:

                    // Read the data
                    SocketChannel sc = (SocketChannel) key.channel();

                    // interpret
                    int bytesEchoed = 0;
                    while (true) {
                        //Clears this buffer.
                        echoBuffer.clear();

                        int number_of_bytes;
                        String message = new String(echoBuffer.array());
                        String[] splits = message.split(",");
                        try {
                            number_of_bytes = sc.read(echoBuffer);
                        } catch (java.io.IOException e) {
                            key.cancel();
                            number_of_bytes = -1;
                        }
                        //-----------Interpret Packets--------------------

                        //-------------Chat-----------------
                        if (splits[0].contentEquals("chat")) {
                            //do chat shit
                            String name = splits[1];
                            String text = splits[2];
                            String sendBack = "chat," + name + "," + text + ","+"\r";
                            System.out.println(sendBack);
                            if (splits[0].equals("chat")) {
                                echoBuffer.clear();
                                echoBuffer.put(sendBack.getBytes());
                            }
                        }

                        //

                        if (number_of_bytes <= 0) {
                            break;
                        }

                        //
                        //

                        echoBuffer.flip();
                        sc.write(echoBuffer);
                        bytesEchoed += number_of_bytes;
                    }

                    System.out.println("Echoed " + bytesEchoed + " from " + sc);

                    // once a key is handled, it needs to be removed
                    it.remove();
                }

            }
        }
    }

Can anyone tell me what I am messing up?

  • You've posted way too much code. Cut what is not relevant. If you don't know what is relevant, use a debugger. – Sotirios Delimanolis Jun 26 '13 at 18:19
  • Use debugger. You can sorted out – Ruchira Gayan Ranaweera Jun 26 '13 at 18:21
  • I suggest that client is sending this stuff and your server is just echoing it correctly. NB If you get an IOException you should close the channel, not cancel the key. – user207421 Jun 26 '13 at 22:07
  • I didnt know how to debug, so I googled it, and stepped through the program with netbeans debugger. I wasn't clearing before I was putting the sendBack string, and that was adding the text to the end of the buffer, instead of the begining. Also, on the client side I was using readLine() to get the incoming data, but I there was no carriage return or new line on the outgoing server data, resulting in my reading nothing. Those two things fixed, have it working properly. – user2348436 Jun 27 '13 at 01:54
  • In other words you were sending the information twice from the client, so the bug wasn't in the server code you posted. – user207421 Jun 27 '13 at 10:44
  • It was two bugs. Both on server. Server wasn't clearing the buffer before writing to it. The buffer also did not contain an end of line or carriage return that the client needed for readLine() to function properly. Both were issues in the code I posted above. – user2348436 Jun 29 '13 at 21:57

1 Answers1

0

I wasn't doing clear() before I was putting the sendBack string to the bytebuffer, and that was adding the text to the end of the buffer, instead of the beginning. Also, on the client side I was using readLine() to get the incoming data, but there was no carriage return "\r" or new line "\n" on the outgoing server data, resulting in my client reading nothing. Those two things fixed, have it working properly.