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