I am working on a project which involves a server/client model running over a network. The server is written in Java, and the client is a C++ addition to an existing product (which is written in VB and C/C++).

I chose to write my section in Java simply because I have a lot more experience in Java development, know the functionality of JDBC (the server interacts with a MySQL database), and we're sending XML files over - which is something i've worked with with Java in the past.

The process of the interation is roughly like this:

1) Server is running
2) Client connects to server
3) Server spins off thread to handle client
4) Client transmits either "GET" or "POST" using a byte stream
5a) If "GET" the server spits out a list of items that we are looking for on the client
5b) If "POST" the server prepares to receive information from the client about the requested server (consists on an XML file and the file itself - transmission handled by us - not FTP or any other jazz)
6) Server acks that it has the file/XML and processes
7) Connection is closed.

My tester (server tester) and the client tester all work just fine, but things are being f*ked up when we try to talk to each other.

I really need some help getting these to processes to talk to each other. I have looked around online and googled this, but haven't been able to find people talking about this. JNI is an option - but not appropriate from my understanding. I don't know how i would get RMI to work with C++ - i think this would just be moving the problem somewhere else.

Thanks in advance.

What kind of problems are you having? I can think of at least three big reasons off the top of my head why a Java server couldn't communicate properly with a C++ client.

We are able to make the connection to each other, but when the client tranfers the UTF-9 chars to make up GET the Java client won't receive the data and the thread dies when the input stream attempts a readLine().

"won't receive" and "dies" aren't detailed enough. What happens when the client sends and the server receives? Making the connection shouldn't be a problem, and from what I understand it isn't. How is the client set up to send data? I can imagine how the server is set up to receive, and unless the client is designed to send what the server can recognize, it won't work. You'll either terminate with errors (which I want to see, as this seems to be your problem) or get garbage.

All right, now i can be a bit more specific.

We are able to transfer ascii files back and forth (xml, file lengths, things like that) but whenever we attempt to transfer a binary file such as an executable, there are corruption issues, and the process fails. Here is some of the code that I am using on my side. This takes place in a thread which receives the socket attribute when constructed.

DataInputStream in = new DataInputStream(socket.getInputStream());

byte[] array = new byte[11];  // 11 is the constant used to receive the file size

in.read(array);
fileString = new String(array);
fileString = fileString.trim();    // remove any whitespace (in particular an \0 which indicates the end of a transfer.
int fileLength = Integer.parseInt(fileString);
array = new byte[fileLength];

in.read(array);

if(verifyFile(tempItem.getMD5Hash(), array) {
    conn.add(blah blah blah...)

verifyItem is an md5 checking function which returns a boolean.

The data other than the file itself is received as an XML file before each associated file is transmitted. The XML file provides an expected md5, the name of the file, it's index in the database, and of course the total number of files to be transmitted.

If there's any other information I can provide you with, please let me know.

Bob

All right, I have managed to get this running just fine. I ended up sitting down and looking at the problem from a more open perspective instead of, "why the hell isn't this bitch doing what i want it to do?" and after some contemplation, I remembered that Java stores a char as 2 bytes, and C++ stores it as 1. This makes things nice and fun.

The workaround? Encode the files as ASCII characters using Hex coding, base 64, or Huffman encoding. Huffman is probably the best if you need to send large files, but that's not the case here, so we've just used hex encoding.

In java, this is a piece of cake to do - just download soap and use the built in library. c++ I'm not too sure - that wasn't my part.

I'm using the same technique to store the information in my database in a machine independent way. The next step is to create a client that the other guys here in the office can use through a web browser, and PHP will make this very easy for me.

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.