338 lines
8.8 KiB
Java
338 lines
8.8 KiB
Java
package chat;
|
||
|
||
import java.net.*;
|
||
|
||
import javax.swing.*;
|
||
|
||
import java.awt.*;
|
||
import java.awt.event.*;
|
||
|
||
import java.io.*;
|
||
import java.util.*;
|
||
|
||
import chat.User;
|
||
|
||
|
||
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 ConnectionListenerThread extends Thread {
|
||
|
||
int port;
|
||
JTextArea displayArea;
|
||
User user;
|
||
|
||
ConnectionListenerThread(User in_user)
|
||
{
|
||
user = in_user;
|
||
}
|
||
public void run()
|
||
{
|
||
Boolean exit = false;
|
||
byte[] buffer = new byte[100];
|
||
DatagramPacket request = new DatagramPacket(buffer, buffer.length);
|
||
DatagramPacket responsePacket1;
|
||
DatagramPacket responsePacket2;
|
||
|
||
DatagramSocket requestSocket;
|
||
DatagramSocket responseSocket;
|
||
|
||
ByteArrayOutputStream bStream1;
|
||
ByteArrayOutputStream bStream2;
|
||
ObjectOutput oo1;
|
||
ObjectOutput oo2;
|
||
|
||
String username;
|
||
|
||
byte[] response1, response2;
|
||
try
|
||
{
|
||
requestSocket = new DatagramSocket(1234);
|
||
responseSocket = new DatagramSocket();
|
||
bStream1 = new ByteArrayOutputStream();
|
||
bStream2 = new ByteArrayOutputStream();
|
||
oo1 = new ObjectOutputStream(bStream1);
|
||
oo2 = new ObjectOutputStream(bStream2);
|
||
|
||
while(!exit)
|
||
{
|
||
try
|
||
{
|
||
System.out.println("Waiting for connection request");
|
||
requestSocket.receive(request);
|
||
System.out.println("Received a request!");
|
||
username = new String(request.getData(), 0, request.getLength());
|
||
InetAddress clientAddress= request.getAddress();
|
||
|
||
oo1.writeObject(user.getUsers());
|
||
response1 = bStream1.toByteArray();
|
||
responsePacket1 = new DatagramPacket(response1, response1.length, clientAddress, 1337);
|
||
responseSocket.send(responsePacket1);
|
||
|
||
oo2.writeObject(user.getHosts());
|
||
response2 = bStream2.toByteArray();
|
||
responsePacket2 = new DatagramPacket(response2, response2.length, clientAddress, 1338);
|
||
responseSocket.send(responsePacket2);
|
||
|
||
if(!user.findUser(username))
|
||
user.add_to_known_users(username, clientAddress.getHostAddress());
|
||
|
||
}
|
||
catch(SocketTimeoutException e)
|
||
{
|
||
|
||
}
|
||
}
|
||
}
|
||
catch(Exception e)
|
||
{
|
||
System.out.println("nik!!");
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
public class ClientWindow implements ActionListener {
|
||
String getLocalIP() {
|
||
String ip = "";
|
||
try(final DatagramSocket socket = new DatagramSocket()){
|
||
socket.connect(InetAddress.getByName("8.8.8.8"), 10002);
|
||
ip = socket.getLocalAddress().getHostAddress();
|
||
}
|
||
catch(Exception e)
|
||
{
|
||
System.out.println("niiiiiiiikkk");
|
||
}
|
||
return ip;
|
||
}
|
||
|
||
JFrame chatWindow;
|
||
|
||
JPanel chatPanel;
|
||
JTextArea chatText;
|
||
JScrollPane textScroll;
|
||
|
||
JPanel sendPanel;
|
||
JTextField messageField;
|
||
JButton sendButton;
|
||
|
||
User user = new User("");
|
||
|
||
ClientWindow()
|
||
{
|
||
|
||
String username = "";
|
||
Boolean connected = false;
|
||
DatagramSocket connectionSocket;
|
||
DatagramSocket userListSocket;
|
||
DatagramSocket hostListSocket;
|
||
|
||
DatagramPacket connectionRequest;
|
||
DatagramPacket userListPacket;
|
||
DatagramPacket hostListPacket;
|
||
|
||
ObjectInputStream userListStream;
|
||
ObjectInputStream hostListStream;
|
||
|
||
ArrayList<String> userList;
|
||
ArrayList<String> hostList;
|
||
|
||
byte[] buffer1 = new byte[20000];
|
||
byte[] buffer2 = new byte[20000];
|
||
|
||
|
||
while (!connected)
|
||
{
|
||
username = JOptionPane.showInputDialog(chatWindow,
|
||
"Enter a username",
|
||
"POPUPOPOPUPUPOPOPUP",
|
||
JOptionPane.PLAIN_MESSAGE);
|
||
try
|
||
{
|
||
connectionSocket = new DatagramSocket();
|
||
userListSocket = new DatagramSocket(1337);
|
||
hostListSocket = new DatagramSocket(1338);
|
||
|
||
userListSocket.setSoTimeout(500);
|
||
hostListSocket.setSoTimeout(500);
|
||
|
||
connectionRequest = new DatagramPacket(username.getBytes(), username.length(),
|
||
InetAddress.getByName("192.168.1.255"), 1234);
|
||
System.out.println("Sending connection request");
|
||
connectionSocket.send(connectionRequest);
|
||
|
||
try
|
||
{
|
||
userListPacket = new DatagramPacket(buffer1, buffer1.length);
|
||
hostListPacket = new DatagramPacket(buffer2, buffer2.length);
|
||
|
||
System.out.println("Waiting for reply");
|
||
userListSocket.receive(userListPacket);
|
||
hostListSocket.receive(hostListPacket);
|
||
|
||
userListStream = new ObjectInputStream(new ByteArrayInputStream(userListPacket.getData()));
|
||
hostListStream = new ObjectInputStream(new ByteArrayInputStream(hostListPacket.getData()));
|
||
|
||
userList = (ArrayList<String>) userListStream.readObject();
|
||
hostList = (ArrayList<String>) hostListStream.readObject();
|
||
|
||
System.out.println(userList.size() + " users currently connected");
|
||
|
||
if (userList.indexOf(username) != -1)
|
||
connected = false;
|
||
else
|
||
{
|
||
for(int i = 0;i < userList.size();i ++)
|
||
{
|
||
System.out.println(hostList.get(i));
|
||
user.add_to_known_users(userList.get(i), hostList.get(i));
|
||
}
|
||
user.add_to_known_users(username, InetAddress.getLocalHost().getHostAddress());
|
||
connected = true;
|
||
}
|
||
}
|
||
catch(SocketTimeoutException e)
|
||
{
|
||
user.add_to_known_users(username, getLocalIP());
|
||
System.out.println("Reply timed out");
|
||
connected = true;
|
||
}
|
||
}
|
||
catch (SocketException e2)
|
||
{
|
||
System.out.println(e2.getMessage());
|
||
}
|
||
catch (Exception e3)
|
||
{
|
||
System.out.println(e3.getMessage());
|
||
}
|
||
}
|
||
user.setpseudo(username);
|
||
|
||
chatWindow = new JFrame("Syst<EFBFBD>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.
|
||
chatWindow.pack();
|
||
chatWindow.setVisible(true);
|
||
|
||
ReceiveThread t2 = new ReceiveThread(1237, chatText);
|
||
ConnectionListenerThread t3 = new ConnectionListenerThread(user);
|
||
t2.start();
|
||
t3.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("");
|
||
|
||
for(String address:user.getHosts())
|
||
{
|
||
SendThread t1 = new SendThread(user.getpseudo(), address, message, 1237);
|
||
t1.start();
|
||
}
|
||
}
|
||
}
|