All,

I am developing a Socket Server in Java. Its job is to receive TCP packets from a unit sending Hayes AT commands via GPRS/sim chip combination over broadband. The software for the unit sending the packets has been written in C++. Packets sent are all in hex bytes. I have no visibility to the source code of the unit.

My server is trying to do a simple read of the packets sent across from the Unit. It appears to accept the socket ok, but when it attemps the following command, it gets an invalid stream header error (shown below):

ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

Is there a known communication issue with data sent via C++ being received by Java? Or am I using the wrong command to read the TCP data sent by the unit? Here is a snipet of the server code:

public class PaymentProcessorServer {
    private ServerSocket server;
    private int port = 2200;

    public PaymentProcessorServer() { 
        try { 
            server = new ServerSocket(port); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
    }

    public static void main(String[] args) {
        PaymentProcessorServer PaymentProcess = new PaymentProcessorServer();
        PaymentProcess.handleConnection();
    }

    public void handleConnection() {
        System.out.println("Waiting for client message...");

        // The server do a loop here to accept all connection initiated by the
        // client application.
        //
        while (true) {
            try {
                Socket socket = server.accept();
                new ConnectionHandler(socket);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
class ConnectionHandler implements Runnable {
    private Socket socket;

    public ConnectionHandler(Socket socket) {
        this.socket = socket;

        Thread t = new Thread(this);
        t.start();

    }

    public void run() {
           //boolean moreData = true;
           //long lStartTime = new Date().getTime();
            /* ObjectOutputStream oos = null;
            ObjectInputStream ois = null;
            */
            while (true) {
                try {
                //
                // Read a message sent by client application
                //
               System.out.println("Received client message from: " + socket.getInetAddress());
                socket.setSoTimeout(30000);

                ObjectInputStream ois =
                    new ObjectInputStream(socket.getInputStream());
                String message = (String)ois.readObject();

                 ObjectOutputStream sout =
                 new ObjectOutputStream(socket.getOutputStream());
                 sout.flush();                     
                System.out.println("Waiting for client message...");
            }catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    } 
} 


Connected to localhost in port 2000
java.io.StreamCorruptedException: invalid stream header: 57686174        at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)        at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)

Thanks in advance for any insight you can provide... I'm hoping there just something minor I'm missing!!

Mike

Recommended Answers

All 5 Replies

please use the CODE tags around your code (you can highlight than choose the '#' button in the message box).

Sorry... Here is the code tagged. Thanks for any help!! Any ideas?

public class PaymentProcessorServer {
private ServerSocket server;
private int port = 2200;

public PaymentProcessorServer() { 
try { 
server = new ServerSocket(port); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
}

public static void main(String[] args) {
PaymentProcessorServer PaymentProcess = new PaymentProcessorServer();
PaymentProcess.handleConnection();
}

public void handleConnection() {
System.out.println("Waiting for client message...");

// The server do a loop here to accept all connection initiated by the
// client application.
//
while (true) {
try {
Socket socket = server.accept();
new ConnectionHandler(socket);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class ConnectionHandler implements Runnable {
private Socket socket;

public ConnectionHandler(Socket socket) {
this.socket = socket;

Thread t = new Thread(this);
t.start();

}

public void run() {
//boolean moreData = true;
//long lStartTime = new Date().getTime();
/* ObjectOutputStream oos = null;
ObjectInputStream ois = null;
*/
while (true) {
try {
//
// Read a message sent by client application
//
System.out.println("Received client message from: " + socket.getInetAddress());
socket.setSoTimeout(30000);

ObjectInputStream ois =
new ObjectInputStream(socket.getInputStream());
String message = (String)ois.readObject();

ObjectOutputStream sout =
new ObjectOutputStream(socket.getOutputStream());
sout.flush(); 
System.out.println("Waiting for client message...");
}catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
} 
}

Looking at the Java docs, I see here: http://java.sun.com/j2se/1.4.2/docs/api/java/io/ObjectOutputStream.html

An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream. The objects can be read (reconstituted) using an ObjectInputStream. Persistent storage of objects can be accomplished by using a file for the stream. If the stream is a network socket stream, the objects can be reconsituted on another host or in another process.

and here: http://java.sun.com/j2se/1.4.2/docs/api/java/io/ObjectInputStream.html

An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream.

So, yes: This class will be unable to read arbitrary data from the socket.

Instead, you need to find some way to read raw bytes or similar. Looks to me as if a DataInputStream might be what you want.

Instead, you need to find some way to read raw bytes or similar. Looks to me as if a DataInputStream might be what you want.

griswolf, thanks for the explanation. That makes perfect sense! I will try DataInputStream today and report back how it goes.

Mike

griswolf, thanks for the explanation. That makes perfect sense! I will try DataInputStream today and report back how it goes.
Mike

Just to circle back on this, DataInputStream DID do the trick! I was able to read and process the data with no problem. Thanks griswolf for the direction!

code:

DataInputStream din;
din = new DataInputStream(socket.getInputStream());
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.