package Socket; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.util.Arrays; import java.util.Date; publicclassDemo09P7S{ publicstaticvoidmain(String[] args)throws IOException { // The byte array for sending and receiving datagram packets byte[] buf = newbyte[1024]; byte[] sendbuf = newbyte[1024]; // Create a packet for receiving data with DatagramPacket(buffer, length) DatagramPacket recvPacket = null; // Create a packet for sending data with DatagramPacket DatagramPacket sendPacket = null; // Create a server socket with DatagramSocket(port) DatagramSocket socket = new DatagramSocket(8000); // Print information about server started System.out.println("Server started on port 8000, " + new Date()); while (true) { // Initialize buffer for each iteration with Arrays.fill() Arrays.fill(buf, (byte)0); Arrays.fill(sendbuf,(byte)0); // Assign data to the received packet recvPacket = new DatagramPacket(buf, buf.length); // Receive radius from the client socket in a packet, call receive() socket.receive(recvPacket); // Print the client hostname and port number with DatagramPacket.getAddress/getPort sendPacket = new DatagramPacket(sendbuf, sendbuf.length); sendPacket.setAddress(recvPacket.getAddress()); sendPacket.setPort(recvPacket.getPort()); System.out.println("Received packet from "); // use Double.parseDouble to transfer byte[] to double double radius = Double.parseDouble(new String(buf)); System.out.println("Radius: " + radius); // Compute area double area = radius * radius * Math.PI; System.out.println("Area is " + area + '\n'); // Send area to the client in a packet, with DatagramPacket.setAddress/setPort/setData/send sendbuf = Double.valueOf(area).toString().getBytes();
// TODO 鍙戦�佺殑鏁扮粍鍙兘瓒呰繃涓�涓狣ouble澶у皬锛屾鏌ユ敹绔槸鍚︽纭帴鏀�
// Assign data to the packet sending to client with setData sendPacket.setData(sendbuf); // send the packet socket.send(sendPacket); System.out.println("Area sent."); } // end while } // end main }
import java.io.*; import java.net.*; import java.util.*; publicclassDemo09P7C{ publicstaticvoidmain(String[] args){ // The byte array for sending and receiving datagram packets byte[] buf = newbyte[1024]; // Server InetAddress InetAddress server = null; // The packet sent to the server DatagramPacket sendpacket = null; // The packet received from the server DatagramPacket recvpacket = null;
// get a DatagramSocket socket try (DatagramSocket socket = new DatagramSocket()){ // get the Server address with InetAddress.getByName server=InetAddress.getByName("10.128.227.70"); // Create a send packet with DatagramPacket(buffer, length, InetAddress, port) // Or create DatagramPacket with buffer and length, with server address/port set later // Create a receive packet with DatagramPacket(buffer, length) recvpacket= new DatagramPacket(buf, buf.length); // Initialize buffer for each iteration with Arrays.fill Arrays.fill(buf, (byte)0);
// 输入圆的半径 with Scanner System.out.println("Please input radius : "); Scanner scanner = new Scanner(System.in); double r = scanner.nextDouble(); // Transfer double number to byte[] with Double.valueOf() and String.getBytes() buf=Double.valueOf(r).toString().getBytes(); // assign the byte[] buf to packet with DatagramPacket.setData sendpacket = new DatagramPacket(buf, buf.length,server,8000); // send the packet with DatagramSocket.send(DatagramPacket) int cnt = 0; while(cnt<1000)socket.send(sendpacket); // receive area from the server in a packet with DatagramSocket.receive(DatagramPacket) buf = newbyte[1024]; Arrays.fill(buf, (byte) 0); recvpacket = new DatagramPacket(buf, buf.length); socket.receive(recvpacket); // Transfer byte[] to double with Double.parseDouble and String(byte[]) double area=Double.parseDouble(new String(buf)); // Display radius and area. System.out.println("Radius is " + r + "\n"); System.out.println("Area received from the server is " + area + '\n'); scanner.close(); } catch (IOException ex) { ex.printStackTrace(); } } }
Code -1: Request has been terminated
Possible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc. [N/A GET https://avoscloud.com/1.1/classes/Comment]
v1.3.10