refactoring: ajout des fonctionnalités réseaux dans une nouvelle classe NetworkClient séparée
This commit is contained in:
rodzic
9d8032a0cd
commit
8ac8cdd27e
2 zmienionych plików z 332 dodań i 282 usunięć
|
@ -12,171 +12,8 @@ import java.util.*;
|
||||||
|
|
||||||
import chat.User;
|
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();
|
|
||||||
|
|
||||||
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);
|
|
||||||
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);
|
|
||||||
responseSocket.send(responsePacket1);
|
|
||||||
|
|
||||||
oo2.writeObject(user.getHosts());
|
|
||||||
response2 = bStream2.toByteArray();
|
|
||||||
responsePacket2 = new DatagramPacket(response2, response2.length, clientAddress, 1338);
|
|
||||||
responseSocket.send(responsePacket2);
|
|
||||||
|
|
||||||
user.add_to_known_users(username, clientAddress.getHostAddress());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
catch(SocketTimeoutException e) {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch(Exception e)
|
|
||||||
{
|
|
||||||
System.out.println("nik!!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public class ClientWindow implements ActionListener {
|
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;
|
JFrame chatWindow;
|
||||||
|
|
||||||
|
@ -188,100 +25,15 @@ public class ClientWindow implements ActionListener {
|
||||||
JTextField messageField;
|
JTextField messageField;
|
||||||
JButton sendButton;
|
JButton sendButton;
|
||||||
|
|
||||||
User user = new User("");
|
NetworkClient network;
|
||||||
|
|
||||||
|
|
||||||
ClientWindow()
|
ClientWindow()
|
||||||
{
|
{
|
||||||
String username = "";
|
Boolean connected = false;
|
||||||
Boolean connected = false;
|
String username = "";
|
||||||
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("25.255.255.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);
|
|
||||||
System.out.println("Received a reply from " + userListPacket.getAddress().getHostAddress());
|
|
||||||
|
|
||||||
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
|
|
||||||
{
|
|
||||||
System.out.println(userList.size() + " users currently connected");
|
|
||||||
for(int i = 0;i < userList.size();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, getLocalIP());
|
|
||||||
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ème de clavardage 2.0.1");
|
chatWindow = new JFrame("Système de clavardage 2.0.1");
|
||||||
|
|
||||||
chatPanel = new JPanel(new BorderLayout(2, 2));
|
chatPanel = new JPanel(new BorderLayout(2, 2));
|
||||||
chatText = new JTextArea(1,1);
|
chatText = new JTextArea(1,1);
|
||||||
textScroll = new JScrollPane(chatText);
|
textScroll = new JScrollPane(chatText);
|
||||||
|
@ -289,34 +41,44 @@ public class ClientWindow implements ActionListener {
|
||||||
sendPanel = new JPanel(new BorderLayout(1, 2));
|
sendPanel = new JPanel(new BorderLayout(1, 2));
|
||||||
messageField = new JTextField();
|
messageField = new JTextField();
|
||||||
sendButton = new JButton("Envoyer");
|
sendButton = new JButton("Envoyer");
|
||||||
sendButton.addActionListener(this);
|
|
||||||
messageField.addActionListener(this);
|
|
||||||
|
|
||||||
|
|
||||||
sendPanel.add(messageField);
|
network = new NetworkClient(chatText);
|
||||||
sendPanel.add(sendButton, BorderLayout.EAST);
|
|
||||||
|
while(!connected && username != null)
|
||||||
|
{
|
||||||
|
username = JOptionPane.showInputDialog(chatWindow,
|
||||||
|
"Enter a username",
|
||||||
|
"POPUPOPOPUPUPOPOPUP",
|
||||||
|
JOptionPane.PLAIN_MESSAGE);
|
||||||
|
|
||||||
|
connected = network.connect(username);
|
||||||
|
}
|
||||||
|
|
||||||
chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
if(username != null)
|
||||||
|
{
|
||||||
chatText.setLineWrap(true);
|
sendButton.addActionListener(this);
|
||||||
chatText.setEditable(false);
|
messageField.addActionListener(this);
|
||||||
|
|
||||||
chatPanel.add(textScroll);
|
|
||||||
chatPanel.add(sendPanel, BorderLayout.SOUTH);
|
|
||||||
|
|
||||||
chatWindow.getContentPane().add(chatPanel, BorderLayout.CENTER);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Display the window.
|
sendPanel.add(messageField);
|
||||||
chatWindow.pack();
|
sendPanel.add(sendButton, BorderLayout.EAST);
|
||||||
chatWindow.setVisible(true);
|
|
||||||
chatWindow.setSize(new Dimension(1024, 768));
|
chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
ReceiveThread t2 = new ReceiveThread(1237, chatText);
|
chatText.setLineWrap(true);
|
||||||
ConnectionListenerThread t3 = new ConnectionListenerThread(user);
|
chatText.setEditable(false);
|
||||||
t2.start();
|
|
||||||
t3.start();
|
chatPanel.add(textScroll);
|
||||||
|
chatPanel.add(sendPanel, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
chatWindow.getContentPane().add(chatPanel, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
//Display the window.
|
||||||
|
chatWindow.pack();
|
||||||
|
chatWindow.setVisible(true);
|
||||||
|
chatWindow.setSize(new Dimension(1024, 768));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
public static void main (String [] args)
|
public static void main (String [] args)
|
||||||
{
|
{
|
||||||
|
@ -327,12 +89,10 @@ public class ClientWindow implements ActionListener {
|
||||||
|
|
||||||
public void actionPerformed(ActionEvent event) {
|
public void actionPerformed(ActionEvent event) {
|
||||||
String message = messageField.getText();
|
String message = messageField.getText();
|
||||||
messageField.setText("");
|
if(message.compareTo("") != 0)
|
||||||
|
|
||||||
for(String address:user.getHosts())
|
|
||||||
{
|
{
|
||||||
SendThread t1 = new SendThread(user.getpseudo(), address, message, 1237);
|
messageField.setText("");
|
||||||
t1.start();
|
network.send(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
290
src/chat/NetworkClient.java
Normal file
290
src/chat/NetworkClient.java
Normal file
|
@ -0,0 +1,290 @@
|
||||||
|
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();
|
||||||
|
|
||||||
|
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);
|
||||||
|
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);
|
||||||
|
responseSocket.send(responsePacket1);
|
||||||
|
|
||||||
|
oo2.writeObject(user.getHosts());
|
||||||
|
response2 = bStream2.toByteArray();
|
||||||
|
responsePacket2 = new DatagramPacket(response2, response2.length, clientAddress, 1338);
|
||||||
|
responseSocket.send(responsePacket2);
|
||||||
|
|
||||||
|
user.add_to_known_users(username, clientAddress.getHostAddress());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch(SocketTimeoutException e) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
System.out.println("nik!!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class NetworkClient {
|
||||||
|
static 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
User user;
|
||||||
|
JTextArea chatText;
|
||||||
|
|
||||||
|
NetworkClient(JTextArea in_chatText)
|
||||||
|
{
|
||||||
|
user = new User("");
|
||||||
|
chatText = in_chatText;
|
||||||
|
}
|
||||||
|
Boolean connect(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];
|
||||||
|
|
||||||
|
if(username == null || username.compareTo("") == 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
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("25.255.255.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);
|
||||||
|
System.out.println("Received a reply from " + userListPacket.getAddress().getHostAddress());
|
||||||
|
|
||||||
|
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
|
||||||
|
{
|
||||||
|
System.out.println(userList.size() + " users currently connected");
|
||||||
|
for(int i = 0;i < userList.size();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, getLocalIP());
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(connected)
|
||||||
|
{
|
||||||
|
user.setpseudo(username);
|
||||||
|
ReceiveThread t2 = new ReceiveThread(1237, chatText);
|
||||||
|
ConnectionListenerThread t3 = new ConnectionListenerThread(user);
|
||||||
|
t2.start();
|
||||||
|
t3.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
return connected;
|
||||||
|
}
|
||||||
|
|
||||||
|
void send (String message)
|
||||||
|
{
|
||||||
|
for(String address:user.getHosts())
|
||||||
|
{
|
||||||
|
SendThread t1 = new SendThread(user.getpseudo(), address, message, 1237);
|
||||||
|
t1.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Ładowanie…
Reference in a new issue