java – 在swing applet中使用Socket

我应该做一个服务器客户端在Java,基于Swing和gui.I neeed做某种方式将从服务器到客户端和从客户端到服务器的套接字,并将传递某种字符串。我想要以后一个这个功能可以根据socket中的字符串做几件事情。
由于某种原因,我找不到一个简单的代码示例,以简单的方式显示它的完成情况。
任何人都有一个简单的例子,或者说明如何做呢?
基于这个example,这是一个使用Swing的简单的网络客户端 – 服务器对。注意与正确同步相关的一些问题:GUI本身是使用invokeLater()在事件分派线程上构建的。此外,代码依赖于append()的线程安全性。最后,它包含了文章Text Area Scrolling的一个方便的提示。

更新:在Java 7中,append()不再被标记为线程安全; invokeLater()在display()中用于排序更新。

image

package net;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.text.DefaultCaret;

/**
 * A simple network client-server pair
 * @https://stackoverflow.com/questions/3245805
 */
public class Echo implements ActionListener, Runnable {

    private static final String HOST = "127.0.0.1";
    private static final int PORT = 12345;
    private final JFrame f = new JFrame();
    private final JTextField tf = new JTextField(25);
    private final JTextArea ta = new JTextArea(15, 25);
    private final JButton send = new JButton("Send");
    private volatile PrintWriter out;
    private Scanner in;
    private Thread thread;
    private Kind kind;

    public static enum Kind {

        Client(100, "Trying"), Server(500, "Awaiting");
        private int offset;
        private String activity;

        private Kind(int offset, String activity) {
            this.offset = offset;
            this.activity = activity;
        }
    }

    public Echo(Kind kind) {
        this.kind = kind;
        f.setTitle("Echo " + kind);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getRootPane().setDefaultButton(send);
        f.add(tf, BorderLayout.NORTH);
        f.add(new JScrollPane(ta), BorderLayout.CENTER);
        f.add(send, BorderLayout.SOUTH);
        f.setLocation(kind.offset, 300);
        f.pack();
        send.addActionListener(this);
        ta.setLineWrap(true);
        ta.setWrapStyleWord(true);
        DefaultCaret caret = (DefaultCaret) ta.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
        display(kind.activity + HOST + " on port " + PORT);
        thread = new Thread(this, kind.toString());
    }

    public void start() {
        f.setVisible(true);
        thread.start();
    }

    //@Override
    public void actionPerformed(ActionEvent ae) {
        String s = tf.getText();
        if (out != null) {
            out.println(s);
        }
        display(s);
        tf.setText("");
    }

    //@Override
    public void run() {
        try {
            Socket socket;
            if (kind == Kind.Client) {
                socket = new Socket(HOST, PORT);
            } else {
                ServerSocket ss = new ServerSocket(PORT);
                socket = ss.accept();
            }
            in = new Scanner(socket.getInputStream());
            out = new PrintWriter(socket.getOutputStream(), true);
            display("Connected");
            while (true) {
                display(in.nextLine());
            }
        } catch (Exception e) {
            display(e.getMessage());
            e.printStackTrace(System.err);
        }
    }

    private void display(final String s) {
        EventQueue.invokeLater(new Runnable() {
            //@Override
            public void run() {
                ta.append(s + "\u23CE\n");
            }
        });
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            //@Override
            public void run() {
                new Echo(Kind.Server).start();
                new Echo(Kind.Client).start();
            }
        });
    }
}
翻译自:https://stackoverflow.com/questions/3244400/socket-using-in-a-swing-applet

转载注明原文:java – 在swing applet中使用Socket