http://tieba.baidu.com/p/2038990107

解决方案 »

  1.   

    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import java.io.*;
    public class CharClient extends Frame {
    Socket s=null;
    TextField tfTxt = new TextField();
    TextArea taContent = new TextArea(); public static void main(String[] args) {
    new CharClient().LanchFrame();
    new CharClient().connect();
    } public void LanchFrame() {
    setSize(200, 100);
    setLocation(100, 100);
    add(tfTxt, BorderLayout.SOUTH);
    add(taContent, BorderLayout.NORTH);
    pack();
    this.addWindowListener(new WindowAdapter(){ @Override
    public void windowClosing(WindowEvent arg0) {
    System.exit(0);
    }});
    tfTxt.addActionListener(new tfListener());
    setVisible(true);
    }
    public void connect(){
    try { s=new Socket("localhost",8888);}catch(Exception e){
    e.getStackTrace();
    }
    }
    public class tfListener implements ActionListener{ @Override
    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    String str=tfTxt.getText().trim();
    taContent.setText(str);
    tfTxt.setText("");
    try{DataOutputStream dis=new DataOutputStream(s.getOutputStream());
    dis.writeUTF(str);
    dis.flush();
    dis.close();
    }catch(Exception e1){
    e1.getStackTrace();
    }
    }
    }
    }
      

  2.   

    import java.io.*;
    import java.net.*; public class ChatServer { public static void main(String[] args) {
    try {
    ServerSocket ss = new ServerSocket(8888);
    while(true){
    Socket s = ss.accept();
    DataInputStream dos=new DataInputStream(s.getInputStream());
    String str1=dos.readUTF();
    System.out.println("qqq");
    System.out.println(str1);
    dos.close();

    }catch (IOException e) {
    e.getStackTrace();
    }
    catch(Exception e2){
    e2.getStackTrace();
    }
    } }
      

  3.   

     客户端的socket断开了
    所以服务端没收到数据将 s=new Socket("localhost",8888);
    移到 
    public void actionPerformed(ActionEvent e) {
    里面 
    放在try{ 的后面即可