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.

3
  • 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. Commented May 16, 2012 at 2:24
  • thanks, I move .flush() to the end Commented May 16, 2012 at 2: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? Commented May 16, 2012 at 3:02

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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?
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.

Comments

0

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

You cannot deserialize Java objects directly using C++.

Comments

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.

2 Comments

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).
yes, actually there is no need to add"US-ASCII" inside it still works fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.