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

写了一个通讯类 客户端类有个错误不能解决 有高手帮小弟我看一下

2012-02-13 
写了一个通讯类 客户端类有个异常不能解决 有高手帮我看一下客户端先启动 再启动服务器的时候有个异常 不

写了一个通讯类 客户端类有个异常不能解决 有高手帮我看一下
客户端先启动 再启动服务器的时候有个异常 不知道怎么解决
还有就是客户端第一次发信息也有抛出个异常 信息发不出去
再就是我定义了关毕窗口的时候就关闭通讯类 可是就是关不掉 而是抛出异常 谁帮我看看

Java code
//服务器端import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.net.*;import java.util.*;public class Server extends JFrame{    JTextArea txt;    ServerSocket seSocket=null;            public void serve(){        txt=new JTextArea();        this.setTitle("学习通讯类");        this.setLayout(new BorderLayout());        this.add(txt,BorderLayout.CENTER);        this.setSize(500,300);        this.setVisible(true);                try{            seSocket=new ServerSocket(8000);            txt.append("服务器启动时间是:"+new Date()+'\n');          } catch (BindException e) {                System.out.println("端口使用中....");                System.out.println("请关掉相关程序并重新运行服务器!");                System.exit(0);            } catch (IOException e) {}                    try{            while(true){            Socket st=seSocket.accept();            CtThread ct=new CtThread();            ct.deliveSt(st);            new Thread(ct).start();          }        }catch(IOException e){}    }                    class CtThread implements Runnable{        Socket st=null;        DataInputStream in=null;        public void deliveSt(Socket st)        {            this.st=st;            try{                in=new DataInputStream(st.getInputStream());            }catch(IOException e){}        }                public void run()        {        try{            BufferedReader br=new BufferedReader(new InputStreamReader(in));            while(true){                String Dialog=br.readLine();                txt.append("从客户端收到信息"+Dialog+'\n');                      }            }catch(IOException e){}        }        }                public static void main(String args[])    {        Server myserver=new Server();        myserver.serve();    }}//客户端类import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.net.*;import java.util.*;public class Client extends JFrame implements ActionListener{    JTextArea txta;    JTextField txtf;    JPanel pl;    JButton bt;    DataOutputStream out=null;    Socket sc=null;    PrintStream ps;    Container f=this.getContentPane();        public void client()    {        f.setLayout(new BorderLayout());        txta=new JTextArea();        f.add(txta,BorderLayout.CENTER);        txtf=new JTextField(20);        bt=new JButton("发送");        pl=new JPanel();        pl.setLayout(new FlowLayout());        pl.add(txtf);        pl.add(bt);        bt.addActionListener(this);        txtf.addActionListener(this);        f.add(pl,BorderLayout.SOUTH);        setTitle("信息发送端");        setSize(500,300);        setVisible(true);        this.addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent e) {                disconnect();                System.exit(0);            }        });                try        {            Socket sc=new Socket("192.168.0.25",8000);            out=new DataOutputStream(sc.getOutputStream());            ps=new PrintStream(out);        }        catch(IOException ex)        {            txta.append(ex.toString()+'\n');        }        }        public void actionPerformed(ActionEvent e)    {        if(e.getSource()==bt|e.getSource()==txtf)        {            String Dialog=txtf.getText();            try            {                ps.println(Dialog);                txta.append("已经发送对话:"+Dialog+'\n');                txtf.setText("");            }            catch(Exception ex)            {                txta.append(ex.toString()+'\n');            }        }    }    public void disconnect() {    try {            out.close();            sc.close();          } catch (IOException e) {}    }                    public static void main(String args[])    {        Client myclient=new Client();        myclient.client();    }} 



谁帮改改

[解决办法]
我感觉你那个把accept()放在构造函数里恐怕不大合适吧,应该写在另外的函数里吧
Thinking in java 中的通讯例子,你参考下吧
Java code
 
//:MultiJabberServer.java
import java.io.*;
import java.net.*;

class ServeOneJabber extends Thread
{
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public ServeOneJabber(Socket s) throws IOException{
socket=s;
in=new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
//Enable auto-flush
out=new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
start();
}
public void run(){
try{
while(true){
String str=in.readLine();
if(str.equals("END"))
break;
System.out.println("Echoing: "+str);
out.println(str);
}
System.out.println("closing...");
}catch(IOException e){
}finally{
try{
socket.close();
}catch(IOException e){}
}
}
}
public class MultiJabberServer
{
static final int PORT=8080;
public static void main(String[] args) throws IOException{
ServerSocket s=new ServerSocket(PORT);
System.out.println("Server Started");
try{
while (true)
{
Socket socket=s.accept();
try{
new ServeOneJabber(socket);
}catch(IOException e){
socket.close();
}
}
}finally{
s.close();
}
}
}
//:JabberClient.java
import java.io.*;
import java.net.*;
public class JabberClient
{
public static void main(String[] args) throws IOException
{
InetAddress addr=InetAddress.getByName(null);
System.out.println("addr = "+addr);
Socket socket=new Socket(addr,JabberServer.PORT);
try{
System.out.println("socket =" +socket);
BufferedReader in=
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
for(int i=0;i <10;i++){
out.println("howdy "+i);
String str=in.readLine();
System.out.println(str);
}
out.println("END");
}finally{
System.out.println("closing...");
socket.close();
}
}
}

[解决办法]
学习

热点排行