[英]How do I convert a ByteArray into int via UDP Datagram
我的客户代码:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.util.Scanner;
public class UDPClient {
public static void main (String[] args) throws IOException{
DatagramSocket ds = new DatagramSocket();
System.out.println("Insert number: ");
Scanner s= new Scanner(System.in);
int num = s.nextInt();
byte[] byteSend = ByteBuffer.allocate(4).putInt(num).array();
InetAddress address = InetAddress.getByName("localhost");
int port = 1999;
DatagramPacket dpSend = new DatagramPacket(byteSend, byteSend.length , address ,port);
ds.send(dpSend);
byte[] byteReceive = new byte[1];
byte[] byteAck = "*".getBytes();
DatagramPacket dpReceive = new DatagramPacket(byteReceive, byteReceive.length);
DatagramPacket dpAck = new DatagramPacket(byteAck, byteAck.length , address, port);
for (int i=1; i<=num; i++){
ds.receive(dpReceive);
String randomString = new String(dpReceive.getData());
System.out.println(randomString);
ds.send(dpAck);
}
ds.close();
}
}
我的服务器代码:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.ByteBuffer;
public class UDPServer {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(1999);
while (true) {
byte[] byteReceive = new byte[4]; //size of type int: 4 bytes
DatagramPacket dp = new DatagramPacket(byteReceive, byteReceive.length);
ds.receive(dp);
int num = ByteBuffer.wrap(dp.getData()).getInt();
InetAddress address = dp.getAddress();
int port = dp.getPort();
byte[] byteSend = "&&&&&".getBytes();
Byte b = rno[0]; // Boxing conversion converts `byte` to `Byte`
int i = b.intValue();
DatagramPacket dpSend = new DatagramPacket(byteSend, byteSend.length, address, port);
byte[] byteAck = new byte[1];
DatagramPacket dpAck = new DatagramPacket(byteAck, byteAck.length);
for (int i = 1; i <= num; i++) {
ds.send(dpSend);
ds.receive(dpAck);
String stringAck = new String(dpAck.getData());
if (!"*".equals(stringAck)) {
System.out.println("Error!");
return;
}
}
}
}
}
我喜欢向服务器(侦听器)发送一个 int,然后将 byte[] 转换为 int,然后服务器将计算“int num -2”并将其发送回客户端,后者将计算相同的结果,因此“num - 2" 直到 num 为 = 或 < 小于 0;
例子
1 run the server
2 run the client
3 digit 10
4 received back from server 8
5 client will subtract also 2 from 8 which now will be 6
6 send back 6 to server
7 server will subtract also 2 from 6 which now will be 4
8 send back 4 to client
9 client will subtract also 2 from 4 which now will be 2
10 client send back 2 to server
11 server will subtract also 2 from 2 which now will be 0
12 client receive 0
13 when client receive num =0 or num < 0 (0 or negative) program shut down.
您可以简单地调用一个实用函数将字节数组转换为 int。
public static int byteArrayToInt(byte[] b) {
return b[3] & 0xFF | (b[2] & 0xFF) << 8 | (b[1] & 0xFF) << 16 |(b[0] & 0xFF) << 24;
}
希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.