How to make Java and C++ sockets play nice

Reply

Join Date: Jun 2004
Posts: 18
Reputation: Sideshow Bob is an unknown quantity at this point 
Solved Threads: 0
Sideshow Bob's Avatar
Sideshow Bob Sideshow Bob is offline Offline
Newbie Poster

How to make Java and C++ sockets play nice

 
0
  #1
Nov 9th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: How to make Java and C++ sockets play nice

 
0
  #2
Nov 9th, 2004
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 18
Reputation: Sideshow Bob is an unknown quantity at this point 
Solved Threads: 0
Sideshow Bob's Avatar
Sideshow Bob Sideshow Bob is offline Offline
Newbie Poster

Re: How to make Java and C++ sockets play nice

 
0
  #3
Nov 10th, 2004
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().
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: How to make Java and C++ sockets play nice

 
0
  #4
Nov 10th, 2004
"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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 18
Reputation: Sideshow Bob is an unknown quantity at this point 
Solved Threads: 0
Sideshow Bob's Avatar
Sideshow Bob Sideshow Bob is offline Offline
Newbie Poster

Re: How to make Java and C++ sockets play nice

 
0
  #5
Nov 19th, 2004
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.

  1. DataInputStream in = new DataInputStream(socket.getInputStream());
  2.  
  3. byte[] array = new byte[11]; // 11 is the constant used to receive the file size
  4.  
  5. in.read(array);
  6. fileString = new String(array);
  7. fileString = fileString.trim(); // remove any whitespace (in particular an \0 which indicates the end of a transfer.
  8. int fileLength = Integer.parseInt(fileString);
  9. array = new byte[fileLength];
  10.  
  11. in.read(array);
  12.  
  13. if(verifyFile(tempItem.getMD5Hash(), array) {
  14. 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
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 18
Reputation: Sideshow Bob is an unknown quantity at this point 
Solved Threads: 0
Sideshow Bob's Avatar
Sideshow Bob Sideshow Bob is offline Offline
Newbie Poster

Re: How to make Java and C++ sockets play nice

 
0
  #6
Nov 21st, 2004
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC