Java中的序列化和反序列化有问题?

时间:2016-05-05 16:46:49

标签: java sockets serialization

我现在正在开展一个项目,在我的班级为一系列不同的端口实施Bellford算法。

我遇到了很多问题,我认为是序列化问题,我通过UDP套接字连接发送Vector_Table对象。我首先序列化向量表然后收到EOFException。我做了一些工作来缩小bug的位置,它似乎在第二个路由器的接收插座中。建造。当我从接收UDPSocket获取bytes []并尝试强制转换为Vector_Table时它来自。

我很难理解为什么抛出它。发送正在为最初构建的路由器工作,所以我认为发送这些表是有效的。我不知道为什么在这个方向会有所不同?

任何协助都将不胜感激。我猜这是一个字节级别的问题,但我有点过头了。

public static void main(String[] args) throws ClassNotFoundException, IOException {
    p P = new p();
    int listenPort = Integer.parseInt(args[0]);
    Vector_Table currentTable = new Vector_Table (Arrays.copyOfRange(args, 1, (args.length)));
    vTableReceiver receiverThread;
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    ObjectOutput obj = new ObjectOutputStream(bOut);
    try {
        obj.writeObject(currentTable);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    currentTable.bytesToTable(bOut.toByteArray());
    System.out.println("hey");

    //currentTable.printTable();
    try {
        receiverThread = new vTableReceiver(listenPort, currentTable);
        receiverThread.run();


public class vTableReceiver implements Runnable{

    p P = new p();
    boolean isRunning = true; 
    Vector_Table currentTable;
    Vector_Table received;
    byte[] buff = new byte[512];
    DatagramSocket socket;

    public vTableReceiver(int port, Vector_Table currTbl) throws SocketException {
        P.P("Receiver Constructed");
        socket = new DatagramSocket(port);
        currentTable = currTbl;

    }

    public void stopThread(){ isRunning = false;}

    public void run() {
        P.P("Receiver Run started");
        try {currentTable.sendToNeighbors(socket);} 
        catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        while(isRunning){
            try{

                DatagramPacket packet = new DatagramPacket(buff, buff.length);
                socket.receive(packet);
                currentTable.reCalc(packet.getData(), socket.getInetAddress());
                currentTable.sendToNeighbors(socket);
                currentTable.printTable();}
            catch(SocketException e){
                e.printStackTrace();
            }

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

    System.out.println("Thread over");
    }
}


public class Vector_Table implements Serializable {

    ArrayList<edge> linkList = new ArrayList<edge>();
    ArrayList<edge> neighborList = new ArrayList<edge>();

    public Vector_Table(String[] cmdLineInterfaces){
        for(int i = 0; i < cmdLineInterfaces.length; i++ ){
            linkList.add(new edge(cmdLineInterfaces[i]));
            neighborList.add(new edge(cmdLineInterfaces[i]));

        }
    }

    public void reCalc(byte[] newTable, InetAddress myAddr) throws ClassNotFoundException, IOException{
        Vector_Table inTable = bytesToTable(newTable);
        int myWeight = 0;
        for(edge e : inTable.linkList){
            if (e.address == myAddr)
                myWeight = e.weight;}
        if(myWeight == 0){System.out.println("Didn't get weight??");}

        for(edge e : inTable.linkList){
            boolean found = false;
            for(edge e2 : linkList){
                if (e2.address == e.address)
                {
                    if (e.weight + myWeight < e2.weight)
                        e2.weight = (e.weight + myWeight);
                    found = true;
                }
            }
            if(!found){
                e.weight += myWeight;
                linkList.add(e);
            }
        }
    }



    void sendToNeighbors(DatagramSocket socket) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        ObjectOutput obj = new ObjectOutputStream(bOut);
        obj.writeObject(this);
        this.bytesToTable(bOut.toByteArray()).printTable();

        for(edge e: neighborList){
            DatagramPacket pack = new DatagramPacket(bOut.toByteArray(), bOut.toByteArray().length,e.address,e.port);
            socket.send(pack);
        }


    }

    public void printTable(){
        System.out.println("Available Nodes:");
        for(int i = 0; i<linkList.size(); i++)
        {
            String destIP = linkList.get(i).address.toString().split("/")[1];
            int destPort = linkList.get(i).port;

            int expense = (linkList.get(i).weight);
            System.out.print("IP: " + destIP);
            System.out.print("     Port: " + destPort);
            System.out.println("      Cost: " + expense);
        }
    };


    public Vector_Table bytesToTable(byte[] inTable) throws ClassNotFoundException{
        ObjectInputStream iStream;
        Vector_Table reTable = null;    
        try {
            iStream = new ObjectInputStream(new ByteArrayInputStream(inTable));
             reTable = (Vector_Table) iStream.readObject();
            iStream.close();
            }
         catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        return reTable;
    }


    public static int toInt(byte[] bytes, int offset, int dataSize) {
          int ret = 0;
          for (int i=0; i<dataSize && i+offset<bytes.length; i++) {
            ret <<= 8;
            ret |= (int)bytes[i] & 0xFF;
          }
          return ret;
        }

}

这是stacktrace:

java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2353)
at java.io.ObjectInputStream$BlockDataInputStream.readInt(ObjectInputStream.java:2846)
at java.io.ObjectInputStream.readHandle(ObjectInputStream.java:1455)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1515)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1781)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373)
at java.util.ArrayList.readObject(ArrayList.java:791)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1058)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1909)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1808)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2018)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1942)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1808)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373)
at Vector_Table.bytesToTable(Vector_Table.java:89)
at Vector_Table.reCalc(Vector_Table.java:30)
at vTableReceiver.run(vTableReceiver.java:37)
at router.main(router.java:26)

0 个答案:

没有答案