1

Everytime I try to send a UDP packet to an address I get the following exception:

java.lang.ArrayIndexOutOfBoundsException
    at java.net.PlainDatagramSocketImpl.send(Native Method)
    at java.net.DatagramSocket.send(DatagramSocket.java:625)
    at services.servicetypes.network.host.Server_UDP_Thread_Monitor.send(Server_UDP_Thread_Monitor.java:84)

Which points directly to the socket send function - this.socket.send(packet);

After placing a break point on my datagram packet I get the following info:

packet  DatagramPacket  java.net.DatagramPacket@52c4c57 
byte[]  #1248(length=16)    
offset  int 0   
length  int 16  
bufLength   int 16  
address Inet4Address    /192.168.0.101  
Static          
Inherited   
port    int 3889

I'm not clear on why this is occuring, if anyone could shed any light on the problem that would be great. My inital thoughts were perhaps one of the datagram packet elements was empty.

(Basic source)

ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(byteOut);

// Game type
out.writeInt(1);

// Client
out.writeInt(gamerToSend.getClientID());

// Position
out.writeFloat(gamerToSend.getX());
out.writeFloat(gamerToSend.getY());


DatagramPacket packet = new DatagramPacket
(
  byteOut.toByteArray(),
  byteOut.size(),
  gamerToSend.getInetAddress(),
  3887
);

this.socket.send(packet);

byteOut.close();
out.close();

INetAddress info: (Ip is correctly displaying destination machine)

address Inet4Address    /192.168.0.101  /192.168.0.101  
Static      
INADDRSZ    int 4   4   
serialVersionUID    long    3286316764910316507 3286316764910316507 
loopback    int 2130706433  2130706433  
IPv4    int 1   1   
IPv6    int 2   2   
preferIPv6Address   boolean false   false   
nameService InetAddress$1   java.net.InetAddress$1@5bf0cf51 java.net.InetAddress$1@5bf0cf51 
serialVersionUID    long    3286316764910316507 3286316764910316507 
addressCache    InetAddress$Cache   java.net.InetAddress$Cache@1ebafdff java.net.InetAddress$Cache@1ebafdff 
negativeCache   InetAddress$Cache   java.net.InetAddress$Cache@679801c  java.net.InetAddress$Cache@679801c  
addressCacheInit    boolean true    true    
unknown_array   InetAddress[]   #1266(length=1) #1266(length=1) 
impl    Inet6AddressImpl    java.net.Inet6AddressImpl@12c9b196  java.net.Inet6AddressImpl@12c9b196  
lookupTable HashMap "size = 0"  "size = 0"  
$assertionsDisabled boolean true    true    
Inherited               
hostName                
address int -1062731675 -1062731675 
family  int 2   2   
canonicalHostName               
caport  int 3889    3889    

Server source http://pastebin.com/JCdjhFQM

2
  • 1
    You need to post your code so we can see what you're doing. Commented Mar 20, 2011 at 17:10
  • Apologies, forgot, source code added. Commented Mar 20, 2011 at 17:14

3 Answers 3

2

I suspect it has something to do with using the DataOutputStream which is buffered, then passing the underlying byte[].

Try out.flush() before creating your packet.

Sign up to request clarification or add additional context in comments.

8 Comments

I've tried, but same error as before. out.flush(); DatagramPacket packet = new DatagramPacket....
The problem is that obviously, the length isn't matching the byte[] you're passing in. Before your packet, do a byte[] foo = byteOut.toByteArray(); and pass that in with foo.length as the size. Or just compare byteOut.size() to that length and see if there's a difference.
byte[] foo = byteOut.toByteArray(); System.out.println("1~" + byteOut.size()); System.out.println("2~" + foo.length); 1~16 2~16 No difference...
You write 16 byte, but your packet has only 13 byte (from the debugging data you posted).
My bad I added some additional data by mistake. Both now show 16 bytes but the issue remains.
|
0

The error is not in the part you posted. I wrapped your source in an example program, and it works without any problems.

package de.fencing_game.paul.examples;

import java.io.*;
import java.net.*;

/**
 */
public class UDPTest {

    private static class Gamer {
        int clientID;
        float x;
        float y;
        InetAddress address;

        public int getClientID() { return clientID; }
        public float getX() { return x; }
        public float getY() { return y; }
        public InetAddress getInetAddress() { return address; }
    }


    private DatagramSocket socket;


    public UDPTest() throws IOException
    {
        this.socket = new DatagramSocket();
    }


    public void sendPackage(Gamer gamerToSend)
        throws IOException
    {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(byteOut);

        // Game type
        out.writeInt(1);

        // Client
        out.writeInt(gamerToSend.getClientID());

        // Position
        out.writeFloat(gamerToSend.getX());
        out.writeFloat(gamerToSend.getY());

        DatagramPacket packet = new DatagramPacket
            (
                byteOut.toByteArray(),
                byteOut.size(),
                gamerToSend.getInetAddress(),
                3887
            );

        this.socket.send(packet);

        byteOut.close();
        out.close();
    }


    public static void main(String[] ignored)
        throws IOException
    {
        Gamer g = new Gamer();
        g.address = InetAddress.getByName("localhost");

        UDPTest sender = new UDPTest();
        sender.sendPackage(g);
    }

}

What are you doing different?

Comments

0

thanks for your help.

It appears the InetAddress is corrupted somewhere as manually setting it to the remote machine like so:

InetAddress addr = InetAddress.getByName("192.168.0.101");

Works perfectly fine, so I have more debugging to do, but thats the problem.

Again thanks for the assist!

1 Comment

Make sure to accept your own answer so the question is closed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.