implémentation de la phase de découverte/connexion finie

This commit is contained in:
Louis Farina 2020-12-14 16:42:27 +01:00
父節點 6904cf452e
當前提交 935125c083
共有 2 個文件被更改,包括 36 次插入36 次删除

查看文件

@ -120,21 +120,24 @@ class ConnectionListenerThread extends Thread {
{
requestSocket = new DatagramSocket(1234);
responseSocket = new DatagramSocket();
bStream1 = new ByteArrayOutputStream();
bStream2 = new ByteArrayOutputStream();
oo1 = new ObjectOutputStream(bStream1);
oo2 = new ObjectOutputStream(bStream2);
while(!exit)
{
try
{
bStream1 = new ByteArrayOutputStream();
bStream2 = new ByteArrayOutputStream();
oo1 = new ObjectOutputStream(bStream1);
oo2 = new ObjectOutputStream(bStream2);
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();
System.out.println("Received a request from " + username + "@" + clientAddress.getHostAddress());
if(!user.findUser(username))
{
oo1.writeObject(user.getUsers());
response1 = bStream1.toByteArray();
responsePacket1 = new DatagramPacket(response1, response1.length, clientAddress, 1337);
@ -145,14 +148,12 @@ class ConnectionListenerThread extends Thread {
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(SocketTimeoutException e) {}
}
}
catch(Exception e)
@ -191,7 +192,6 @@ public class ClientWindow implements ActionListener {
ClientWindow()
{
String username = "";
Boolean connected = false;
DatagramSocket connectionSocket;
@ -228,7 +228,7 @@ public class ClientWindow implements ActionListener {
hostListSocket.setSoTimeout(500);
connectionRequest = new DatagramPacket(username.getBytes(), username.length(),
InetAddress.getByName("192.168.1.255"), 1234);
InetAddress.getByName("25.255.255.255"), 1234);
System.out.println("Sending connection request");
connectionSocket.send(connectionRequest);
@ -240,6 +240,7 @@ public class ClientWindow implements ActionListener {
System.out.println("Waiting for reply");
userListSocket.receive(userListPacket);
hostListSocket.receive(hostListPacket);
System.out.println("Received a reply from " + userListPacket.getAddress().getHostAddress());
userListStream = new ObjectInputStream(new ByteArrayInputStream(userListPacket.getData()));
hostListStream = new ObjectInputStream(new ByteArrayInputStream(hostListPacket.getData()));
@ -247,18 +248,17 @@ public class ClientWindow implements ActionListener {
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
{
System.out.println(userList.size() + " users currently connected");
for(int i = 0;i < userList.size();i ++)
{
System.out.println(hostList.get(i));
System.out.println(userList.get(i) + " : " + hostList.get(i));
user.add_to_known_users(userList.get(i), hostList.get(i));
}
user.add_to_known_users(username, InetAddress.getLocalHost().getHostAddress());
user.add_to_known_users(username, getLocalIP());
connected = true;
}
}
@ -282,7 +282,7 @@ public class ClientWindow implements ActionListener {
chatWindow = new JFrame("Système de clavardage 2.0.1");
chatPanel = new JPanel(new GridLayout(2, 2));
chatPanel = new JPanel(new BorderLayout(2, 2));
chatText = new JTextArea(1,1);
textScroll = new JScrollPane(chatText);
@ -297,7 +297,6 @@ public class ClientWindow implements ActionListener {
sendPanel.add(sendButton);
chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
chatWindow.setSize(new Dimension(1024, 768));
chatText.setLineWrap(true);
chatText.setEditable(false);
@ -312,6 +311,7 @@ public class ClientWindow implements ActionListener {
//Display the window.
chatWindow.pack();
chatWindow.setVisible(true);
chatWindow.setSize(new Dimension(1024, 768));
ReceiveThread t2 = new ReceiveThread(1237, chatText);
ConnectionListenerThread t3 = new ConnectionListenerThread(user);

查看文件

@ -6,8 +6,8 @@ import java.util.*;
public class User {
private String pseudo;
private ArrayList<String> knownUsers;
private ArrayList<String> knownHosts;
private List<String> knownUsers;
private List<String> knownHosts;
public User(String in_pseudo)
{
@ -33,11 +33,11 @@ public class User {
{
return (knownUsers.indexOf(username) != -1);
}
public ArrayList<String> getUsers()
public List<String> getUsers()
{
return knownUsers;
}
public ArrayList<String> getHosts()
public List<String> getHosts()
{
return knownHosts;
}