gpt4 book ai didi

Java UDP - 数据包发送问题

转载 作者:行者123 更新时间:2023-11-30 05:05:05 31 4
gpt4 key购买 nike

每次我尝试将 UDP 数据包发送到某个地址时,都会出现以下异常:

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)

直接指向套接字发送函数 - this.socket.send(packet);

在数据报包上放置断点后,我得到以下信息:

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

我不清楚为什么会发生这种情况,如果有人能阐明这个问题,那就太好了。我最初的想法是数据报包元素之一可能是空的。

(基本来源)

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 信息:(Ip 正确显示目标计算机)

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

服务器源 http://pastebin.com/JCdjhFQM

最佳答案

我怀疑这与使用缓冲的DataOutputStream,然后传递底层byte[]有关。

在创建数据包之前尝试 out.flush()

关于Java UDP - 数据包发送问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5370010/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com