简体   繁体   English

在jtextarea中显示已连接的客户端

[英]Display connected client in jtextarea

I want to be able to detect any connected clients and display them on my JTextArea . 我希望能够检测到所有已连接的客户端并将其显示在我的JTextArea Here is my server code 这是我的服务器代码

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(); 
    } 
}

And the client code : 和客户端代码:

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(); 
        } 
    } 
}

I guess the problem is with the correct placement of this line 我想问题出在这条线的正确位置

clien.append(socket.getInetAddress().getHostName());

Any suggestions 有什么建议么

in the traitements () just before client.append you need to add the folowing code : 在client.append之前的traitements()中,您需要添加以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM