日期:2014-05-20  浏览次数:20755 次

百思不得其解的一个socket问题!
Java code

//服务器端代码
public class ServT {
    ServerSocket ss = null;
    List<Socket> list = null;//保存所有的连接

    public ServT() {
        try {
            ss = new ServerSocket(9999);
        } catch (IOException e) {
            e.printStackTrace();
        }
        list = new ArrayList<Socket>();
    }

    //该内部类用来处理群发的问题
    private class T implements Runnable {
        Socket s;
        List<Socket> list;

        public T(Socket s, List<Socket> list) {
            this.s = s;
            this.list = list;
        }

        @Override
        public void run() {
            BufferedReader br = null;
            PrintWriter pw = null;
            try {
                br = new BufferedReader(new InputStreamReader(
                        s.getInputStream()));
                while (true) {
                    String mesg = br.readLine();
                    for (int i = 0; i < list.size(); i++) {
                        pw = new PrintWriter(list.get(i).getOutputStream());
                        pw.println(mesg);
                        pw.flush();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public void start() {
        Socket s;
        try {
            s = ss.accept();
            list.add(s);
            new T(s, list);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        new ServT().start();
    }

}
//客服端代码
public class Client extends JFrame implements ActionListener {
    JButton conn;//连接
    JButton send;//发送
    JTextArea jta;
    JTextField jtf;
    Socket s = null;

    public Client() {

        setTitle("qq");
        setBounds(100, 100, 300, 400);
        JPanel jp1 = new JPanel();
        add(jp1, BorderLayout.NORTH);
        conn = new JButton("conn");
        conn.addActionListener(this);
        jp1.add(conn);
        JPanel jp2 = new JPanel();
        add(jp2, BorderLayout.CENTER);
        jta = new JTextArea(10, 12);
        jp2.add(new JScrollPane(jta));
        JPanel jp3 = new JPanel();
        add(jp3, BorderLayout.SOUTH);
        jp3.setLayout(new FlowLayout());
        jtf = new JTextField(10);
        send = new JButton("send");
        send.addActionListener(this);
        jp3.add(jtf);
        jp3.add(send);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
//发送
    private class T extends Thread {
        Socket t;
        String mesg;

        public T(Socket t, String mesg) {
            this.t = t;
            this.mesg = mesg;
        }

        public void run() {
            PrintWriter pw = null;
            try {
                pw = new PrintWriter(t.getOutputStream());
                pw.println(mesg);
                pw.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == send) {
            String mesg = jtf.getText();
            new T(s, mesg).start();//发送
            new TT(s).start();//开始接收
        }
        if (e.getSource() == conn) {
            try {
                s = new Socket("127.0.0.1", 9999);

            } catch (Exception ee) {
            }
        }
    }
//连接
    private class TT extends Thread {
        Socket s;

        public TT(Socket s) {
            super();
            this.s = s;
        }

        @Override
        public void run() {
            BufferedReader br = null;
            try {

                String mesg = null;
                br = new BufferedReader(new InputStreamReader(
                        s.getInputStream()));
                while (true) {
                    mesg = br.readLine();
                    jta.append(mesg + "\r");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        }

    }

    public static void main(String[] args) {
        new Client();

    }
}