/* * DGSServer.java * * Created on 18 luty 2008, 09:41 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package sclient; /** * * @author */ // DGSServer.java import java.io.*; import java.net.*; 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); } } }
/* * DSClient.java * * Created on 18 luty 2008, 09:39 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package sclient; /** * * @author */ // DGSClient.java 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; 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 (); } } }