如果在不同项目的客户端定义了序列化类,如何从服务器端的序列化类中提取数据

时间:2016-10-31 17:42:16

标签: java serialization arraylist client-server bufferedimage

我在两个独立的项目中创建了“客户端”和“服务器”。我需要从客户端向服务器发送两个arraylists和一个缓冲图像。为此,我创建了一个序列化类“sending_data”,其中包含两个arraylists和缓冲图像。

由于“sending_data”类仅在“client”中定义而不在“server”中定义,因此我在“server”中包含了“client”的jar。 数据被客户端成功发送为“sending_data”对象,但是我在从服务器端接收到的“sending_data”对象中提取两个arraylists和缓冲图像时遇到问题。我不知道我错在哪里。

以下是“sending_data”类的代码。

public class sending_data implements Serializable
{
 ArrayList<ArrayList<String>> stringer=new ArrayList<>();
 ArrayList<ArrayList<Integer>> integ=new ArrayList<>();
  transient byte[] client_byte;

 public ArrayList<ArrayList<String>> get_string_arraylist()
 {
     return stringer;
 }
 public ArrayList<ArrayList<Integer>> get_integer_arraylist()
 {
     return integ;
 }
 public byte[] get_buff_array()
 {
     return client_byte;
 }
 public void set_string_arrayList(ArrayList<ArrayList<String>> arr_str)
 {
     stringer=arr_str;
 }
 public void set_integer_arrayList(ArrayList<ArrayList<Integer>> arr_integ)
 {
     integ=arr_integ;
 }
 public void set_byte_array(byte[] cli_byte)
 {
     client_byte=cli_byte;
 }
}

下面给出了从“客户端”到“服务器”端发送数据的代码部分:

 String server_name="localhost";
            int port=25000;
            try
            {
                System.out.println("Connecting to " + server_name + " on port " + port);
                Socket client = new Socket(server_name, port);
                System.out.println("Just connected to " + client.getRemoteSocketAddress());
                ObjectOutputStream outToServer = new ObjectOutputStream(client.getOutputStream());
               sending_data sd_=new sending_data();  
                sd_.set_string_arrayList(global);
                sd_.set_integer_arrayList(global_inte);
                sd_.set_byte_array(imageInByte);
                outToServer.writeObject(sd_);
                System.out.println("Data send to server");
                ObjectInputStream inFromServer = new ObjectInputStream(client.getInputStream());
                sen_data=(sending_data)inFromServer.readObject();
                outToServer.close();
                inFromServer.close();
                client.close();
                System.out.println("message send");
            }
            catch(IOException ex)
            {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                 Logger.getLogger(Sender.class.getName()).log(Level.SEVERE, null, ex);
             }

这是“服务器”端的代码。我只显示声明的代码部分并从“客户端”接收数据。

public class Server extends Application {
 String s="";
 ImageView myImageView;
 BufferedImage bbbf;
 DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
 Date date = new Date();
 ArrayList<ArrayList<String>> stringer_=new ArrayList<>();
 ArrayList<ArrayList<Integer>> Integg=new ArrayList<>();
 byte[] byter;
Sender.sending_data  sd=null;
@Override
public void start(Stage primaryStage) {
    Text t = new Text(20,20," ");
    t.setFont(Font.font ("Segoe Print", 20));
     myImageView=new ImageView();
    myImageView.setFitHeight(300);
    myImageView.setFitWidth(200);
    Button btn = new Button();
    btn.setText("Start Server");
    btn.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            try
            {
   ServerSocket serverSocket=new ServerSocket(25000);
   System.out.println("Waiting for connection on "+25000);
   Socket socket=serverSocket.accept();
   System.out.println("connected to localhost on port no. "+25000);
   ObjectInputStream serverInputStream = new ObjectInputStream(socket.getInputStream());
   ObjectOutputStream serverOutputStream = new ObjectOutputStream(socket.getOutputStream());
   Object anObj = serverInputStream.readObject();    
   sd= (Sender.sending_data)anObj; 
   byter=new byte[4665600];
   if(sd!=null)
   {
       t.setText("Data recieved at  "+dateFormat.format(date));
   }
   stringer_=sd.get_string_arraylist();
   Integg=sd.get_integer_arraylist();
   byter=sd.get_buff_array();
   for(int i=0;i<20;i++)
   {
       System.out.print(byter[i]+"  ");
   }
   System.out.println("data recieved successfully");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    });


public static void main(String[] args)  {
   launch(args);
}   

}

我得到的错误是:

java.lang.NullPointerException
at server.Server$1.handle(Server.java:80)
at server.Server$1.handle(Server.java:57)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)

我认为问题出在“服务器”一侧:

 Sender.sending_data sd=null;

所以我试着通过将上面的行改为:

来解决它
Sender.sending_data sd= new Sender.sending_data 

但它显示错误并且netbeans中给出的提示是:包含Sender.sending_data的封闭实例是必需的 我完全糊涂了。请帮帮我。

0 个答案:

没有答案