diff --git a/CellRenderer.class b/CellRenderer.class
new file mode 100644
index 0000000..082879e
--- /dev/null
+++ b/CellRenderer.class
Binary files differ
diff --git a/CellRenderer.java b/CellRenderer.java
new file mode 100644
index 0000000..0f31fb1
--- /dev/null
+++ b/CellRenderer.java
@@ -0,0 +1,30 @@
+import java.awt.Component;
+
+import javax.swing.JTable;
+import javax.swing.SwingConstants;
+import javax.swing.table.DefaultTableCellRenderer;
+
+/**
+ * This Class is used to overwrite the Default Cell Renderer in the JTable used in CnFrameUDP
+ *
+ * @author trefois
+ */
+public class CellRenderer extends DefaultTableCellRenderer {
+
+ /**
+ * Generated serialID by Eclipse
+ */
+ private static final long serialVersionUID = 6305609864454156868L;
+
+ /**
+ * Overwrites getTableCellRendererComponent
+ *
Puts Horizontal Alignement to CENTER
+ */
+ public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
+ super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
+ row, column);
+ this.setHorizontalAlignment(SwingConstants.CENTER);
+ return this;
+ }
+
+}
diff --git a/CnFrame.class b/CnFrame.class
new file mode 100644
index 0000000..5c50ac8
--- /dev/null
+++ b/CnFrame.class
Binary files differ
diff --git a/CnFrame.java b/CnFrame.java
new file mode 100644
index 0000000..88a26f8
--- /dev/null
+++ b/CnFrame.java
@@ -0,0 +1,53 @@
+import java.awt.BorderLayout;
+import java.awt.Dimension;
+import java.awt.Toolkit;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+
+import javax.swing.JFrame;
+
+/**
+ * This class is the Super Class of CnFrameUDP and CnFrameTCP.
+ * @author trefois
+ *
+ */
+public class CnFrame extends JFrame {
+ /**
+ * Generated serialID by Eclipse
+ */
+ private static final long serialVersionUID = -5235044694247669643L;
+
+ /**
+ * Custom Constructor.
+ * Sets the Window title to windowTitle
+ * @param windowTitle The Title provided as String
+ */
+ public CnFrame (String windowTitle) {
+ this.setTitle(windowTitle);
+ frameConstructor();
+ }
+
+ /**
+ * Construct the main Frame.
+ */
+ private void frameConstructor() {
+ // Set Behaviour when "x" is clicked.
+ setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+
+ // Get Screen Dimensions
+ Dimension windowSize = Toolkit.getDefaultToolkit().getScreenSize();
+ setBounds(50, 50, windowSize.width / 2, windowSize.height / 2);
+
+ getContentPane().setLayout(new BorderLayout());
+ }
+
+ /**
+ * Retrieves the current time in the following format: dd/MMM/yyyy HH:mm:ss
+ * @return The String representation of the current Date
+ */
+ public String getCurrentTime() {
+ DateFormat dateFormat = new SimpleDateFormat("dd / MMMM / yyyy -- HH:mm:ss");
+ java.util.Date date = new java.util.Date();
+ return dateFormat.format(date);
+ }
+}
diff --git a/CnFrameTCP.class b/CnFrameTCP.class
new file mode 100644
index 0000000..6c1099d
--- /dev/null
+++ b/CnFrameTCP.class
Binary files differ
diff --git a/CnFrameTCP.java b/CnFrameTCP.java
new file mode 100644
index 0000000..7f032d7
--- /dev/null
+++ b/CnFrameTCP.java
@@ -0,0 +1,67 @@
+import java.awt.BorderLayout;
+import java.awt.GridLayout;
+
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JTextArea;
+
+/**
+ * This Class dispalys messages received from the Server. E.g. Server Hello etc...
+ * Call appendText(String) to update the GUI
+ *
+ * @author trefois
+ */
+public class CnFrameTCP extends CnFrame {
+
+ /**
+ * Generated serialID by Eclipse
+ */
+ private static final long serialVersionUID = 4899514760671051872L;
+
+ /**
+ * The TextArea used to display the messages
+ */
+ private JTextArea textArea;
+
+ /**
+ * Constructs a CnFrameTCP Instance with title windowTitle
+ * @param windowTitle
+ */
+ public CnFrameTCP(String windowTitle) {
+ super(windowTitle);
+ setSize(500, 250);
+
+ JPanel centerPanel = new JPanel();
+
+ textArea = new JTextArea();
+ textArea.setEditable(false);
+
+ JScrollPane scrollPane = new JScrollPane(textArea);
+
+ centerPanel.setLayout(new GridLayout(1, 1));
+ centerPanel.add(scrollPane);
+
+ // put the centerPanel to the Center
+ getContentPane().add(centerPanel, BorderLayout.CENTER);
+
+ // Make this frame visible
+ setVisible(true);
+ }
+
+ /**
+ * Append Text to the textArea
+ * @param textToAppend
+ */
+ public void appendText(String textToAppend) {
+ textArea.append(super.getCurrentTime() + " : " + textToAppend);
+ textArea.setCaretPosition(textArea.getText().length());
+ }
+
+ /**
+ * Clear the TextArea.
+ *
+ */
+ public void clearTextArea() {
+ textArea.replaceRange("", 0, textArea.getText().length());
+ }
+}
diff --git a/CnFrameUDP.class b/CnFrameUDP.class
new file mode 100644
index 0000000..0a35645
--- /dev/null
+++ b/CnFrameUDP.class
Binary files differ
diff --git a/CnFrameUDP.java b/CnFrameUDP.java
new file mode 100644
index 0000000..2ee6ae5
--- /dev/null
+++ b/CnFrameUDP.java
@@ -0,0 +1,140 @@
+import java.awt.BorderLayout;
+import java.util.Vector;
+
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JTable;
+import javax.swing.table.DefaultTableModel;
+
+
+/**
+ * This Class is used to display UDP Broadcast Messages sent from the Server to the Client.
+ * The Client should create a CnFrameUDP Instance and call the method "appendBroadcastMessage"
+ * each time an UDP Broadcast is received.
+ *
Example:
+ *
+ * CnFrameUDP cnFrameUDP = new CnFrameUDP("Networking Course");
+ * while(true) {
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ cnFrameUDP.appendBroadcastMessage("192.168.1.5", "****X***X****X");
+ }
+ *
+ * @author Christophe Trefois
+ *
+ */
+public class CnFrameUDP extends CnFrame {
+ /**
+ * Generated serialID by Eclipse
+ */
+ private static final long serialVersionUID = 2774784580042117224L;
+
+ /**
+ * Vector containing the table rows data
+ */
+ private Vector tableRows;
+
+ /**
+ * Vector containing the column names. Filled with addColumns.
+ */
+ private Vector columns;
+
+ /**
+ * Array containing the column names
+ */
+ private static final String[] columnNames = {"Received At", "IP Address", "Letters"};
+
+ /**
+ * tableModel used by the Table
+ */
+ private DefaultTableModel tableModel;
+
+ /**
+ * The table used to display the broadcast messages
+ */
+ private JTable broadcastTable;
+
+ /**
+ * Constructs a CnFrameUDP Instance with title windowTitle
+ * @param windowTitle The Title if the window
+ */
+ public CnFrameUDP(String windowTitle) {
+ super(windowTitle);
+
+ // Create new JPanel
+ JPanel centerPanel = new JPanel();
+
+ // Fill the columns Vector
+ addColumns(columnNames);
+
+ tableModel = new DefaultTableModel();
+ tableModel.setDataVector(tableRows, columns);
+
+ // create new JPanel
+ broadcastTable = new JTable(tableModel);
+ broadcastTable.getColumnModel().getColumn(0).setCellRenderer(new CellRenderer());
+ broadcastTable.getColumnModel().getColumn(1).setCellRenderer(new CellRenderer());
+ broadcastTable.getColumnModel().getColumn(2).setCellRenderer(new CellRenderer());
+
+ // Add ScrollBar to the broadcastTable
+ JScrollPane scrollPane = new JScrollPane(broadcastTable);
+ centerPanel.add(scrollPane);
+
+ // put the centerPanel to the Center
+ getContentPane().add(centerPanel, BorderLayout.CENTER);
+
+ // Pack
+ pack();
+
+ // Make this frame visible
+ setVisible(true);
+ }
+
+ /**
+ * Fill the column name vector
+ * @param columnNames the array of Strings to fill the vector with
+ */
+ private void addColumns(String[] columnNames) {
+ // Init tableRows and columns Vectors
+ tableRows = new Vector();
+ columns = new Vector();
+
+ for (int i = 0; i < columnNames.length; i++)
+ columns.addElement((String) columnNames[i]);
+ }
+
+ /**
+ * Adds a row to the JTable
+ * @param broadcastIP The IP from the broadcast message
+ * @param letters The Letters
+ */
+ private void addRow(String broadcastIP, String letters) {
+ Vector tempVector = new Vector();
+ tempVector.addElement(super.getCurrentTime());
+ tempVector.addElement(broadcastIP);
+ tempVector.addElement(letters);
+ tableRows.addElement(tempVector);
+ broadcastTable.addNotify();
+ }
+
+ /**
+ * Modifies the GUI by appending the broadcast Message
+ * @param broadcastIP the IP extracted from the broadcast UDP Message
+ * @param broadcastLetters the letters from the UDP Broadcast
+ */
+ public void appendBroadcastMessage(String broadcastIP, String broadcastLetters) {
+ addRow(broadcastIP, broadcastLetters);
+ }
+
+ /**
+ * Clears the Table
+ *
+ */
+ public void clearTable() {
+ tableRows.clear();
+ broadcastTable.addNotify();
+ }
+}
diff --git a/TCPClient.class b/TCPClient.class
new file mode 100644
index 0000000..64b79bb
--- /dev/null
+++ b/TCPClient.class
Binary files differ
diff --git a/TCPClient.java b/TCPClient.java
new file mode 100644
index 0000000..fdf96c2
--- /dev/null
+++ b/TCPClient.java
@@ -0,0 +1,65 @@
+import java.net.*;
+import java.io.*;
+
+public class TCPClient {
+
+ private Socket sock;
+
+ private String targethost;
+
+ private int targetport;
+
+ private String name;
+
+ private BufferedReader inr;
+ private DataOutputStream outs;
+
+ private CnFrameTCP gui;
+
+
+ public static void main ( String[] args ) {
+ CnFrameTCP gui = new CnFrameTCP("TP 4: TCP Part");
+
+ try {
+ TCPClient tc = new TCPClient(gui, "in3sun23.epfl.ch", 13370, "x-way");
+
+ tc.join();
+
+ tc.leave();
+
+ } catch ( IOException ioe ) {
+ System.out.println("IO-Error");
+ gui.appendText("IO-Error");
+ }
+ }
+
+ TCPClient ( CnFrameTCP gui, String targethost, int targetport, String name ) throws IOException {
+ this.gui = gui;
+ this.targethost = targethost;
+ this.targetport = targetport;
+ this.name = name;
+
+ try {
+ sock = new Socket(targethost, targetport);
+ } catch ( UnknownHostException uhe ) {
+ System.out.println("Unknown host");
+ gui.appendText("Unknown host");
+ }
+
+ outs = new DataOutputStream(sock.getOutputStream());
+
+ inr = new BufferedReader(new InputStreamReader(sock.getInputStream()));
+ }
+
+ public void join ( ) throws IOException {
+ outs.writeBytes("Hello: " + name + "\n");
+ System.out.println(inr.readLine());
+ gui.appendText(inr.readLine());
+ }
+
+ public void leave ( ) throws IOException {
+ sock.close();
+ System.out.println("Connection closed.");
+ gui.appendText("Connection closed.");
+ }
+}
diff --git a/UDPServer.class b/UDPServer.class
new file mode 100644
index 0000000..9484dcd
--- /dev/null
+++ b/UDPServer.class
Binary files differ
diff --git a/UDPServer.java b/UDPServer.java
new file mode 100644
index 0000000..c50670e
--- /dev/null
+++ b/UDPServer.java
@@ -0,0 +1,55 @@
+import java.net.*;
+import java.io.*;
+import java.nio.*;
+
+public class UDPServer {
+ private DatagramSocket sock;
+ private CnFrameUDP gui;
+
+ public static void main ( String[] args ) {
+ CnFrameUDP gui = new CnFrameUDP("Title of the window");
+ UDPServer udpS = new UDPServer(gui);
+ udpS.loop();
+ }
+
+ UDPServer ( CnFrameUDP gui ) {
+ this.gui = gui;
+ try {
+ sock = new DatagramSocket(13371);
+ } catch ( SocketException se ) {
+ System.out.println("Couldn't create socket.");
+ }
+ }
+
+ public void loop ( ) {
+ int buffsize = 4096;
+ byte[] buff = new byte[buffsize];
+ byte[] head = new byte[3];
+ byte[] addr = new byte[4];
+ byte[] load = new byte[buffsize-7];
+ DatagramPacket rp;
+ String data;
+
+ while ( true ) {
+ rp = new DatagramPacket(buff, buff.length);
+ try {
+ sock.receive(rp);
+ } catch ( IOException ioe ) {
+ System.out.println("IO-Error");
+ }
+
+ ByteBuffer bb = ByteBuffer.wrap(rp.getData());
+ bb.get(head);
+ bb.get(addr);
+ bb.get(load);
+
+ //System.out.println(rp.getAddress().toString() + ":" + Integer.toString(rp.getPort()) + " > " + new String(load));
+ try {
+ System.out.println(InetAddress.getByAddress(addr).toString() + " > " + new String(load).trim());
+ gui.appendBroadcastMessage(InetAddress.getByAddress(addr).toString(), new String(load).trim());
+ } catch ( UnknownHostException uhe ) {
+ System.out.println("Errornous packet address");
+ }
+ }
+ }
+}