首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

烦恼.基础的都不会.该如何处理

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

烦恼...基础的都不会..
将线程用在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);    }} 



首先谢谢看完的朋友,再多谢回答的朋友,这个线程和socket自己学着感觉问题很多,而且自己动手写的没有运行到过..搞到我人都烦了.所以请高手指点下.

[解决办法]
可以这样创建线程么?
[解决办法]
...你創建線程的目的是什么?你為什么要在你的類里邊寫那個 run方法?
[解决办法]
这样写线程好像不对吧,第一次看到这样写,我觉得可以写成匿名内部类的形式,如下:
new Thread(new Runnable(){
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);
}
}
}
}).start();
只有启动线程就没有问题了
[解决办法]
线程基础的就两种方法,一个是实现RUNNABLE借口的类,在THREAD()中引用。
另一个是extends thread一个类,然后直接START。
改一下写法大概就行了。

热点排行