package chat; import java.net.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.net.DatagramSocket; class SendThread extends Thread { String username; String address; String message; int port; SendThread(String in_username, String in_address, String in_message, int in_port) { username = in_username; address = in_address; message = in_message; port = in_port; } public void run() { Socket link; Boolean connected = false; PrintWriter out; while(!connected) { try { link = new Socket(address, port); out = new PrintWriter(link.getOutputStream(),true); out.println(username + " : " + message); connected = true; } catch(IOException e) { System.out.println("nik1!"); connected = false; } } } } class ReceiveThread extends Thread { int port; JTextArea displayArea; ReceiveThread(int in_port, JTextArea in_displayArea) { port = in_port; displayArea = in_displayArea; } public void run() { try { ServerSocket servSocket = new ServerSocket(port); Boolean exit = false; while(!exit) { Socket link = servSocket.accept(); BufferedReader in = new BufferedReader(new InputStreamReader(link.getInputStream())); String message = in.readLine(); displayArea.append(message + "\n"); displayArea.setCaretPosition(displayArea.getDocument().getLength()); link.close(); } } catch(IOException e) { System.out.println("nik2 !"); } } } /*class PopupWindow implements ActionListener { JFrame popupWindow; JPanel popupPanel; JButton submitButton; JPanel fieldLine[] = new JPanel[4]; JTextField inputField[] = new JTextField[4]; JLabel fieldLabel[] = new JLabel[4]; PopupWindow() { fieldLabel[0] = new JLabel("Destination IP address"); fieldLabel[1] = new JLabel("Destination port"); fieldLabel[2] = new JLabel("Source port"); fieldLabel[3] = new JLabel("Username"); popupPanel = new JPanel(new GridLayout(5,1)); for(int i = 0;i < 4; i ++) { inputField[i] = new JTextField(); fieldLine[i] = new JPanel(new GridLayout(1,2)); fieldLine[i].add(fieldLabel[i]); fieldLine[i].add(inputField[i]); popupPanel.add(fieldLine[i]); } submitButton = new JButton("OK"); popupPanel.add(submitButton); popupWindow = new JFrame(); popupWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); popupWindow.setSize(new Dimension(640, 480)); popupWindow.getContentPane().add(popupPanel, BorderLayout.CENTER); popupWindow.pack(); popupWindow.setVisible(true); } public void actionPerformed(ActionEvent event) { } }*/ public class ClientWindow implements ActionListener { JFrame chatWindow; JPanel chatPanel; JTextArea chatText; JScrollPane textScroll; JPanel sendPanel; JTextField messageField; JButton sendButton; String address; int sport; int dport; String username; ClientWindow() { chatWindow = new JFrame("Système de clavardage 2.0.1"); chatPanel = new JPanel(new GridLayout(2, 2)); chatText = new JTextArea(1,1); textScroll = new JScrollPane(chatText); sendPanel = new JPanel(new GridLayout(1, 2)); messageField = new JTextField(); sendButton = new JButton("Envoyer"); sendButton.addActionListener(this); messageField.addActionListener(this); sendPanel.add(messageField); sendPanel.add(sendButton); chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); chatWindow.setSize(new Dimension(1024, 768)); chatText.setLineWrap(true); chatText.setEditable(false); chatPanel.add(textScroll); chatPanel.add(sendPanel, BorderLayout.SOUTH); chatWindow.getContentPane().add(chatPanel, BorderLayout.CENTER); //Display the window. address = JOptionPane.showInputDialog(chatWindow, "Enter the destination address", "POPOPOPOPUPUPOPOPUP", JOptionPane.PLAIN_MESSAGE); sport = Integer.parseInt(JOptionPane.showInputDialog(chatWindow, "Enter the source port", "POPOPOPOPUPUPOPOPUP", JOptionPane.PLAIN_MESSAGE)); dport = Integer.parseInt(JOptionPane.showInputDialog(chatWindow, "Enter the destination port", "POPOPOPOPUPUPOPOPUP", JOptionPane.PLAIN_MESSAGE)); username = JOptionPane.showInputDialog(chatWindow, "Enter a username", "POPOPOPOPUPUPOPOPUP", JOptionPane.PLAIN_MESSAGE); chatWindow.pack(); chatWindow.setVisible(true); ReceiveThread t2 = new ReceiveThread(sport, chatText); t2.start(); } public static void main (String [] args) { JFrame.setDefaultLookAndFeelDecorated(true); ClientWindow window = new ClientWindow(); } public void actionPerformed(ActionEvent event) { String message = messageField.getText(); messageField.setText(""); chatText.append(username + " : " + message + "\n"); SendThread t1 = new SendThread(username, address, message, dport); t1.start(); } }