//客户端
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.*;
import java.io.*;public class Client extends JFrame{
 private Socket s;
 private JButton send=new JButton("发送");
 private JPanel jp1=new JPanel();
 private JPanel jp2=new JPanel();
 private JTextArea jta=new JTextArea(24,24);
 private JTextField jtf=new JTextField(15);
 private Message m=new Message();
 private ObjectOutputStream oos;
 private ObjectInputStream ois;
 public Client()
 {
  super("Client");
  try {
   s=new Socket("219.229.153.184",8000);
   oos=new ObjectOutputStream(s.getOutputStream());
   ois=new ObjectInputStream(s.getInputStream());
  } catch (UnknownHostException e1) {
   // TODO Auto-generated catch block
   //e1.printStackTrace();
   System.out.println(1);
  } catch (IOException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
  jta.setEditable(false);
  jp1.add(jta);
  this.add(jp1,"Center");
  jp2.add(jtf);
  jp2.add(send);
  //m.Set("1");
  this.setLocation(300,300);
  this.add(jp2,"South");
  this.setSize(300,300);
  this.setVisible(true);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  send.addMouseListener(new MouseAdapter()
  {
   public void mouseClicked(MouseEvent e)
   {
    
    jta.append(jtf.getText()+'\n');
    m.Set(jtf.getText());
    System.out.println(m.Get());
    jtf.setText("");
    try {
     oos.writeObject(m);
     oos.flush();
    } catch (IOException e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
    }
    //m.Set("2");
   }
  });
  get g=new get(ois);
  g.start();
  
 }
 class get extends Thread
 {
  private ObjectInputStream ois;
  private Message m=new Message();
  public get(ObjectInputStream i)
  {
   ois=i;
  }
  public void run()
  {
   while(true)
   {
    try {
     m.Set(((Message)ois.readObject()).Get());
     jta.append(m.Get()+'\n');
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } catch (ClassNotFoundException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
 }
 public static void main(String[] args)
 {
  new Client();
 }
}
//服务器端
import javax.swing.*;
import java.net.*;
import java.awt.*;
import java.io.*;public class Server {
 private ServerSocket ss;
 private Connect c1;
 private Connect c2;
 
 public Server()
 {
  try {
   ss=new ServerSocket(8000);
   Socket s1=ss.accept();
   Socket s2=ss.accept();
   
   c1=new Connect(s1,s2);
   c2=new Connect(s2,s1);
   
   c1.start();
   c2.start();
   
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public static void main(String[] args)
 {
  new Server();
 }
}
class Connect extends Thread
{
 Socket s1;
 Socket s2;
 private ObjectOutputStream oos;
 private ObjectInputStream ois;
 Message m=new Message();
 public Connect(Socket x1,Socket x2)
 {
  s1=x1;
  s2=x2;
  try {
   oos=new ObjectOutputStream(s1.getOutputStream());
   ois=new ObjectInputStream(s2.getInputStream());
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public void run()
 {
  while(true)
  {
   try {
    m.Set(((Message)ois.readObject()).Get());
    oos.writeObject(m);
    oos.flush();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
  }
 }
}

解决方案 »

  1.   

    Message 是我自己定义的一个类  有继承java.io.Serializable 接口
      

  2.   


    public class Message implements java.io.Serializable{
    private String str=new String(); public String Get() 
    {
    return str;
    } public void Set(String str)
    {
    this.str = str;
    }
    }
      

  3.   

    随便的改了一下,可以运行而已:import javax.swing.*;
    import java.awt.*;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.net.*;
    import java.io.*;public class Client extends JFrame{
    private Socket s;
    private JButton send=new JButton("发送");
    private JPanel jp1=new JPanel();
    private JPanel jp2=new JPanel();
    private JTextArea jta=new JTextArea(24,24);
    private JTextField jtf=new JTextField(15);
    private Message m=new Message();
    private ObjectOutputStream oos;
    private ObjectInputStream ois;
    public Client()
    {
    super("Client");
    try {
    s=new Socket("127.0.0.1",8000);
    oos=new ObjectOutputStream(s.getOutputStream());
    ois=new ObjectInputStream(s.getInputStream());
    } catch (UnknownHostException e1) {
    // TODO Auto-generated catch block
    //e1.printStackTrace();
    System.out.println(1);
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    jta.setEditable(false);
    jp1.add(jta);
    this.add(jp1,"Center");
    jp2.add(jtf);
    jp2.add(send);
    //m.Set("1");
    this.setLocation(300,300);
    this.add(jp2,"South");
    this.setSize(300,300);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    send.addMouseListener(new MouseAdapter()
    {
    public void mouseClicked(MouseEvent e)
    { jta.append(jtf.getText()+'\n');
    m.Set(jtf.getText());
    System.out.println("发送内容:"+m.Get());
    jtf.setText("");
    try {
    Message mm = new Message();
    mm.Set(m.Get());
    oos.writeObject(mm);
    oos.flush();
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    //m.Set("2");
    }
    });
    get g=new get(ois);
    g.start(); }
    class get extends Thread
    {
    private ObjectInputStream ois;
    private Message m=new Message();
    public get(ObjectInputStream i)
    {
    ois=i;
    }
    public void run()
    {
    while(true)
    {
    try {
    Object nn=ois.readObject();
    if(nn!=null)
    {
    Message mm = (Message)nn;
    jta.append(mm.Get()+'\n');
    System.out.println("收到内容:"+mm.Get());
    }else
    {
    try {
    Thread.sleep(100);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    }
    public static void main(String[] args)
    {
    new Client();
    }
    }
    //服务器端
    import javax.swing.*;
    import java.net.*;
    import java.awt.*;
    import java.io.*;public class Server {
    private ServerSocket ss;
    private Connect c1;
    private Connect c2; public Server()
    {
    try {
    ss=new ServerSocket(8000);
    Socket s1=ss.accept();
    c1=new Connect(s1);
    c1.start(); } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    public static void main(String[] args)
    {
    new Server();
    }
    }
    class Connect extends Thread
    {
    Socket s1;
    Socket s2;
    private ObjectOutputStream oos;
    private ObjectInputStream ois;
    Message m=new Message();
    public Connect(Socket x1)
    {
    s1=x1;
    try {
    oos=new ObjectOutputStream(s1.getOutputStream());
    ois=new ObjectInputStream(s1.getInputStream());
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    public void run()
    {
    while(true)
    {
    try {
    Object nn=ois.readObject();
    if(nn!=null)
    {
    Message mm = (Message)nn;
    System.out.println("服务端收到的内容:"+mm.Get());
    oos.writeObject(mm);
    oos.flush();
    }else
    {
    try {
    Thread.sleep(100);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } }
    }
    }