| | |
How to make Java and C++ sockets play nice
![]() |
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.
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.
"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.
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.
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
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.
Java Syntax (Toggle Plain Text)
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...)
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.
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.
![]() |
Similar Threads
- Java sockets with OSI 7 layer modle (Java)
- Can I make a Java Image Editing Photshop-like program (Java)
- java on mobiles(make progrmaes/games) (Java)
- how do you make java programs faster? (Java)
Other Threads in the Java Forum
- Previous Thread: stream tokenizer
- Next Thread: problems accessing class instances
| Thread Tools | Search this Thread |
2dgraphics 3d @param account affinetransform android api apple applet application arc arguments array arrays automation banking binary binarytree bluetooth chatprogramusingobjects class client code color compare component count database derby design detection eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image integer j2me java java.xls javadesktopapplications javaprojects jni jpanel julia keytool keyword linux list macintosh map method methods midlethttpconnection mobile netbeans object os pong problem producer program project projectideas recursion reference replaysolutions ria rim scanner server set size sms sort sourcelabs sql stop string swing terminal threads transforms tree ui unicode validation web windows






