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

时间:2013-08-06 13:06:26

标签: java swing jtextarea serversocket event-dispatch-thread

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

任何建议

1 个答案:

答案 0 :(得分:0)

在client.append之前的traitements()中

你需要添加以下代码:

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