Java套接字编程(下)
byte [] buffer = new byte [100]; DatagramPacket dgp = new DatagramPacket (buffer, buffer.length); InetAddress ia = InetAddress.getByName ("www.disney.com"); dgp.setAddress (ia); dgp.setPort (6000); // Send datagram packet to port 6000. |
byte [] buffer = new byte [100]; InetAddress ia = InetAddress.getByName ("www.disney.com"); DatagramPacket dgp = new DatagramPacket (buffer, buffer.length, ia, 6000); |
byte [] buffer2 = new byte [256]; dgp.setData (buffer2); dgp.setLength (buffer2.length); |
import java.io.*; import java.net.*; class DGSClient { public static void main(String[] args) { String host = "localhost"; // If user specifies a command-line argument, that argument // represents the host name. if (args.length == 1) host = args[0]; DatagramSocket s = null; try { // Create a datagram socket bound to an arbitrary port. s = new DatagramSocket(); // Create a byte array that will hold the data portion of a // datagram packet's message. That message originates as a // String object, which gets converted to a sequence of // bytes when String's getBytes() method is called. The // conversion uses the platform's default character set. byte[] buffer = new String("Send me a datagram").getBytes(); // Convert the name of the host to an InetAddress object. // That object contains the IP address of the host and is // used by DatagramPacket. InetAddress ia = InetAddress.getByName(host); // Create a DatagramPacket object that encapsulates a // reference to the byte array and destination address // information. The destination address consists of the // host's IP address (as stored in the InetAddress object) // and port number 10000 -- the port on which the server // program listens. DatagramPacket dgp = new DatagramPacket(buffer, buffer.length, ia, 10000); // Send the datagram packet over the socket. s.send(dgp); // Create a byte array to hold the response from the server. // program. byte[] buffer2 = new byte[100]; // Create a DatagramPacket object that specifies a buffer // to hold the server program's response, the IP address of // the server program's computer, and port number 10000. dgp = new DatagramPacket(buffer2, buffer.length, ia, 10000); // Receive a datagram packet over the socket. s.receive(dgp); // Print the data returned from the server program and stored // in the datagram packet. System.out.println(new String(dgp.getData())); } catch (IOException e) { System.out.println(e.toString()); } finally { if (s != null) { s.close(); } } } }
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; class DGSServer { public static void main(String[] args) throws IOException { System.out.println("Server starting ...\n"); // Create a datagram socket bound to port 10000. Datagram // packets sent from client programs arrive at this port. DatagramSocket s = new DatagramSocket(10000); // Create a byte array to hold data contents of datagram // packet. byte[] data = new byte[100]; // Create a DatagramPacket object that encapsulates a reference // to the byte array and destination address information. The // DatagramPacket object is not initialized to an address // because it obtains that address from the client program. DatagramPacket dgp = new DatagramPacket(data, data.length); // Enter an infinite loop. Press Ctrl+C to terminate program. while (true) { // Receive a datagram packet from the client program. s.receive(dgp); // Display contents of datagram packet. System.out.println(new String(data)); // Echo datagram packet back to client program. s.send(dgp); } } }
import java.io.*; import java.net.*; class MCClient { public static void main(String[] args) throws IOException { // Create a MulticastSocket bound to local port 10000. All // multicast packets from the server program are received // on that port. MulticastSocket s = new MulticastSocket(10000); // Obtain an InetAddress object that contains the multicast // group address 231.0.0.1. The InetAddress object is used by // DatagramPacket. InetAddress group = InetAddress.getByName("127.0.0.1"); // Join the multicast group so that datagram packets can be // received. s.joinGroup(group); // Read several datagram packets from the server program. for (int i = 0; i < 10; i++) { // No line will exceed 256 bytes. byte[] buffer = new byte[256]; // The DatagramPacket object needs no addressing // information because the socket contains the address. DatagramPacket dgp = new DatagramPacket(buffer, buffer.length); // Receive a datagram packet. s.receive(dgp); // Create a second byte array with a length that matches // the length of the sent data. byte[] buffer2 = new byte[dgp.getLength()]; // Copy the sent data to the second byte array. System.arraycopy(dgp.getData(), 0, buffer2, 0, dgp.getLength()); // Print the contents of the second byte array. (Try // printing the contents of buffer. You will soon see why // buffer2 is used.) System.out.println(new String(buffer2)); } // Leave the multicast group. s.leaveGroup(group); // Close the socket. s.close(); } }
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.MulticastSocket; class MCServer { public static void main(String[] args) throws IOException { System.out.println("Server starting...\n"); // Create a MulticastSocket not bound to any port. MulticastSocket s = new MulticastSocket(); // Because MulticastSocket subclasses DatagramSocket, it is // legal to replace MulticastSocket s = new MulticastSocket (); // with the following line. // <script type="text/javascript"></script> // // DatagramSocket s = new DatagramSocket (); // Obtain an InetAddress object that contains the multicast // group address 231.0.0.1. The InetAddress object is used by // DatagramPacket. InetAddress group = InetAddress.getByName("127.0.0.1"); // Create a DatagramPacket object that encapsulates a reference // to a byte array (later) and destination address // information. The destination address consists of the // multicast group address (as stored in the InetAddress object) // and port number 10000 -- the port to which multicast datagram // packets are sent. (Note: The dummy array is used to prevent a // NullPointerException object being thrown from the // DatagramPacket constructor.) byte[] dummy = new byte[0]; DatagramPacket dgp = new DatagramPacket(dummy, 0, group, 10000); // Send 30000 Strings to the port. for (int i = 0; i < 30000; i++) { // Create an array of bytes from a String. The platform's // default character set is used to convert from Unicode // characters to bytes. byte[] buffer = ("Video line " + i).getBytes(); // Establish the byte array as the datagram packet's // buffer. dgp.setData(buffer); // Establish the byte array's length as the length of the // datagram packet's buffer. dgp.setLength(buffer.length); // Send the datagram to all members of the multicast group // that listen on port 10000. s.send(dgp); } // Close the socket. s.close(); } }