Skip to content

Commit

Permalink
Migliorire disconnessione
Browse files Browse the repository at this point in the history
  • Loading branch information
LL1201 committed Dec 16, 2022
1 parent 96e71d4 commit 72fa568
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
Binary file added ChatLocale.jar
Binary file not shown.
15 changes: 13 additions & 2 deletions src/ChatLocale/ChatClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public ChatClient(Socket s, String nomeChat) {

public void run() {
// timeout per lo spegimento del server e la disconnessione dei client
clientClose = true;
try {
s.setSoTimeout(4500);
s.setSoTimeout(1500);
} catch (SocketException e1) {
e1.printStackTrace();
}
Expand Down Expand Up @@ -58,8 +59,17 @@ public void run() {
}

}
out.println(nomeChat + ": si è disconnesso dal server \n");

System.out.println("Client is down");
try {
s.close();
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}

GUI.txtChat.append("Ti sei disconnesso dal server \n");
}

public void Invia(String messaggio) {
Expand All @@ -69,5 +79,6 @@ public void Invia(String messaggio) {

public static void Disconnetti() {
ChatThread.clientClose = false;
clientClose = false;
}
}
9 changes: 8 additions & 1 deletion src/ChatLocale/ChatThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public ChatThread(Socket cmd, String ip) {
public void run() {
// timeout per lo spegimento del server e la disconnessione dei client
try {
s.setSoTimeout(4500);
s.setSoTimeout(1500);
} catch (SocketException e1) {
e1.printStackTrace();
}
Expand Down Expand Up @@ -55,6 +55,13 @@ public void run() {
}
}
System.out.println("Client is down");
try {
s.close();
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public void StampaMessaggio(String msg) {
Expand Down
16 changes: 13 additions & 3 deletions src/ChatLocale/GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowEvent;
import java.awt.Component;
import javax.swing.ScrollPaneConstants;

public class GUI extends JFrame {
Expand All @@ -35,6 +35,7 @@ public class GUI extends JFrame {
private JTextField txtIpServer;
public static JTextArea txtChat;
public static JTextArea txtMessaggio;
public static ExecutorService poolClient = Executors.newFixedThreadPool(50);

/**
* Launch the application.
Expand Down Expand Up @@ -99,6 +100,7 @@ public void actionPerformed(ActionEvent e) {
});
btnInvia.setBounds(451, 478, 117, 60);
contentPane.add(btnInvia);


JButton btnConnetti = new JButton("Connettiti");
btnConnetti.addActionListener(new ActionListener() {
Expand All @@ -109,8 +111,8 @@ public void actionPerformed(ActionEvent e) {
try {
Socket socket = new Socket(txtIpServer.getText(), 1212);
chatClient = new ChatClient(socket, txtNomeChat.getText());
// Server.client.add(chatClient);
new Thread(chatClient).start();
poolClient.execute(chatClient);
// new Thread(chatClient).start();
System.out.println("Pool OK");
btnConnetti.setEnabled(false);
btnDisconnetti.setEnabled(true);
Expand Down Expand Up @@ -145,6 +147,9 @@ public void actionPerformed(ActionEvent e) {
btnAvviaServer.setEnabled(false);
btnConnetti.setEnabled(true);
txtChat.append("Processo server avviato. In attesa di connessione");
txtIpServer.setEnabled(false);
txtNomeChat.setEnabled(false);
btnConnetti.setEnabled(false);
}
});
btnAvviaServer.setBounds(10, 52, 558, 46);
Expand All @@ -153,6 +158,10 @@ public void actionPerformed(ActionEvent e) {
btnDisconnetti.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ChatClient.Disconnetti();
btnConnetti.setEnabled(true);
txtIpServer.setEnabled(true);
txtNomeChat.setEnabled(true);
btnDisconnetti.setEnabled(false);
}
});
btnDisconnetti.setEnabled(false);
Expand All @@ -170,6 +179,7 @@ public void actionPerformed(ActionEvent e) {

txtChat = new JTextArea();
txtChat.setBounds(10, 176, 558, 291);
txtChat.setEditable(false);
contentPane.add(txtChat);

txtMessaggio = new JTextArea();
Expand Down

0 comments on commit 72fa568

Please sign in to comment.