changement du système de connection TCP pour les messages: gestion d'une connection TCP permanente par utilisateur plutôt qu'une connection par message

这个提交包含在:
Louis Farina 2021-01-11 17:40:14 +01:00
父节点 f45cfe2d1a
当前提交 b91f7a1c10
共有 3 个文件被更改,包括 159 次插入129 次删除

查看文件

@ -13,8 +13,6 @@ import java.util.*;
import chat.User; import chat.User;
public class ClientWindow implements ActionListener { public class ClientWindow implements ActionListener {
JFrame chatWindow; JFrame chatWindow;
JPanel chatPanel; JPanel chatPanel;

查看文件

@ -14,41 +14,41 @@ import java.util.*;
import chat.User; import chat.User;
import chat.Message; import chat.Message;
class SendThread extends Thread { //class SendThread extends Thread {
String address; // String address;
Message message; // Message message;
int port; // int port;
//
SendThread(Message in_message, String in_address, int in_port) // SendThread(Message in_message, String in_address, int in_port)
{ // {
message = in_message; // message = in_message;
address = in_address; // address = in_address;
port = in_port; // port = in_port;
} // }
public void run() // public void run()
{ // {
Socket link; // Socket link;
boolean connected = false; // boolean connected = false;
ObjectOutputStream out; // ObjectOutputStream out;
//
while(!connected) // while(!connected)
{ // {
try // try
{ // {
link = new Socket(address, port); // link = new Socket(address, port);
out = new ObjectOutputStream(link.getOutputStream()); // out = new ObjectOutputStream(link.getOutputStream());
out.writeObject(message); // out.writeObject(message);
connected = true; // connected = true;
} // }
catch(IOException e) // catch(IOException e)
{ // {
e.printStackTrace(); // e.printStackTrace();
connected = false; // connected = false;
} // }
} // }
//
} // }
} //}
class ReceiveThread extends Thread { class ReceiveThread extends Thread {
@ -69,43 +69,53 @@ class ReceiveThread extends Thread {
try try
{ {
ServerSocket servSocket = new ServerSocket(port); ServerSocket servSocket = new ServerSocket(port);
Socket link;
boolean exit = false; boolean exit = false;
boolean connected = false;
while(!exit) while(!connected)
{ {
try try
{ {
Socket link = servSocket.accept(); link = servSocket.accept();
ObjectInputStream in = new ObjectInputStream(link.getInputStream()); connected = true;
Message message = (Message) in.readObject();
if(message.getText() != null) while(!exit)
{ {
displayArea.append(message.getAuthor() + " : " + message.getText() + "\n"); try
} {
else System.out.println("Waiting for a message");
{ ObjectInputStream in = new ObjectInputStream(link.getInputStream());
displayArea.append(message.getAuthor() + " has left the chat.\n"); System.out.println("Received a message");
for(int i = 0;i < known_users.size();i ++) Message message = (Message) in.readObject();
{ if(message.getText() != null)
if (known_users.get(i).getName().equals(message.getAuthor())) {
known_users.remove(i); displayArea.append(message.getAuthor() + " : " + message.getText() + "\n");
} }
knownUsersPanel.setText(""); else
knownUsersPanel.append("Online:\n"); {
for(User a:known_users) displayArea.append(message.getAuthor() + " has left the chat.\n");
{ known_users.remove(new User(message.getAuthor()));
knownUsersPanel.append(" " + a.getName() + " \n"); knownUsersPanel.setText("");
} knownUsersPanel.append("Online:\n");
} for(User u:known_users)
displayArea.setCaretPosition(displayArea.getDocument().getLength()); {
knownUsersPanel.append(" " + u.getName() + " \n");
}
exit = true;
}
displayArea.setCaretPosition(displayArea.getDocument().getLength());
}
catch(Exception e)
{
e.printStackTrace();
}
}
link.close(); link.close();
} servSocket.close();
catch(Exception e) }
{ catch(SocketTimeoutException e) {}
}
}
}
} }
catch(IOException e) catch(IOException e)
{ {
@ -118,10 +128,12 @@ class ConnectionListenerThread extends Thread {
JTextArea displayArea; JTextArea displayArea;
JTextArea knownUsersPanel; JTextArea knownUsersPanel;
List<User> known_users; List<User> known_users;
List<Socket> dest_sockets;
ConnectionListenerThread(List<User> in_known_users, JTextArea in_displayArea, JTextArea in_knownUsersPanel) ConnectionListenerThread(List<User> in_known_users, List<Socket> in_dest_sockets, JTextArea in_displayArea, JTextArea in_knownUsersPanel)
{ {
known_users = in_known_users; known_users = in_known_users;
dest_sockets = in_dest_sockets;
displayArea = in_displayArea; displayArea = in_displayArea;
knownUsersPanel = in_knownUsersPanel; knownUsersPanel = in_knownUsersPanel;
} }
@ -136,12 +148,10 @@ class ConnectionListenerThread extends Thread {
DatagramSocket requestSocket; DatagramSocket requestSocket;
DatagramSocket responseSocket; DatagramSocket responseSocket;
ByteArrayOutputStream bStream;
ObjectOutput oo;
String username; String username;
String response = "";
byte[] response1; byte[] responseBytes;
try try
{ {
requestSocket = new DatagramSocket(1234); requestSocket = new DatagramSocket(1234);
@ -151,23 +161,24 @@ class ConnectionListenerThread extends Thread {
{ {
try try
{ {
bStream = new ByteArrayOutputStream();
oo = new ObjectOutputStream(bStream);
System.out.println("Waiting for connection request"); System.out.println("Waiting for connection request");
requestSocket.receive(request); requestSocket.receive(request);
username = new String(request.getData(), 0, request.getLength()); username = new String(request.getData(), 0, request.getLength());
InetAddress clientAddress= request.getAddress(); InetAddress clientAddress= request.getAddress();
System.out.println("Received a request from " + username + "@" + clientAddress.getHostAddress()); System.out.println("Received a request from " + username + "@" + clientAddress.getHostAddress());
System.out.println("Response:");
for(User user:known_users) for(User u:known_users)
{ {
System.out.println(user.getName() + ":" + user.getAddress()); response += u.getName() + " ";
} }
oo.writeObject(known_users); response += ";";
response1 = bStream.toByteArray(); for(Socket s:dest_sockets)
responsePacket = new DatagramPacket(response1, response1.length, clientAddress, 1337); {
response += s.getInetAddress() + " ";
}
System.out.println("Response :" + response);
responseBytes = response.getBytes();
responsePacket = new DatagramPacket(responseBytes, responseBytes.length, clientAddress, 1337);
responseSocket.send(responsePacket); responseSocket.send(responsePacket);
accepted = true; accepted = true;
@ -177,7 +188,10 @@ class ConnectionListenerThread extends Thread {
} }
if(accepted) if(accepted)
{ {
known_users.add(new User(username, clientAddress.getHostAddress())); ReceiveThread next = new ReceiveThread(1237, displayArea, known_users, knownUsersPanel);
next.start();
known_users.add(new User(username));
Collections.sort(known_users); Collections.sort(known_users);
@ -209,6 +223,7 @@ class ConnectionListenerThread extends Thread {
public class NetworkClient { public class NetworkClient {
private User user; private User user;
private List<User> known_users; private List<User> known_users;
private List<Socket> dest_sockets;
private JTextArea chatText; private JTextArea chatText;
private JTextArea knownUsersPanel; private JTextArea knownUsersPanel;
@ -227,10 +242,11 @@ public class NetworkClient {
NetworkClient(JTextArea in_chatText, JTextArea in_knownUsersPanel) NetworkClient(JTextArea in_chatText, JTextArea in_knownUsersPanel)
{ {
user = new User("", ""); user = new User("");
chatText = in_chatText; chatText = in_chatText;
knownUsersPanel = in_knownUsersPanel; knownUsersPanel = in_knownUsersPanel;
known_users = new ArrayList<User>(); known_users = new ArrayList<User>();
dest_sockets = new ArrayList<Socket>();
} }
boolean connect(String username) boolean connect(String username)
@ -241,12 +257,9 @@ public class NetworkClient {
DatagramSocket userListSocket; DatagramSocket userListSocket;
DatagramPacket connectionRequest; DatagramPacket connectionRequest;
DatagramPacket userListPacket; DatagramPacket responsePacket;
ObjectInputStream userListStream;
ArrayList<User> userList;
byte[] buffer1 = new byte[20000]; byte[] buffer1 = new byte[20000];
if(username == null || username.compareTo("") == 0) if(username == null || username.compareTo("") == 0)
@ -266,36 +279,40 @@ public class NetworkClient {
try try
{ {
userListPacket = new DatagramPacket(buffer1, buffer1.length); responsePacket = new DatagramPacket(buffer1, buffer1.length);
System.out.println("Waiting for reply"); System.out.println("Waiting for reply");
userListSocket.receive(userListPacket); userListSocket.receive(responsePacket);
System.out.println("Received a reply from " + userListPacket.getAddress().getHostAddress()); System.out.println("Received a reply from " + responsePacket.getAddress().getHostAddress());
String[] response = new String(responsePacket.getData()).split(";");
userListStream = new ObjectInputStream(new ByteArrayInputStream(userListPacket.getData())); String[] usernameList = response[0].split(" ");
String[] addressList = response[1].split(" ");
userList = (ArrayList<User>) userListStream.readObject(); for(String u:usernameList)
for(User a:userList)
{ {
nameAvailable = nameAvailable && (!username.equals(a.getName())); nameAvailable = nameAvailable && (!username.equals(u));
} }
if(nameAvailable) if(nameAvailable)
{ {
System.out.println(userList.size() + " users currently connected"); System.out.println(usernameList.length + " users currently connected");
for(int i = 0;i < userList.size();i ++) for(String u:usernameList)
{ {
System.out.println(userList.get(i).getName() + " : " + userList.get(i).getAddress()); known_users.add(new User (u));
known_users.add(userList.get(i)); }
for(String a:addressList)
{
dest_sockets.add(new Socket(a, 1237));
} }
known_users.add(new User(username, "25.67.234.235"));
Collections.sort(known_users); Collections.sort(known_users);
connected = true; connected = true;
} }
} }
catch(SocketTimeoutException e) catch(SocketTimeoutException e)
//Si on est tout seul sur le réseau (on ne reçoit aucune réponse)
{ {
known_users.add(new User(username, "25.67.234.235"));
System.out.println("Reply timed out"); System.out.println("Reply timed out");
connected = true; connected = true;
} }
@ -313,24 +330,30 @@ public class NetworkClient {
} }
if(connected) if(connected)
{ {
user.setName(username); user.setName(username);
known_users.add(new User(username));
chatText.append(username + " has joined the chat.\n"); ReceiveThread t2 = new ReceiveThread(1237, chatText, known_users, knownUsersPanel);
ConnectionListenerThread t3 = new ConnectionListenerThread(known_users, dest_sockets, chatText, knownUsersPanel);
t2.start();
t3.start();
try
{
dest_sockets.add(new Socket("25.67.234.235", 1237));
}
catch(IOException e){}
chatText.append(username + " has joined the chat.\n");
chatText.setCaretPosition(chatText.getDocument().getLength()); chatText.setCaretPosition(chatText.getDocument().getLength());
knownUsersPanel.setText(""); knownUsersPanel.setText("");
knownUsersPanel.append(" Online :\n"); knownUsersPanel.append(" Online :\n");
for(User a:known_users) for(User a:known_users)
{ {
knownUsersPanel.append(" " + a.getName() + " \n"); knownUsersPanel.append(" " + a.getName() + " \n");
} }
ReceiveThread t2 = new ReceiveThread(1237, chatText, known_users, knownUsersPanel);
ConnectionListenerThread t3 = new ConnectionListenerThread(known_users, chatText, knownUsersPanel);
t2.start();
t3.start();
} }
return connected; return connected;
@ -338,10 +361,19 @@ public class NetworkClient {
void send (String message) void send (String message)
{ {
for(User recipient:known_users) // for(User recipient:known_users)
{ // {
SendThread t1 = new SendThread(new Message(user.getName(), message), recipient.getAddress(), 1237); // SendThread t1 = new SendThread(new Message(user.getName(), message), recipient.getAddress(), 1237);
t1.start(); // t1.start();
} // }
for(Socket s:dest_sockets)
{
try
{
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
out.writeObject(new Message(user.getName(), message));
}
catch(IOException e){}
}
} }
} }

查看文件

@ -6,13 +6,13 @@ import java.util.*;
public class User implements Comparable<User>, Serializable{ public class User implements Comparable<User>, Serializable{
private String name; private String name;
private String address; // private String address;
public User(String in_name, String in_address) public User(String in_name)
{ {
name = in_name; name = in_name;
address = in_address; // address = in_address;
} }
public void setName(String new_name) public void setName(String new_name)
@ -23,14 +23,14 @@ public class User implements Comparable<User>, Serializable{
{ {
return name; return name;
} }
public void setAddress(String new_address) /*public void setAddress(String new_address)
{ {
name = new_address; name = new_address;
} }
public String getAddress() public String getAddress()
{ {
return address; return address;
} }*/
boolean equals(User b) boolean equals(User b)
{ {