1

The Java client can connect to the C++ server using TCP. The Java client is on Win7. The C++ server is on Linux.

The problem is I cannot send data successfully to the C++ server. The Java code is:

public static void main (String [] args ) throws IOException {
    Socket sock = new Socket("10.217.140.200",7000); 

    String id = "TEST";
    char encoding = 'a';

    ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
    oos.flush();

    oos.writeObject(encoding);

After your kind helps, I now use outputstream and it works. Remember to delete the ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream()); otherwise it will output 4 chars to c++ server.

Greg Kopff
  • 15,945
  • 12
  • 55
  • 78
zhoubo
  • 39
  • 8
  • You don't really give enough info to be sure where the problem is, but for a start the `oos.flush()` should go after the `oos.writeObject()`, not before. – Mac May 16 '12 at 02:24
  • thanks, I move .flush() to the end – zhoubo May 16 '12 at 02:58
  • now, I can send data from java client to c++ server, the strange thing is that the data stored starts from the fourth position (don't know why 0 to 3 location is not used ). when I print out the received data on c++ side, the result for printing 0 to 3 is two ? anyone knows what the first four location is used for? – zhoubo May 16 '12 at 03:02

4 Answers4

2

You're using an ObjectOutputStream, which does Java object serialisation. This is not (easily) portable across languages. Since you're only sending a String, you don't need to use an ObjectOutputStream - just use the OutputStream returned from the socket.

Greg Kopff
  • 15,945
  • 12
  • 55
  • 78
  • thanks, I changed to outputstream, and now, I can send data from java client to c++ server, the strange thing is that the data stored starts from the fourth position (don't know why 0 to 3 location is not used ). when I print out the received data on c++ side, the result for printing 0 to 3 is two ? anyone knows what the first four location is used for? – zhoubo May 16 '12 at 03:04
0

I highly recommend using some 3rd-party library for serialization of your objects. Doing it manually is very time-consuming (for you) and error-prone.

I personally big fan of Google Protocol Buffers. I actively use it on almost all of my projects (Java server, .Net client, C++ client).

What is it? Protocol Buffers are a way of encoding structured data in an efficient yet extensible format. Google uses Protocol Buffers for almost all of its internal RPC protocols and file formats.

List of implementations for different languages is here.

expert
  • 29,290
  • 30
  • 110
  • 214
0

You also could use JNI in your C++ side to deal with the deserialization stuff.

You cannot deserialize Java objects directly using C++.

ebasconp
  • 1,608
  • 2
  • 17
  • 27
0

After updating your code to not use the ObjectOutputStream, I'm going to guess that your problem is either in the C++ server or because of not converting the String into ASCII prior to transmitting over the socket. Java strings can be represented in various encodings, including Unicode.

Does this work any better for you?

public static void main (String [] args ) throws IOException
{
    Socket sock = new Socket("10.217.140.200",7000);
    String id = "TEST";

    OutputStream oos = sock.getOutputStream();

    oos.write( id.getBytes("US-ASCII") );
    oos.flush();
}

If not, please post both your updated client and server side code via editing your question.

Patrick W
  • 317
  • 1
  • 13
  • While I agree that you should explicitly set the encoding to use, given that the test string is simply "TEST" and this will map into the right place in pretty much any encoding scheme, I don't think this will end up being the problem. (What I'm saying is, the platform's default encoding scheme will very likely have the ASCII charset at the "bottom" anyway, so "TEST" should work). – Greg Kopff May 16 '12 at 04:57
  • yes, actually there is no need to add"US-ASCII" inside it still works fine – zhoubo May 16 '12 at 05:50