Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class DatagramServer
- {
- public static void main(String[] args) throws IOException
- {
- byte[] buf = new byte[256];
- DatagramSocket socket = new DatagramSocket (4456);
- // receive request
- DatagramPacket packet = new DatagramPacket (buf, buf.length);
- System.out.println ("Waiting for date request");
- socket.receive (packet);
- System.out.println ("Received date request");
- // Send Date & Time
- String dString = new Date().toString();
- buf = dString.getBytes();
- // send the response to the client at "address" and "port"
- InetAddress address = packet.getAddress ();
- int port = packet.getPort ();
- packet = new DatagramPacket (buf, buf.length, address, port);
- socket.send (packet);
- System.out.println ("Sent date request");
- socket.close ();
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class DatagramServer
- {
- public static void main(String[] args) throws IOException
- {
- byte[] buf = new byte[256];
- DatagramSocket socket = new DatagramSocket (4456);
- // receive request
- DatagramPacket packet = new DatagramPacket (buf, buf.length);
- System.out.println ("Waiting for date request");
- socket.receive (packet);
- System.out.println ("Received date request");
- // Send Date & Time
- String dString = new Date().toString();
- buf = dString.getBytes();
- // send the response to the client at "address" and "port"
- InetAddress address = packet.getAddress ();
- int port = packet.getPort ();
- packet = new DatagramPacket (buf, buf.length, address, port);
- socket.send (packet);
- System.out.println ("Sent date request");
- socket.close ();
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////////
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.*;
- import java.io.*;
- import java.net.*;
- public class UDPDateClient extends UDPDateGUI
- {
- public ButtonHandler bHandler;
- public UDPDateClient (String title)
- {
- super (title);
- bHandler = new ButtonHandler();
- sendButton.addActionListener( bHandler );
- }
- private class ButtonHandler implements ActionListener
- {
- public void actionPerformed (ActionEvent event) //throws IOException
- {
- // get a datagram socket
- try
- {
- DatagramSocket socket = new DatagramSocket ();
- // send request
- byte[] buf = new byte[32];
- InetAddress address = InetAddress.getByName ("127.0.0.1");
- DatagramPacket packet = new DatagramPacket (buf, buf.length, address, 4455);
- System.out.println ("About to request date & time");
- socket.send(packet);
- System.out.println ("Sent request for date and time");
- // get response
- packet = new DatagramPacket (buf, buf.length);
- socket.receive (packet);
- System.out.println ("Received date & time");
- // display response
- String received = new String (packet.getData());
- System.out.println ("Remote Date and Time: " + received);
- socket.close ();
- rxArea.setText ("Remote Date and Time: " + received);
- }
- catch (IOException e)
- {
- System.err.println("Couldn't get I/O for the connection to: 194.81.104.118.");
- System.exit(1);
- }
- }
- }
- public void run () throws IOException
- {
- }
- public static void main(String[] args) //throws IOException
- {
- UDPDateClient f = new UDPDateClient ("Date Client Program");
- f.pack ();
- f.show ();
- try
- {
- f.run ();
- }
- catch (IOException e)
- {
- System.exit(1);
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.*;
- public class UDPDateGUI extends JFrame
- {
- public JButton sendButton;
- public JTextArea txArea, rxArea;
- public Container container;
- public UDPDateGUI (String title)
- {
- super (title);
- container = getContentPane();
- container.setLayout (new FlowLayout());
- //txArea = new JTextArea (6, 40);
- rxArea = new JTextArea (6, 40);
- sendButton = new JButton ("Send");
- //bHandler = new ButtonHandler ();
- //sendButton.addActionListener (bHandler);
- container.add (rxArea);
- //container.add (txArea);
- container.add (sendButton);
- }
- }
Add Comment
Please, Sign In to add comment