Java UDP - Packet Send Problem
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
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.
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?
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!
精彩评论