0

I'm programming a Java server and a C++ client with the purpose of send Strings between them. But I am having so much problems with C++...

On my server, I create the server socket that listens on. After connection I expect a client message with:

entrada = new DataInputStream(socketCliente1.getInputStream());
mensajeRecibido=entrada.readUTF();

In my client I create the socket and connect as follows:

SOCKET ConnectSocket = INVALID_SOCKET;
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
struct addrinfo *result = NULL, *ptr = NULL, hints;

ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;


iResult = getaddrinfo("192.168.0.17", DEFAULT_PORT, &hints, &result);
ptr = result;
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
freeaddrinfo(result);

The problem comes here ... How do I send the String to the Java server?

And if I write in the Java server as:

output = new DataOutputStream (socketCliente1.getOutputStream ()); 
salida.writeUTF ("Hello");

How could receive with my C++ client?

If it's possible I would like not to touch the code in Java, because it has to work with Java clients too (which I have not had any problems to implement).

I don't have so much idea about C, so please, if it's possible tell me details like the libraries to use in the answer.

MrLore
  • 3,759
  • 2
  • 28
  • 36
  • You're probably going to get a decent idea of what will be required on the C++ side [after reading this](http://stackoverflow.com/questions/7630242/why-does-dataoutputstream-writeutf-add-additional-2-bytes-at-the-beginning), a somewhat related question. – WhozCraig May 30 '14 at 16:39
  • http://msdn.microsoft.com/en-us/library/windows/desktop/ms740149(v=vs.85).aspx – Ivan May 30 '14 at 16:40
  • Since you explicitly asked for it, using the Boost.Asio library (http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio.html) will make your C++ code much simpler. – Christian Hackl May 30 '14 at 17:08

0 Answers0