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 ***********