I added the ability to copy on the server side. To do so, I couldn't use JOptionPane. In lieu of this, you'll notice I've put a JTextField in a JFrame. Also, for headless machines, the server prints out messages on the console.
For the client, I've added a proper GUI for input, that accepts pasted text and what nots. Let me know what you think.
package Reminder;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class DistributedReminderServer implements Runnable {
private static DistributedReminderServer server;
public static DistributedReminderServer getInstance() {
if (server == null) {
server = new DistributedReminderServer();
}
return server;
}
private static ServerSocket socket;
private DistributedReminderServer() {
if (System.getProperty("port") == null) { System.setProperty("port", "0"); }
try {
socket = new ServerSocket(Integer.parseInt(System.getProperty("port")));
socket.setReuseAddress(true);
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(e.hashCode());
}
System.err.println("Server bound to "+socket.getLocalPort());
}
public void run() {
String ourString = "";
while (true) {
Socket client = null;
try {
client = socket.accept();
} catch (IOException e) {
System.err.println(e.getMessage());
}
System.err.println("Connection Received from "+client.getRemoteSocketAddress());
StringBuffer notification = new StringBuffer();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
while ((ourString = in.readLine()) != null) {
notification.append(ourString);
}
in.close();
System.err.println(notification.toString());
} catch (IOException e) {
System.err.println(e.getMessage());
}
System.setProperty("reminder.notificationMessage", notification.toString());
DistributedReminderGUI g = new DistributedReminderGUI();
new Thread(g).start();
}
}
public static void main (String[] args) {
DistributedReminderServer s = DistributedReminderServer.getInstance();
Thread t= new Thread(s);
t.start();
}
} // end DistributedReminderServer
package Reminder;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class DistributedReminderClient {
static final JFrame frame = new JFrame("Remind");
static Dimension dims = null;
public DistributedReminderClient() {
}
private static JPanel addPanel(String label, JComponent field) {
final JPanel labelFieldPanel = new JPanel();
labelFieldPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
final JLabel labelField = new JLabel(label);
Dimension dims = new Dimension(123, 16);
labelField.setPreferredSize(dims);
labelFieldPanel.add(labelField);
labelFieldPanel.add(field);
return labelFieldPanel;
}
public static void displayForm() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
final JTextField hostname;
final JTextField port;
final JTextArea notification;
JButton goButton = new JButton("Do it!");
hostname = new JTextField(80);
port = new JTextField(80);
notification = new JTextArea(5, 80);
frame.add(addPanel("Remote Hostname: ", hostname));
frame.add(addPanel("Remote Port: ", port));
frame.add(addPanel("Message to send: ", new JScrollPane(notification)));
goButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.setProperty("hostname", hostname.getText());
System.setProperty("port", port.getText());
System.setProperty("message", notification.getText());
frame.dispose();
sendMessage();
}});
frame.add(goButton);
frame.pack();
frame.setVisible(true);
}
public static void task () {
String hostname = "";
int port = 0;
String notification = "";
try {
hostname = System.getProperty("hostname");
port = Integer.parseInt(System.getProperty("port"));
notification = System.getProperty("message");
} catch (java.lang.NumberFormatException e) {
displayForm();
} catch (NullPointerException e) {
displayForm();
}
sendMessage();
}
public static void sendMessage() {
Socket client = new Socket();
try {
client.connect(new InetSocketAddress(System.getProperty("hostname"),Integer.parseInt(System.getProperty("port"))));
PrintStream out = new PrintStream(client.getOutputStream());
out.println(System.getProperty("message"));
out.flush();
out.close();
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
public static void main (String[] args) {
task();
}
} // end DistributedReminderClient
package Reminder;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class DistributedReminderGUI implements Runnable {
public DistributedReminderGUI() {
}
public void run() {
try {
Toolkit.getDefaultToolkit().beep();
JFrame frame = new JFrame();
JTextField notificationField = new JTextField();
notificationField.setText(System.getProperty("reminder.notificationMessage"));
notificationField.setEditable(false);
frame.add(notificationField);
frame.pack();
frame.setVisible(true);
} catch (java.awt.HeadlessException e) {
System.err.println(System.getProperty("reminder.notificationMessage"));
}
}
}



Leave a comment