我希望能够检测到任何连接的客户端并将它们显示在我的JTextArea
. 这是我的服务器代码
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
public class serveur extends Thread {
final static int port = 9632;
private Socket socket;
private JTextArea clien;
private String res;
public static void main(String[] args) {
// window
final int windowX = 640; //pixels
final int windowY = 500; //pixels
final FlowLayout LAYOUT_STYLE = new FlowLayout();
JFrame window = new JFrame("admin");
window.setSize(windowX, windowY);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel liste = new JLabel("Clients connectés ");
JTextArea clien = new JTextArea(20,50);
clien.setEditable(false);
JButton captureButton = new JButton("Capture d'écran");
JButton partageButton = new JButton("Partage d'écran");
JButton envoiButton = new JButton("Envoi de fichier");
JButton lancementButton = new JButton("Lancement d'une application");
JButton redémarrageButton = new JButton("Redémarrage de la machine");
JButton infoButton = new JButton("Plus d'information");
Container c = window.getContentPane();
c.setLayout(LAYOUT_STYLE);
c.add(captureButton);
c.add(partageButton);
c.add(envoiButton);
c.add(redémarrageButton);
c.add(infoButton);
c.add(lancementButton);
c.add(liste);
c.add(clien);
c.add(new JSeparator(SwingConstants.VERTICAL));
window.setVisible(true);
//serveur
try{
ServerSocket socketServeur = new ServerSocket(port);
System.out.println("Lancement du serveur");
while (true) {
Socket socketClient = socketServeur.accept();
serveur t = new serveur(socketClient);
t.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//socketServeur.setOption("reuseAddress", true);
public serveur(Socket socket) {
this.socket = socket;
}
public void traitements() {
try {
clien.append(socket.getInetAddress().getHostName());
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
traitements();
}
}
和客户端代码:
import java.net.*;
import java.io.*;
public class client {
final static int port = 9632;
public static void main(String[] args) {
Socket socket;
try {
socket = new Socket(InetAddress.getLocalHost(), port);
} catch (Exception e) {
e.printStackTrace();
}
}
}
我想问题在于这条线的正确位置
clien.append(socket.getInetAddress().getHostName());
有什么建议么