Client.java
// Fig. 28.9: Client.java // Client side of connectionless client/server computing with datagrams. import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class Client extends JFrame { private JTextField enterField; // for entering messages private JTextArea displayArea; // for displaying messages private DatagramSocket socket; // socket to connect to server // set up GUI and DatagramSocket public Client() { super("Client"); enterField = new JTextField("Type message here"); enterField.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { try // create and send packet { // get message from textfield String message = event.getActionCommand(); displayArea.append("\nSending packet containing: " + message + "\n"); byte[] data = message.getBytes(); // convert to bytes // create sendPacket DatagramPacket sendPacket = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), 5000); socket.send(sendPacket); // send packet displayArea.append("Packet sent\n"); displayArea.setCaretPosition( displayArea.getText().length()); } catch (IOException ioException) { displayMessage(ioException + "\n"); ioException.printStackTrace(); } } } ); add(enterField, BorderLayout.NORTH); displayArea = new JTextArea(); add(new JScrollPane(displayArea), BorderLayout.CENTER); setSize(400, 300); // set window size setVisible(true); // show window try // create DatagramSocket for sending and receiving packets { socket = new DatagramSocket(); } catch (SocketException socketException) { socketException.printStackTrace(); System.exit(1); } } // wait for packets to arrive from Server, display packet contents public void waitForPackets() { while (true) { try // receive packet and display contents { byte[] data = new byte[100]; // set up packet DatagramPacket receivePacket = new DatagramPacket( data, data.length); socket.receive(receivePacket); // wait for packet // display packet contents displayMessage("\nPacket received:" + "\nFrom host: " + receivePacket.getAddress() + "\nHost port: " + receivePacket.getPort() + "\nLength: " + receivePacket.getLength() + "\nContaining:\n\t" + new String(receivePacket.getData(), 0, receivePacket.getLength())); } catch (IOException exception) { displayMessage(exception + "\n"); exception.printStackTrace(); } } } // manipulates displayArea in the event-dispatch thread private void displayMessage(final String messageToDisplay) { SwingUtilities.invokeLater( new Runnable() { public void run() // updates displayArea { displayArea.append(messageToDisplay); } } ); } public static void main(String[] args) { Client application = new Client(); // create client application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); application.waitForPackets(); // run client application } }
Server.java
// Fig. 28.7: Server.java // Server side of connectionless client/server computing with datagrams. import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.SwingUtilities; public class Server extends JFrame { private JTextArea displayArea; // displays packets received private DatagramSocket socket; // socket to connect to client // set up GUI and DatagramSocket public Server() { super("Server"); displayArea = new JTextArea(); // create displayArea add(new JScrollPane(displayArea), BorderLayout.CENTER); setSize(400, 300); // set size of window setVisible(true); // show window try // create DatagramSocket for sending and receiving packets { socket = new DatagramSocket(5000); } catch (SocketException socketException) { socketException.printStackTrace(); System.exit(1); } } // wait for packets to arrive, display data and echo packet to client public void waitForPackets() { while (true) { try // receive packet, display contents, return copy to client { byte[] data = new byte[100]; // set up packet DatagramPacket receivePacket = new DatagramPacket(data, data.length); socket.receive(receivePacket); // wait to receive packet // display information from received packet displayMessage("\nPacket received:" + "\nFrom host: " + receivePacket.getAddress() + "\nHost port: " + receivePacket.getPort() + "\nLength: " + receivePacket.getLength() + "\nContaining:\n\t" + new String(receivePacket.getData(), 0, receivePacket.getLength())); sendPacketToClient(receivePacket); // send packet to client } catch (IOException ioException) { displayMessage(ioException + "\n"); ioException.printStackTrace(); } } } // echo packet to client private void sendPacketToClient(DatagramPacket receivePacket) throws IOException { displayMessage("\n\nEcho data to client..."); // create packet to send DatagramPacket sendPacket = new DatagramPacket( receivePacket.getData(), receivePacket.getLength(), receivePacket.getAddress(), receivePacket.getPort()); socket.send(sendPacket); // send packet to client displayMessage("Packet sent\n"); } // manipulates displayArea in the event-dispatch thread private void displayMessage(final String messageToDisplay) { SwingUtilities.invokeLater( new Runnable() { public void run() // updates displayArea { displayArea.append(messageToDisplay); // display message } } ); } public static void main(String[] args) { Server application = new Server(); // create server application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); application.waitForPackets(); // run server application } }
Maintained by John Loomis, updated Sun Apr 09 17:20:31 2017