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

帮忙看看这个网络中传递类怎么不行?
Client:
import   java.net.*;
import   java.io.*;
class   Info   implements   Serializable
{
int   i;
int   j;
public   Info(int   x,int   y)
{
i=x;
j=y;
}
}
public   class   Client7  
{
  Info   a;
  ObjectOutputStream   out;
  Socket   socket;
    public     Client7()
{
try
{
a.i=0;
a.j=1;
socket=new   Socket( "127.0.0.1 ",7777);
out=new   ObjectOutputStream(socket.getOutputStream());
out.writeObject(a);
}
catch(Exception   e)
{
System.out.println( "   no   server ");
}


}  
public   static   void   main(String[]   args)
{
Client7   aa=new   Client7();
}


}

Server:
import   java.net.*;
import   java.io.*;
class   Info   implements   Serializable
{
int   i;
int   j;
public   Info(int   x,int   y)
{
i=x;
j=y;
}
}
public   class     Server7
{
      Info   b;
      ObjectOutputStream   out;
      ObjectInputStream   in;
      Socket   socket;
      ServerSocket   server;
      public   Server7()
      {
      try
      {
      server=new   ServerSocket(7777);
      socket=server.accept();
      in=new   ObjectInputStream(socket.getInputStream());
      out=new   ObjectOutputStream(socket.getOutputStream());
      b=(Info)in.readObject();
      System.out.println(b.i);
      System.out.println(b.j);
      }
        catch(Exception   e)
        {
        System.out.println( "no   client ");
        }
      }
      public   static   void   main(String[]   args)
      {
      Server7   bb=new   Server7();
      }
}
刚学网络,帮忙看看~!!

------解决方案--------------------
public Info(int x,int y)
{
i=x;
j=y;
}


你这里提供了类的构造方法,

a.i=0;
a.j=1;

改为 a = new Info(0,1);
试下