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

烦恼...基础的都不会..
将线程用在socket上面,老是实现不了,非常不解...请各位高手指点一下。
Java code
package work_2;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Panel;
import java.awt.BorderLayout;

import java.net.Socket;
import java.net.UnknownHostException;

import java.io.InputStreamReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedReader; 
import java.io.PrintWriter;

public class chat_client extends JFrame implements ActionListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    /**
     * @param args
     */
    JButton jb = new JButton("start");
    List list = new List();
    Panel p1 = new Panel(), p2 = new Panel();
    Socket s = null;
    InputStream ip = null;
    PrintWriter pw;
    InputStreamReader ir = null;
    BufferedReader br = null;

    chat_client() {
        jb.addActionListener(this);
        this.setLayout(new BorderLayout());
        this.add(list, BorderLayout.CENTER);
        this.add(p1, BorderLayout.SOUTH);
        p1.setLayout(new BorderLayout());
        p1.add(jb, BorderLayout.EAST);
        this.setSize(275, 345);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        try {
            s = new Socket("season", 3000);
        } catch (UnknownHostException ue) {
            list.add("UnknownHostException");
            System.exit(0);
        } catch (IOException ie) {
            list.add("IOException");
            System.exit(0);
        }
        new Thread(run()).start();
    }

    private Runnable run() {
        // TODO Auto-generated method stub
        if (s != null) {
            while (true) {
                try {
                    ip = s.getInputStream();
                    ir = new InputStreamReader(ip);
                    br = new BufferedReader(ir);
                    list.add(br.readLine());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    list = new List();
                    list.add("IOException :can't get data");
                }
            }
        }else{
            System.exit(0);
        }
        return run();
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new chat_client();
    }

    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        Object b = e.getSource();
        if (b.equals(jb)) {
            try {
                pw = new PrintWriter(s.getOutputStream());
                pw.println("ok    ");
                pw.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
                System.out.println("op error");
            }
        }

    }
}



Java code
package work_2;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JButton;

import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.io.InputStreamReader; 
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.PrintWriter;


public class chat_server extends JFrame implements ActionListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    /**
     * @param args
     */
    JButton jb = new JButton("work");
    ServerSocket ss = null;
    Socket s = null;
    InputStream ip = null;
    InputStreamReader ir = null;
    BufferedReader br = null;
    PrintWriter pw;

    chat_server() {
        jb.addActionListener(this);
        this.setLayout(new FlowLayout());
        this.add(jb);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(200, 100);
        this.setVisible(true);
        try {
            ss = new ServerSocket(3000);
        } catch (IOException ie) {
            System.exit(0);
            System.out.println("IOException on newing");
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new chat_server();
    }

    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        Object b = e.getSource();
        if (b.equals(jb)) {
            new Thread(run()).start();
        }
    }

    private Runnable run() {
        // TODO Auto-generated method stub
        if (ss != null) {
            while (true) {
                try {
                    s = ss.accept();
                    new Thread(run2(s)).start();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    System.exit(0);
                }
            }
        }
        return run();
    }

    private Runnable run2(Socket s2) {
        // TODO Auto-generated method stub
        if (s2 != null) {
            try {
                ip = s2.getInputStream();
                ir = new InputStreamReader(ip);
                br = new BufferedReader(ir);
                String string = br.readLine().trim();
                pw = new PrintWriter(s2.getOutputStream());
                pw.println(string);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println("thread2               ");
            }
        }
        return run2(s2);
    }

}