WIP système de connection à plusieurs utilisateurs
This commit is contained in:
parent
5dd3f1d0da
commit
a745f39e0c
1 changed files with 79 additions and 43 deletions
|
@ -107,8 +107,8 @@ class ConnectionListenerThread extends Thread {
|
|||
DatagramSocket requestSocket;
|
||||
DatagramSocket responseSocket;
|
||||
|
||||
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
|
||||
ObjectOutput oo = new ObjectOutputStream(bStream);
|
||||
ByteArrayOutputStream bStream;
|
||||
ObjectOutput oo;
|
||||
|
||||
String username;
|
||||
|
||||
|
@ -117,6 +117,8 @@ class ConnectionListenerThread extends Thread {
|
|||
{
|
||||
requestSocket = new DatagramSocket(1234);
|
||||
responseSocket = new DatagramSocket();
|
||||
bStream = new ByteArrayOutputStream();
|
||||
oo = new ObjectOutputStream(bStream);
|
||||
|
||||
while(!exit)
|
||||
{
|
||||
|
@ -128,12 +130,12 @@ class ConnectionListenerThread extends Thread {
|
|||
|
||||
oo.writeObject(user.getUsers());
|
||||
response1 = bStream.toByteArray();
|
||||
responsePacket1 = new DatagramPacket(response1, response1.length, clientAddress, 1235);
|
||||
responsePacket1 = new DatagramPacket(response1, response1.length, clientAddress, 1337);
|
||||
responseSocket.send(responsePacket1);
|
||||
|
||||
oo.writeObject(user.getHosts());
|
||||
response2 = bStream.toByteArray();
|
||||
responsePacket2 = new DatagramPacket(response2, response2.length, clientAddress, 1236);
|
||||
responsePacket2 = new DatagramPacket(response2, response2.length, clientAddress, 1338);
|
||||
responseSocket.send(responsePacket2);
|
||||
if(!user.findUser(username))
|
||||
user.add_to_known_users(username, clientAddress.toString());
|
||||
|
@ -164,11 +166,11 @@ public class ClientWindow implements ActionListener {
|
|||
JTextField messageField;
|
||||
JButton sendButton;
|
||||
|
||||
User user;
|
||||
User user = new User("");
|
||||
|
||||
ClientWindow()
|
||||
{
|
||||
String username;
|
||||
String username = "";
|
||||
Boolean connected = false;
|
||||
DatagramSocket connectionSocket;
|
||||
DatagramSocket userListSocket;
|
||||
|
@ -182,46 +184,74 @@ public class ClientWindow implements ActionListener {
|
|||
ObjectInputStream hostListStream;
|
||||
|
||||
ArrayList<String> userList;
|
||||
ArrayList<String> hostList;
|
||||
|
||||
username = JOptionPane.showInputDialog(chatWindow,
|
||||
"Enter a username",
|
||||
"POPOPOPOPUPUPOPOPUP",
|
||||
JOptionPane.PLAIN_MESSAGE);
|
||||
ArrayList<String> hostList;
|
||||
|
||||
byte[] buffer1 = new byte[20000];
|
||||
byte[] buffer2 = new byte[20000];
|
||||
|
||||
|
||||
while (!connected)
|
||||
{
|
||||
connectionSocket = new DatagramSocket();
|
||||
userListSocket = new DatagramSocket(1235);
|
||||
hostListSocket = new DatagramSocket(1236);
|
||||
|
||||
connectionRequest = new DatagramPacket(username.getBytes(), username.length(),
|
||||
InetAddress.getByName("192.168.1.255"), 1234);
|
||||
|
||||
username = JOptionPane.showInputDialog(chatWindow,
|
||||
"Enter a username",
|
||||
"POPUPOPOPUPUPOPOPUP",
|
||||
JOptionPane.PLAIN_MESSAGE);
|
||||
try
|
||||
{
|
||||
userListStream = new ObjectInputStream(new ByteArrayInputStream(userListPacket.getData()));
|
||||
hostListStream = new ObjectInputStream(new ByteArrayInputStream(hostListPacket.getData()));
|
||||
|
||||
userList = (ArrayList<String>) userListStream.readObject();
|
||||
hostList = (ArrayList<String>) hostListStream.readObject();
|
||||
|
||||
if (userList.indexOf(username) != -1)
|
||||
connected = false;
|
||||
else
|
||||
{
|
||||
for(int i = 0;i < userList.size();i ++)
|
||||
{
|
||||
user.add_to_known_users(userList.get(i), hostList.get(i));
|
||||
}
|
||||
connected = true;
|
||||
}
|
||||
}
|
||||
catch(SocketTimeoutException e)
|
||||
{
|
||||
connected = true;
|
||||
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();
|
||||
|
||||
if (userList.indexOf(username) != -1)
|
||||
connected = false;
|
||||
else
|
||||
{
|
||||
for(int i = 0;i < userList.size();i ++)
|
||||
{
|
||||
user.add_to_known_users(userList.get(i), hostList.get(i));
|
||||
}
|
||||
connected = true;
|
||||
}
|
||||
}
|
||||
catch(SocketTimeoutException e)
|
||||
{
|
||||
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ème de clavardage 2.0.1");
|
||||
|
||||
|
@ -256,8 +286,10 @@ public class ClientWindow implements ActionListener {
|
|||
chatWindow.pack();
|
||||
chatWindow.setVisible(true);
|
||||
|
||||
ReceiveThread t2 = new ReceiveThread(sport, chatText);
|
||||
ReceiveThread t2 = new ReceiveThread(1237, chatText);
|
||||
ConnectionListenerThread t3 = new ConnectionListenerThread(user);
|
||||
t2.start();
|
||||
t3.start();
|
||||
}
|
||||
public static void main (String [] args)
|
||||
{
|
||||
|
@ -269,8 +301,12 @@ public class ClientWindow implements ActionListener {
|
|||
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();
|
||||
chatText.append(user.getpseudo() + " : " + message + "\n");
|
||||
|
||||
for(String address:user.getHosts())
|
||||
{
|
||||
SendThread t1 = new SendThread(user.getpseudo(), address, message, 1237);
|
||||
t1.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue