0

I have implemented a c++ server runs in linux to receive a string from an Android client. The connection established successfully and the string received successfully as well (I know that from the number of bytes received!), however, I can't display the message in one go, I need to access the Char array to display each character. The data been sent on Android using this line :

dataOutputStream.writeUTF(StringToSent);

This is the code of the server below :

char receivedBuff[1025];    
Connection = accept(listenfd, (struct sockaddr*)NULL, NULL);
                cout << "Connection accepted \n";

                numOfBytes = read(Connection,receivedBuff,sizeof(receivedBuff));
                  if (numb < 0) 
                       printf("ERROR reading from socket");
                  printf("%s\n",receivedBuff);

When I try to display the received buffer using the line below, I got nothing :

cout << receivedBuff << Lendl;

However, I can get it a char by char like the line below, but it is messy!

cout << receivedBuff [0] << receivedBuff[1]  << receivedBuff[2] << endl;

I have tried to cast the char Array to string and it does not work. Any suggestions?

*********** THE LAST UPDATE WITH SOLUTION *********** Android side :

PrintStream ps = null;
                ps = new PrintStream(socketw.getOutputStream());
                ps.println(MessageToSent +'\0');

Server side :

 numOfBytes = read(Connection,receivedBuff,sizeof(receivedBuff));
              if (numb < 0) 
                   printf("ERROR reading from socket");
              printf("%s done %d",receivedBuff, numOfBytes);

*********** THE LAST UPDATE WITH SOLUTION ***********

M. A.
  • 134
  • 1
  • 11
  • Show exactly the contents of StringToSend and tell us the value of numOfBytes please. – greenapps Apr 07 '15 at 19:59
  • 1
    Add receivedBuff[numOfBytes]=0; – greenapps Apr 07 '15 at 20:01
  • StringToSent = "ABCD" numOfBytes= 6 There are 2 bytes added on each message I sent I don't know why, if StringToSent = "AB" then numOfBytes will be 4. – M. A. Apr 07 '15 at 20:07
  • 'There are 2 bytes added on each message'. And you will not tell where? In front or at end? – greenapps Apr 07 '15 at 20:30
  • @greenapps Thank you for your concern, I have replaced the dataOutputStream with PrintStream.println on android and now it does not add any extra bytes. On the server sides it fills the empty spaces with zeros like this: thank you green apps 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 done 21 bytes – M. A. Apr 07 '15 at 20:42
  • I have append the SentMessage in Android with '\0' and it solved the problem, here is the last output: Connection accepted thank you greenapps done 20 bytes – M. A. Apr 07 '15 at 20:44

1 Answers1

1

DataOutputStream.writeUTF writes a 16bit length followed by that number of UTF-8-like bytes.

printf prints a NUL terminated C string.

The two are not compatible. Specifically, for a string < 256 bytes, the first byte written by writeUTF is NUL, thereby resulting in a C-string of length 0, like you're seeing.

It is up to you to decide on a common protocol and implement it on both the client side and the server side. A simple example is to write strings as UTF-8 encoded data terminated by a line feed: you can do this with PrintStream.println in Java and std::getline in C++.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • Thank you, do you know any compatible functions ? – M. A. Apr 07 '15 at 20:09
  • @MohammedAl-sadi Yes, see update. If you decide on a line based protocol, you can easily find examples for how to read or write lines to sockets in both languages. – that other guy Apr 07 '15 at 20:12