簡體   English   中英

在jtextarea中顯示已連接的客戶端

[英]Display connected client in jtextarea

我希望能夠檢測到所有已連接的客戶端並將其顯示在我的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之前的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