daily pastebin goal
68%
SHARE
TWEET

Untitled

a guest Dec 10th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4. public class DatagramServer
  5. {
  6.     public static void main(String[] args) throws IOException
  7.     {
  8.         byte[] buf = new byte[256];
  9.         DatagramSocket socket = new DatagramSocket (4456);
  10.         // receive request
  11.         DatagramPacket packet = new DatagramPacket (buf, buf.length);
  12.         System.out.println ("Waiting for date request");
  13.         socket.receive (packet);
  14.         System.out.println ("Received date request");
  15.         // Send Date & Time
  16.         String dString = new Date().toString();
  17.         buf = dString.getBytes();
  18. // send the response to the client at "address" and "port"
  19.         InetAddress address = packet.getAddress ();
  20.         int port = packet.getPort ();
  21.        
  22.         packet = new DatagramPacket (buf, buf.length, address, port);
  23.         socket.send (packet);
  24.         System.out.println ("Sent date request");
  25.         socket.close ();
  26.     }
  27. }
  28.  
  29. //////////////////////////////////////////////////////////////////////////////////////////
  30.  
  31.  
  32. import java.io.*;
  33. import java.net.*;
  34. import java.util.*;
  35. public class DatagramServer
  36. {
  37.     public static void main(String[] args) throws IOException
  38.     {
  39.         byte[] buf = new byte[256];
  40.         DatagramSocket socket = new DatagramSocket (4456);
  41.         // receive request
  42.         DatagramPacket packet = new DatagramPacket (buf, buf.length);
  43.         System.out.println ("Waiting for date request");
  44.         socket.receive (packet);
  45.         System.out.println ("Received date request");
  46.         // Send Date & Time
  47.         String dString = new Date().toString();
  48.         buf = dString.getBytes();
  49. // send the response to the client at "address" and "port"
  50.         InetAddress address = packet.getAddress ();
  51.         int port = packet.getPort ();
  52.        
  53.         packet = new DatagramPacket (buf, buf.length, address, port);
  54.         socket.send (packet);
  55.         System.out.println ("Sent date request");
  56.         socket.close ();
  57.     }
  58. }
  59.  
  60.  
  61. //////////////////////////////////////////////////////////////////////////////////////////////
  62.  
  63. import java.awt.*;
  64. import java.awt.event.*;    
  65. import javax.swing.*;      
  66. import javax.swing.event.*;
  67. import java.io.*;
  68. import java.net.*;
  69.  
  70. public class UDPDateClient extends UDPDateGUI
  71. {
  72.     public ButtonHandler bHandler;
  73.  
  74.     public UDPDateClient (String title)
  75.     {
  76.         super (title);
  77.         bHandler = new ButtonHandler();
  78.         sendButton.addActionListener( bHandler );
  79.     }
  80.  
  81.     private class ButtonHandler implements ActionListener  
  82.     {  
  83.         public void actionPerformed (ActionEvent event)  //throws IOException
  84.         {
  85.             // get a datagram socket
  86.             try
  87.             {
  88.                 DatagramSocket socket = new DatagramSocket ();
  89.                 // send request
  90.                 byte[] buf = new byte[32];
  91.                 InetAddress address = InetAddress.getByName ("127.0.0.1");
  92.                 DatagramPacket packet = new DatagramPacket (buf, buf.length, address, 4455);
  93.                 System.out.println ("About to request date & time");
  94.                 socket.send(packet);
  95.                 System.out.println ("Sent request for date and time");
  96.                 // get response
  97.                 packet = new DatagramPacket (buf, buf.length);
  98.                 socket.receive (packet);
  99.                 System.out.println ("Received date & time");
  100.                 // display response
  101.                 String received = new String (packet.getData());
  102.                 System.out.println ("Remote Date and Time: " + received);
  103.                 socket.close ();
  104.                 rxArea.setText ("Remote Date and Time: " + received);
  105.             }
  106.             catch (IOException e)
  107.             {
  108.                 System.err.println("Couldn't get I/O for the connection to: 194.81.104.118.");
  109.                 System.exit(1);    
  110.             }
  111.         }
  112.     }
  113.  
  114.     public void run () throws IOException
  115.     {      
  116.     }
  117.  
  118.     public static void main(String[] args) //throws IOException
  119.     {
  120.  
  121.         UDPDateClient f = new UDPDateClient ("Date Client Program");
  122.        
  123.         f.pack ();
  124.         f.show ();
  125.         try
  126.         {
  127.             f.run ();
  128.         }
  129.         catch (IOException e)
  130.         {
  131.             System.exit(1);    
  132.         }
  133.  
  134.     }
  135.  
  136. }
  137.  
  138.  
  139. /////////////////////////////////////////////////////////////////////////////////////////////
  140.  
  141. import java.awt.*;
  142. import java.awt.event.*;
  143. import javax.swing.*;
  144. import javax.swing.event.*;
  145.  
  146. public class UDPDateGUI extends JFrame
  147. {
  148.     public JButton sendButton;
  149.     public JTextArea txArea, rxArea;
  150.     public Container container;
  151.  
  152.     public UDPDateGUI (String title)
  153.     {
  154.         super (title);
  155.         container = getContentPane();
  156.         container.setLayout (new FlowLayout());
  157.         //txArea = new JTextArea (6, 40);
  158.         rxArea = new JTextArea (6, 40);
  159.         sendButton = new JButton ("Send");
  160.         //bHandler = new ButtonHandler ();
  161.         //sendButton.addActionListener (bHandler);
  162.         container.add (rxArea);
  163.         //container.add (txArea);
  164.         container.add (sendButton);
  165.     }
  166. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top