Reputation: 39
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.
Upvotes: 1
Views: 2237
Reputation: 317
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.
Upvotes: 0
Reputation: 1638
You also could use JNI in your C++ side to deal with the deserialization stuff.
You cannot deserialize Java objects directly using C++.
Upvotes: 0
Reputation: 30095
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.
Upvotes: 0
Reputation: 16545
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.
Upvotes: 2