My client code:
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();
}
}
My server code:
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;
}
}
}
}
}
I like to send an int to the Server (Listener) which will then convert the byte[] into an int then the server will compute "int num -2" and send it back to the client who ewill compute the same so "num -2" UNTILL the num is = or < than 0;
EXAMPLE
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.
You can simply call a utility function to convert byte array to int.
public static int byteArrayToInt(byte[] b) {
return b[3] & 0xFF | (b[2] & 0xFF) << 8 | (b[1] & 0xFF) << 16 |(b[0] & 0xFF) << 24;
}
Hope this will help.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.