But how can I get a C++ client to do the same thing as the java client? I've tried many things but nothing seems to work. Ideally I wouldn't want to touch the java server, is that possible? If someone could point me to a good example or some sample code, that would be much appreciated. I searched through a lot of websites but to no avail.

Nice example, although you should really remember to check return codes and especially check error codes. Your loop will end if an error occurs and you don't check to ensure that connect succeeded. In networking code, it is absolutely imperative that you always know how much data is actually in your buffer also, do NOT assume that things are null-terminated. Also, multiple calls to send on one side can result in those buffers being concatenated so that you get all of the data in one read on the other side.
–
Jeff TuckerDec 22 '09 at 0:48

@Jeff: Actually, from reading the code example I assumed processBuffer() would handle errors and perhaps copy any partially-complete requests split over multiple reads to a separate heap buffer to be processed on the next read. At least, that's what I'd do.
–
asveikauDec 22 '09 at 0:52

Yes, as I said, the code is very, very simplified. Of course it should be improved a lot. I consider it just an example for a start, not a end solution.
–
AndresDec 22 '09 at 0:59

You might want to include the code for writen() and readn() or mention where they came from (UNIX Network Programming IIRC).
–
D.ShawleyDec 22 '09 at 1:12

Without studying the code too carefully, my first concern is that you're converting a String (in chars, or byte-pairs) to bytes, and then sending these down the socket. Are you retrieving these in the same fashion on the C++ end ? i.e. using the same character encoding ?

i will complement this answer by saying that Java uses Unicode (UTF-16) to store a character. so either you convert your Java String to ANSI before sending or you use a std::wstring on the C++ client side.
–
Adrien PlissonDec 23 '09 at 11:16

As long as your're not using a language specific protocol (such as Java RMI) and using straight sockets (or some other language neutral protocol like web services) then it will work. You just have to make sure your client and server are speaking the same protocol (e.g. TCP/IP + your custom protocol on top). If using straight sockets you're basically sending binary over the wire - so you need to make sure you're encoding/decoding the data the same on both sides. Typically this is done with some sort of byte level protocol if you're rolling your own. As an example Project Dark Star is a game server written in Java but has clients in Java, C/C++, Flash etc. It uses user defined a binary protocol for cross language communications.

Yes you can have a C++ client (or a C client or a COBOL client or a perl client or a ruby client or a ... client) talk to a java server. All you do is port the java code to the equivalent C++ code (or C code or COBOL code or perl code or ruby code or a ... code). The server neither knows nor cares what language the client is written in.

I would start with this reference Internet Sockets and then use the POSIX API Documentation.

I had the same problem prior. The client was C++ based and it has to connect to my Java-based server both running on Windows. I was stumped for the longest time why the Java server would drop connection upon client connection.

Later I realised it because of the endian problems. Java VM = big endian, C++ (on my hardware) was little endian. Small problem but took a while to figure out for this amateur.