java 如何在java中通过UDP发送一个int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5236620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to send an int through UDP in java
提问by user650309
I'm trying to write a bit of code which sends a single int over UDP. The code I have so far:
我正在尝试编写一些通过 UDP 发送单个 int 的代码。我到目前为止的代码:
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?
问题是接收器正在向屏幕打印“50”,这显然是不对的。我认为问题可能在于我以某种方式将它作为字符串或其他东西发送并且它没有正确阅读。有什么帮助吗?
回答by Ray Tayek
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);
}
}
}
回答by iluxa
InputStream.read() returns a single byte, not a 32-bit integer (see javadoc). So what you want is
InputStream.read() 返回单个字节,而不是 32 位整数(请参阅 javadoc)。所以你想要的是
ObjectInputStream os = new ObjectInputStream(bin);
os.readInt();
回答by Archer
The problem is that you're receiving CHAR CODE of '2' and not acctual 2 as integer. Try changing the your receiver code to:
问题是您收到的 CHAR CODE 是 '2' 而不是 acctual 2 作为整数。尝试将您的接收器代码更改为:
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.
但我猜 ObjectInputStream 解决方案对你来说会更好。