I am writing a Java client/server application which will communicate 2 maps back and forth between each program. The client populates the initial map and sends it to the server. The server receives the map as expected and displays the data. The server then builds a response map, and returns that to the client. The Client receives the map as expected and displays the data. All seems well. Now the Client clears and repopulates the map with more data and sends the map to the server. The server receives it as expected, builds and sends the response map. The client reads the 'objectinputstream' for the map, however the map is now empty. The Client clears and repopulates the map with more data and sends the map to the server again. The server seems to receive the map, however the data that is displayed is the data that was received by the second successful transmission, not the current data. Can anyone shed light on what is going on here? All help greatly appreciated!

Here is the Server's Log:

- awaiting connection
- about to readobject
- read this>I-InkjetBadLabel = `Send`
- read this>I-LogoPakBadLabel = `Send`
- read this>I-InkjetBadLabelAck = `Response`
- read this>I-LogoPakBadLabelAck = `Response`
- writing this>I-InkjetBadLabel = `Send`
- writing this>I-LogoPakBadLabel = `Send`
- writing this>I-InkjetBadLabelAck = `Response`
- writing this>I-LogoPakBadLabelAck = `Response`
- wrote socket
- about to readobject
- read this>InkjetBadLabel = `False0`
- read this>LogoPakBadLabel = `False0`
- writing this>InkjetBadLabel = `False0`
- writing this>LogoPakBadLabel = `False0`
- wrote socket
- about to readobject
- read this>InkjetBadLabel = `False0`
- read this>LogoPakBadLabel = `False0`
- writing this>InkjetBadLabel = `False0`
- writing this>LogoPakBadLabel = `False0`
- wrote socket
- about to readobject
- read this>InkjetBadLabel = `False0`
- read this>LogoPakBadLabel = `False0`
- writing this>InkjetBadLabel = `False0`
- writing this>LogoPakBadLabel = `False0`
- wrote socket
- about to readobject
- read this>InkjetBadLabel = `False0`
- read this>LogoPakBadLabel = `False0`
- writing this>InkjetBadLabel = `False0`
- writing this>LogoPakBadLabel = `False0`
- wrote socket

Here is the client's Log:

-  PLC_socket. open!!
-  istream = new ObjectInputStream
-  ostream = new ObjectOutputStream
-  sending externalProgramCommInitializeMap
-  externalProgramCommInitializeMap =>I-InkjetBadLabel = `Send`
-  externalProgramCommInitializeMap =>I-LogoPakBadLabel = `Send`
-  externalProgramCommInitializeMap =>I-InkjetBadLabelAck = `Response`
-  externalProgramCommInitializeMap =>I-LogoPakBadLabelAck = `Response`
-  ostream.writeObject
-  istream.readObject()  1 
-   initialize received externalProgramCommBitMapOutOfPFC  response -
-  externalProgramCommBitMap =>I-InkjetBadLabel = `Send`
-  externalProgramCommBitMap =>I-LogoPakBadLabel = `Send`
-  externalProgramCommBitMap =>I-InkjetBadLabelAck = `Response`
-  externalProgramCommBitMap =>I-LogoPakBadLabelAck = `Response`
-  xx=0
-  externalProgramCommBitMapIntoPFC.clear 
-  externalProgramCommBitMapIntoPFC =>InkjetBadLabel = `False0`
-  externalProgramCommBitMapIntoPFC =>LogoPakBadLabel = `False0`
-  writing map!
-  ostream.flush
-  done writing
-  data - istream.readObject()  
-  data - received externalProgramCommBitMapOutOfPFC  response
-   !!!!!! RESPONSE MAP IS EMPTY !!!!!!
-  xx=1
-  externalProgramCommBitMapIntoPFC.clear 
-  externalProgramCommBitMapIntoPFC =>InkjetBadLabel = `False1`
-  externalProgramCommBitMapIntoPFC =>LogoPakBadLabel = `False1`
-  writing map!
-  ostream.flush
-  done writing
-  data - istream.readObject()  
-  data - received externalProgramCommBitMapOutOfPFC  response
-   !!!!!! RESPONSE MAP IS EMPTY !!!!!!
-  xx=2
-  externalProgramCommBitMapIntoPFC.clear 
-  externalProgramCommBitMapIntoPFC =>InkjetBadLabel = `False2`
-  externalProgramCommBitMapIntoPFC =>LogoPakBadLabel = `False2`
-  writing map!
-  ostream.flush
-  done writing
-  data - istream.readObject()  
-  data - received externalProgramCommBitMapOutOfPFC  response
-   !!!!!! RESPONSE MAP IS EMPTY !!!!!!
-  xx=3
-  externalProgramCommBitMapIntoPFC.clear 
-  externalProgramCommBitMapIntoPFC =>InkjetBadLabel = `False3`
-  externalProgramCommBitMapIntoPFC =>LogoPakBadLabel = `False3`
-  writing map!
-  ostream.flush
-  done writing
-  data - istream.readObject()  
-  data - received externalProgramCommBitMapOutOfPFC  response
-   !!!!!! RESPONSE MAP IS EMPTY !!!!!!

Here is my Client program:

package com.agfa.Verify;

import java.io.*;
import java.net.*;
import java.net.UnknownHostException;
import java.util.*;
import org.apache.log4j.Logger;

public class testVerifyScannedLabels implements Serializable {

    private static final long serialVersionUID = 1L;
    private final Logger log = Logger.getLogger(testVerifyScannedLabels.class.getSimpleName());
    private boolean debugEnabled = false;
    private Map<String, String> externalProgramCommBitMapIntoPFC = new LinkedHashMap<String, String>();
    private Map<String, String> externalProgramCommBitMapOutOfPFC = new LinkedHashMap<String, String>();
    private Socket PLC_socket = null;
    private Map<String, String> externalProgramCommInitializeMap = new LinkedHashMap<String, String>();
    private ObjectOutputStream ostream = null;
    private ObjectInputStream istream = null;
    private List<String> errorMessages = new ArrayList<String>();

    private void initialize() {
        externalProgramCommInitializeMap.put("I-InkjetBadLabel", "Send");
        externalProgramCommInitializeMap.put("I-LogoPakBadLabel", "Send");
        externalProgramCommInitializeMap.put("I-InkjetBadLabelAck", "Response");
        externalProgramCommInitializeMap.put("I-LogoPakBadLabelAck", "Response");
    }

    public testVerifyScannedLabels() {

        initialize();

        /***********************************************************************
         * sockets client for PLC Communicator
         **********************************************************************/
        if (PLC_socket == null) {
            log.debug("PLC_socket == null");
            try {
                istream = null;
                ostream = null;

                PLC_socket = new Socket(InetAddress.getLocalHost(), 60103);
                PLC_socket.setKeepAlive(true);

                log.debug("PLC_socket opened on port 60103");
            } catch (NumberFormatException x) {
                log.warn("PLC_CommMapPort-NumberFormatException Exception! " + x.getMessage());
            } catch (ConnectException x) {
                log.warn("PLC_CommMapPort-Connection Exception! " + x.getMessage());
            } catch (UnknownHostException x) {
                log.warn("PLC_CommMapPort-UnknownHostException Exception! " + x.getMessage());
            } catch (IOException x) {
                log.warn("PLC_CommMapPort-IOException Exception! " + x.getMessage());
            }
        }

        if (PLC_socket == null) {
            log.error("PLC_socket was not established!");
            istream = null;
            ostream = null;
        } else {
            log.debug("PLC_socket not null!!!");
            try { // Socket client..

                if (PLC_socket.isConnected() && !PLC_socket.isClosed()) {
                    log.info("PLC_socket. open!!");
                    if (istream == null) {
                        log.debug("istream = new ObjectInputStream");
                        istream = new ObjectInputStream(PLC_socket.getInputStream());
                    }
                    if (ostream == null) {
                        log.debug("ostream = new ObjectOutputStream");
                        ostream = new ObjectOutputStream(PLC_socket.getOutputStream());
                        log.debug("sending externalProgramCommInitializeMap");
                        final Iterator<Map.Entry<String, String>> it = externalProgramCommInitializeMap
                                .entrySet().iterator();
                        while (it.hasNext()) {
                            final Map.Entry<String, String> pairs = it.next();
                            log.debug("externalProgramCommInitializeMap =>" + pairs.getKey()
                                    + " = `" + pairs.getValue() + "`");
                        }
                        log.debug("ostream.writeObject");
                        ostream.writeObject(externalProgramCommInitializeMap);

                        log.debug("istream.readObject()  1 ");
                        externalProgramCommBitMapOutOfPFC = (Map<String, String>) istream
                                .readObject();
                        log.debug(" initialize received externalProgramCommBitMapOutOfPFC  response -");
                        final Iterator<Map.Entry<String, String>> rit = externalProgramCommBitMapOutOfPFC
                                .entrySet().iterator();
                        while (rit.hasNext()) {
                            final Map.Entry<String, String> pairs = rit.next();
                            log.debug("externalProgramCommBitMap =>" + pairs.getKey() + " = `"
                                    + pairs.getValue() + "`");
                        }
                    }

                    for (int xx = 0; xx < 100; xx++) { // for testing only
                        log.debug("xx=" + xx);
                        log.debug("externalProgramCommBitMapIntoPFC.clear ");
                        externalProgramCommBitMapIntoPFC.clear();
                        externalProgramCommBitMapIntoPFC.put("InkjetBadLabel", "False" + xx);
                        externalProgramCommBitMapIntoPFC.put("LogoPakBadLabel", "False" + xx);
                        externalProgramCommBitMapOutOfPFC.clear();

                        for (String line : errorMessages) {
                            log.info("build externalProgramCommBitMapIntoPFC " + line.toUpperCase());
                            if (line.toUpperCase().contains("INKJET")) {
                                log.debug("in ink");
                                externalProgramCommBitMapIntoPFC.put("InkjetBadLabel", "True" + xx);
                            }
                            if (line.toUpperCase().contains("LOGOPAK")) {
                                log.debug("in logo");
                                externalProgramCommBitMapIntoPFC
                                        .put("LogoPakBadLabel", "True" + xx);
                            }
                        }

                        final Iterator<Map.Entry<String, String>> wit = externalProgramCommBitMapIntoPFC
                                .entrySet().iterator();
                        while (wit.hasNext()) {
                            final Map.Entry<String, String> pairs = wit.next();
                            log.debug("externalProgramCommBitMapIntoPFC =>" + pairs.getKey()
                                    + " = `" + pairs.getValue() + "`");
                        }

                        log.debug("writing map!");
                        ostream.writeObject(externalProgramCommBitMapIntoPFC);
                        log.debug("ostream.flush");
                        ostream.flush();
                        log.debug("done writing");
                        log.debug("data - istream.readObject()  ");
                        externalProgramCommBitMapOutOfPFC = (Map<String, String>) istream
                                .readObject();
                        log.debug("data - received externalProgramCommBitMapOutOfPFC  response");
                        final Iterator<Map.Entry<String, String>> rit = externalProgramCommBitMapOutOfPFC
                                .entrySet().iterator();
                        if (rit.hasNext()) {
                            while (rit.hasNext()) {
                                final Map.Entry<String, String> pairs = rit.next();
                                log.debug("externalProgramCommBitMap =>" + pairs.getKey() + " = `"
                                        + pairs.getValue() + "`");
                            }
                        } else
                            log.debug(" !!!!!! RESPONSE MAP IS EMPTY !!!!!!");
                        Thread.sleep(4000L);
                        /**/} // end for testing ..
                } else {
                    log.error("VerifyScannedLabels unable to connect to PLC_Datacomm via Sockets");
                }
                // end of Socket client
            } catch (UnknownHostException e) {
                log.error("VerifyScannedLabels unable to connect to PLC_Datacomm via Sockets  "
                        + e.getMessage());
                PLC_socket = null;
            } catch (IOException e) {
                log.error("VerifyScannedLabels unable to connect to PLC_Datacomm via Sockets "
                        + e.getMessage());
                PLC_socket = null;
            } catch (ClassNotFoundException e) {
                log.error("VerifyScannedLabels unable to connect to PLC_Datacomm via Sockets "
                        + e.getMessage());
                PLC_socket = null;
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (debugEnabled) {
                log.debug("VerifyScannedLabels processing Ends.");
            }

            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        testVerifyScannedLabels vsl = new testVerifyScannedLabels();
    }
}    

Here is my server program:

package com.agfa.sockets;

import java.net.*;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.io.*;

import org.apache.log4j.Logger;


public class testSocketThreadSendReceiveMap {
    private Logger log = Logger.getLogger(testSocketThreadSendReceiveMap.class.getSimpleName());
    private ServerSocket ssock = null;
    private Socket socket = null;
    private ObjectOutputStream ostream = null;
    private ObjectInputStream istream = null;
    private Map<String, String> dataMapIntoRemote = new LinkedHashMap<String, String>();
    private Map<String, String> dataMapFromRemote = new LinkedHashMap<String, String>();

    public static void main(String[] args) {

        testSocketThreadSendReceiveMap tSTSRM = new testSocketThreadSendReceiveMap();
    }

    @SuppressWarnings("unchecked")
    public testSocketThreadSendReceiveMap() {

        try {
            log.info("awaiting connection");
            ssock = new ServerSocket(60103);
            socket = ssock.accept();
            ostream = new ObjectOutputStream(socket.getOutputStream());
            istream = new ObjectInputStream(socket.getInputStream());

            while (true) {
                //log.debug("following is for testing - remove from production");
                /*
                 * in real life dataMapIntoRemote will be provided by parent
                 * Thread
                 */
                dataMapIntoRemote.clear();
                /****** remove above *****/

                dataMapFromRemote.clear();
                log.debug("about to readobject");
                dataMapFromRemote.putAll((Map<String, String>) istream.readObject());

                final Iterator<Map.Entry<String, String>> it = dataMapFromRemote.entrySet()
                        .iterator();
                while (it.hasNext()) {
                    final Map.Entry<String, String> pairs = it.next();
                    log.debug("read this>" + pairs.getKey() + " = `" + pairs.getValue() + "`");

                        //log.debug("following is for testing - remove from production");
                        /*
                         * in real life dataMapIntoRemote will be provided by
                         * parent Thread
                         */
                        dataMapIntoRemote.put(pairs.getKey(), pairs.getValue());
                        /****** remove above *****/

                }

                final Iterator<Map.Entry<String, String>> wit = dataMapIntoRemote.entrySet()
                        .iterator();
                while (wit.hasNext()) {
                    final Map.Entry<String, String> pairs = wit.next();
                    log.debug("writing this>" + pairs.getKey() + " = `" + pairs.getValue() + "`");
                }

                ostream.writeObject(dataMapIntoRemote);
                ostream.flush();
                log.debug("wrote socket");
            }
        } catch (IOException e) {
            log.error("SocketThreadSendReceiveMap-While(True).IOException! " + e.getMessage());
        } catch (Exception e) {
            log.error("SocketThreadSendReceiveMap-While(True).Exception! " + e.getMessage());

        }
        try {
            log.warn("SocketThreadSendReceiveMap.class socket.close");
            socket.close();
        } catch (IOException e) {
            log.error("SocketThreadSendReceiveMap-socket.close().IOException! " + e.getMessage());
        }
    }
}
有帮助吗?

解决方案

You also need to call ostream.reset() immediately after the call to ostream.flush().

ObjectOutputStream maintains an internal map of references it has written in order to save serialization time if you end up writing the same reference multiple times, which is what you're doing when you send your map. Calling reset() after flush() clears this internal reference table and causes objects to be reserialized the next time time writeObject(object) gets called, regardless of whether the same reference has been passed in before.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top