Java的插座SwingWorker的运行,但是接收或发送的任何消息(Java socket swi

2019-07-19 01:14发布

前几天我试图创建一个服务器 - 客户端或客户端服务器作为实验使用一个线程来了解插座,但后来有人告诉我,我应该使用SwingWorker的。 我做了一些研究如何使用和已经在实施它的做法,但它仍然无法正常工作。 该SwingWorker类线程看起来并不像它在运行甚至寿我得到一个连接并使用.excute()。 如果你们能帮那个地方我做错了,这将是巨大的。 SwingWorker类是在startSever()和startClient()方法。

    private void startServer() {
        SwingWorker <Void, String> runningServer = new SwingWorker<Void, String>(){
        protected Void doInBackground() {
            try {
                listeningSocket = new ServerSocket(port);
                System.out.println("waiting for connection");
                connection = listeningSocket.accept();
                connected = true;
                System.out.println("Connected");
                String incomeMessage =null;
                while(connected){
                inStream = connection.getInputStream();
                    inDataStream = new DataInputStream(inStream);
                    if (myMessage !=null){
                        outStream = connection.getOutputStream();
                        outDataStream = new DataOutputStream(outStream);
                    outDataStream.writeUTF(myMessage);
                    }

                    if((incomeMessage = inDataStream.readUTF())!=null){
                        clientMessage = incomeMessage;
                        publish(clientMessage);
                        incomeMessage =null;
                    }
                }
            } catch (IOException e) {
                clientMessage = "Connection Lost";
            }
        return null;
    }           
runningServer.execute();
}

Answer 1:

这里是一个非常简单的例子。

基本上,因为你的程序需要异步通信(也就是你需要能够从套接字读取,并在同一时间写它),你需要每个流卸载到一个单独的线程。

这个例子中的管理流程,好了,不存在的。 实际上,你应该有某种“联系”经理将能够干净利落关闭输出和输入线程,这样,例如,当用户键入“再见”时,输出线程将能够告诉连接管理器连接应该终止。 然后,它会告诉输入线程停止读取任何新的消息并终止...

客户

public class Client {

    public static void main(String[] args) {

        try {
            Socket master = new Socket("localhost", 8900);
            new Thread(new InputHandler(master)).start();
            new Thread(new OuputHandler(master)).start();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    public static class InputHandler implements Runnable {

        private Socket socket;

        public InputHandler(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            boolean commune = true;
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                while (commune) {
                    String text = reader.readLine();
                    System.out.println("\n<server> " + text);
                    if (text.toLowerCase().equals("bye")) {
                        commune = false;
                    }
                }
            } catch (Exception exp) {
                exp.printStackTrace();
            } finally {
                try {
                    reader.close();
                } catch (Exception e) {
                }
                try {
                    socket.close();
                } catch (Exception e) {
                }
            }
        }
    }

    public static class OuputHandler implements Runnable {

        private Socket socket;

        public OuputHandler(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            boolean commune = true;
            BufferedWriter writer = null;
            try {
                writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                Scanner scanner = new Scanner(System.in);
                while (commune) {
                    System.out.print("> ");
                    String text = scanner.nextLine();
                    writer.write(text);
                    writer.newLine();
                    writer.flush();
                    if (text.equalsIgnoreCase("bye")) {
                        commune = false;
                    }
                }
            } catch (Exception exp) {
                exp.printStackTrace();
            } finally {
                try {
                    writer.close();
                } catch (Exception e) {
                }
                try {
                    socket.close();
                } catch (Exception e) {
                }
            }
        }
    }
}

服务器

public class Server {

    public static void main(String[] args) {

        try {
            ServerSocket master = new ServerSocket(8900);
            Socket socket = master.accept();
            new Thread(new InputHandler(socket)).start();
            new Thread(new OuputHandler(socket)).start();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    public static class InputHandler implements Runnable {

        private Socket socket;

        public InputHandler(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            boolean commune = true;
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                while (commune) {
                    String text = reader.readLine();
                    System.out.println("\n<client> " + text);
                    if (text.toLowerCase().equals("bye")) {
                        commune = false;
                    }
                }
            } catch (Exception exp) {
                exp.printStackTrace();
            } finally {
                try {
                    reader.close();
                } catch (Exception e) {
                }
                try {
                    socket.close();
                } catch (Exception e) {
                }
            }
        }
    }
    public static class OuputHandler implements Runnable {

        private Socket socket;

        public OuputHandler(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            boolean commune = true;
            BufferedWriter writer = null;
            try {
                writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                Scanner scanner = new Scanner(System.in);
                while (commune) {
                    System.out.print("> ");
                    String text = scanner.next();
                    writer.write(text);
                    writer.newLine();
                    writer.flush();
                    if (text.equalsIgnoreCase("bye")) {
                        commune = false;
                    }
                }
            } catch (Exception exp) {
                exp.printStackTrace();
            } finally {
                try {
                    writer.close();
                } catch (Exception e) {
                }
                try {
                    socket.close();
                } catch (Exception e) {
                }
            }
        }
    }
}

更新(嗲)

虽然我有你的源代码在我的面前......

应该非常,非常,很少是需要做textMessage.addKeyListener(this)

由于您使用的是JTextField ,你应该使用ActionListener来代替。 有对这个重要原因AA号码,但对你来说,主要的一个是这样一个事实:“接受”的行动是取决于外观。 虽然大多数系统都使用输入 ,因为“接受”的动作,并不能保证。

看看如何写一个动作侦听器的更多信息

鉴于你正在尝试做的,一为一个整体不错的尝试一般的复杂性!



Answer 2:

使用这个例子 ,下面的变化与单个Telnet客户端工作。

private PrintWriter out;
...
public void keyPressed(KeyEvent e) {
    if (e.getKeyChar() == KeyEvent.VK_ENTER) {
        myMessage = friendLabel + textMessage.getText();
    if (out != null) {
        out.println(myMessage);
    }
    ...
}
...
protected Void doInBackground() {
    try {
        listeningSocket = new ServerSocket(port);
        System.out.println("Waiting for connection");
        connection = listeningSocket.accept();
        connected = true;
        System.out.println("Connected");
        Scanner in = new Scanner(connection.getInputStream());
        out = new PrintWriter(connection.getOutputStream(), true);
        publish("Connected");
        while (true) {
            publish(in.nextLine());
        }
    } catch (IOException e) {
        clientMessage = "Connection Lost";
        try {
            connection.close();
            System.out.println("Closed");
        } catch (IOException e1) {
            e1.printStackTrace();
            connected = false;
        }
    }
    return null;
}


Answer 3:

我看到你的服务器端口是8900,你的客户端端口是8900了。 我不知道,如果它在服务器和客户端在同一台机器上运行的重要...



文章来源: Java socket swingWorker running but no message received or transmitted