///// Java 用 DatagramSocket 做 UDP 接收/傳送 (connectionless) magpie:network/> cat -n UDPReceive2.java 1 // UDPReceive2.java 2 // This example is from the book _Java in a Nutshell_ by David Flanagan. 3 // Written by David Flanagan. Copyright (c) 1996 O'Reilly & Associates. 4 // You may study, use, modify, and distribute this example for any purpose. 5 // This example is provided WITHOUT WARRANTY either expressed or implied. 6 // Modified by tsaiwn@csie.nctu.edu.tw to fix International problem 7 8 import java.io.*; 9 import java.net.*; 10 // This program waits to receive Datagrams sent to port 6010. 11 // When it receives one, it displays the sending host and port, 12 // and prints the contents of the Datagram as a string. 13 /// /// classes used: DatagramSocket, DatagramPacket 14 public class UDPReceive2 { 15 static final int port = 6010; 16 public static void main(String args[]) throws Exception 17 { 18 byte[] buffer = new byte[1600]; 19 String s; 20 // Create a socket to listen on the port. (use receive(packet); ) 21 DatagramSocket socket = new DatagramSocket(port); 22 23 for(;;) { // Create a packet with an empty buffer to receive data 24 // Bug workaround: create a new packet each time through the loop. 25 // If we create the packet outside of this loop, then it seems to 26 // loose track of its buffer size, and incoming packets are 27 // truncated. 28 DatagramPacket packet = new DatagramPacket(buffer, buffer.length); 29 socket.receive(packet); // Wait to receive a Datagram 30 // Convert the contents to a string; note the Charset problem 31 ///s = new String(buffer, 0, 0, packet.getLength()); //deprecated 32 //s = new String(buffer, 0, packet.getLength(),"Big5"); //by tsaiwn 33 s = new String(buffer, 0, packet.getLength()); // default Charset 34 // Note that 網路上流動是 Byte Stream, Not character 35 // And display them on the Console 36 System.out.println("UDPReceive: received from " + 37 packet.getAddress().getHostName() + ":" + 38 packet.getPort() + ": " + s); 39 } // for 40 } // main 41 } magpie:network/> cat -n UDPSend2.java 1 // UDPSend2.java 2 // This example is from the book _Java in a Nutshell_ by David Flanagan. 3 // Written by David Flanagan. Copyright (c) 1996 O'Reilly & Associates. 4 // You may study, use, modify, and distribute this example for any purpose. 5 // This example is provided WITHOUT WARRANTY either expressed or implied. 6 // Modified by tsaiwn@csie.nctu.edu.tw to solve International problem 7 8 import java.io.*; 9 import java.net.*; 10 11 // This class sends the specified text as a datagram to port 6010 of the 12 // specified host. Note: use quote "if the message contains special char " 13 // Note: 若訊息(message)有空白或特殊字符請用雙引號"夾住" 14 15 public class UDPSend2 { 16 static final int port = 6010; 17 public static void main(String args[]) throws Exception { 18 if (args.length != 2) { 19 System.out.println("Usage: java UDPSend host message"); 20 System.exit(0); 21 } 22 23 // Get the internet address of the specified host 24 InetAddress address = InetAddress.getByName(args[0]); 25 26 // Convert the message to an array of bytes 27 int msglen = args[1].length(); // character length, not byte Len 28 byte[] message = new byte[msglen+msglen]; // double by tsaiwn 29 //args[1].getBytes(0, msglen, message, 0); 30 message = args[1].getBytes(); //use Default charset, by tsaiwn@csie 31 32 // Initilize the packet with data and address 33 DatagramPacket packet = new DatagramPacket(message, 34 message.length, address, port); //good form, tsaiwn 35 // Create a socket, and send the packet through it. 36 DatagramSocket socket = new DatagramSocket(); 37 socket.send(packet); // send it out anyway 38 } // main 39 }