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

This commit is contained in:
Louis Farina 2021-01-11 17:40:14 +01:00
parent f45cfe2d1a
commit b91f7a1c10
3 changed files with 159 additions and 129 deletions

View file

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

View file

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

View file

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