Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help in UDP - buffer?

843790May 10 2009 — edited May 10 2009
So, I'm programming a UDP Server-Client app, but here's the problem:

a) a client conects, and sends the message "conectar#Username" , being Username a getText from a field.
This is ok. Sample 'conectar#Manchester'
b) the server receives it, and ok, everything is fine.
c) if another client try to connect, and it's username is smaller than the previous ones, everything goes wrong...
Sample: 'conectar#Chelsea', becomes 'conectar#Chelseater'

It keeps getting the size of the first one... How can I fix that???

Here's the basic code of the receive and send stuff:

Server:
 while (true) {
                        try {
                            DatagramPacket packet = new DatagramPacket(receiveData, receiveData.length);
                            socket.receive(packet);
                            String sentence = new String(packet.getData());
Clients:
DatagramSocket clientSocket = new DatagramSocket();
        InetAddress IPAddress = InetAddress.getByName("127.0.0.1");
        byte[] sendData = new byte[1024];
        byte[] receiveData = new byte[1024];
        sentence = ("conectar#" + TFUser.getText());
        sendData = sentence.getBytes();
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 5555);
        clientSocket.send(sendPacket);
Anyone can help? Thanks...
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 7 2009
Added on May 10 2009
2 comments
130 views