java中如何通过UDP发送int

发布于 2024-10-21 01:23:10 字数 1067 浏览 7 评论 0原文

我正在尝试编写一些通过 UDP 发送单个 int 的代码。到目前为止我的代码:

发送者:

int num = 2;

DatagramSocket socket = new DatagramSocket();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
PrintStream pout = new PrintStream( bout );
pout.print(num);
byte[] barray = bout.toByteArray();
DatagramPacket packet = new DatagramPacket( barray, barray.length );
InetAddress remote_addr = InetAddress.getByName("localhost");           
packet.setAddress( remote_addr );
packet.setPort(1989);
socket.send( packet );

接收者:

        DatagramSocket socket = new DatagramSocket(1989);
        DatagramPacket packet = new DatagramPacket(new byte[256] , 256);

        socket.receive(packet);

        ByteArrayInputStream bin = new ByteArrayInputStream(packet.getData());

        for (int i=0; i< packet.getLength(); i++)
        {
        int data = bin.read();
        if(data == -1)
        break;
        else
        System.out.print((int) data);

问题是接收者在屏幕上打印“50”,这显然是不正确的。我认为问题可能是我以某种方式将它作为字符串或其他东西发送,但它读取不正确。有什么帮助吗?

I'm trying to write a bit of code which sends a single int over UDP. The code I have so far:

Sender:

int num = 2;

DatagramSocket socket = new DatagramSocket();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
PrintStream pout = new PrintStream( bout );
pout.print(num);
byte[] barray = bout.toByteArray();
DatagramPacket packet = new DatagramPacket( barray, barray.length );
InetAddress remote_addr = InetAddress.getByName("localhost");           
packet.setAddress( remote_addr );
packet.setPort(1989);
socket.send( packet );

Receiver:

        DatagramSocket socket = new DatagramSocket(1989);
        DatagramPacket packet = new DatagramPacket(new byte[256] , 256);

        socket.receive(packet);

        ByteArrayInputStream bin = new ByteArrayInputStream(packet.getData());

        for (int i=0; i< packet.getLength(); i++)
        {
        int data = bin.read();
        if(data == -1)
        break;
        else
        System.out.print((int) data);

The problem is the receiver is printing '50' to screen which is obviously not right. I think that the problem may be that I'm somehow sending it as a string or something and its not reading it right. Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

勿挽旧人 2024-10-28 01:23:10

使用数据流,例如:

import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        final DataOutputStream dataOut = new DataOutputStream(byteOut);
        dataOut.writeInt(1);
        dataOut.writeDouble(1.2);
        dataOut.writeLong(4l);
        dataOut.close(); // or dataOut.flush()
        final byte[] bytes = byteOut.toByteArray();
        final ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
        final DataInputStream dataIn = new DataInputStream(byteIn);
        final int integ = dataIn.readInt();
        final double doub = dataIn.readDouble();
        final long lon = dataIn.readLong();
        System.out.println(integ);
        System.out.println(doub);
        System.out.println(lon);
    }

}

Use data streams like:

import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        final DataOutputStream dataOut = new DataOutputStream(byteOut);
        dataOut.writeInt(1);
        dataOut.writeDouble(1.2);
        dataOut.writeLong(4l);
        dataOut.close(); // or dataOut.flush()
        final byte[] bytes = byteOut.toByteArray();
        final ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
        final DataInputStream dataIn = new DataInputStream(byteIn);
        final int integ = dataIn.readInt();
        final double doub = dataIn.readDouble();
        final long lon = dataIn.readLong();
        System.out.println(integ);
        System.out.println(doub);
        System.out.println(lon);
    }

}

你另情深 2024-10-28 01:23:10

InputStream.read() 返回单个字节,而不是 32 位整数(请参阅 javadoc)。所以你想要的是

ObjectInputStream os = new ObjectInputStream(bin);
os.readInt();

InputStream.read() returns a single byte, not a 32-bit integer (see javadoc). So what you want is

ObjectInputStream os = new ObjectInputStream(bin);
os.readInt();
孤者何惧 2024-10-28 01:23:10

问题是您收到的是“2”的字符代码,而不是实际的 2 作为整数。尝试将您的接收器代码更改为:

    DatagramSocket socket = new DatagramSocket(1989);
    DatagramPacket packet = new DatagramPacket(new byte[256] , 256);

    socket.receive(packet);

    System.out.print(new String(packet.getData()));

但我猜 ObjectInputStream 解决方案更适合您。

The problem is that you're receiving CHAR CODE of '2' and not acctual 2 as integer. Try changing the your receiver code to:

    DatagramSocket socket = new DatagramSocket(1989);
    DatagramPacket packet = new DatagramPacket(new byte[256] , 256);

    socket.receive(packet);

    System.out.print(new String(packet.getData()));

But ObjectInputStream solution would work better for you I guess.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文