gpt4 book ai didi

java - 在jtextarea中显示连接的客户端

转载 作者:行者123 更新时间:2023-12-01 04:29:57 24 4
gpt4 key购买 nike

我希望能够检测任何连接的客户端并将它们显示在我的 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());

任何建议

最佳答案

在 client.append 之前的 Traiements() 中,您需要添加以下代码:

BufferedReader brBufferedReader1 = new BufferedReader(new InputStreamReader(connection.getInputStream()));  
Client.append(brBufferedReader1.readLine());

关于java - 在jtextarea中显示连接的客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18081071/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com