Problem
I am trying to send a packet from C++ Server to my Android client but the android client is never receiving the packet. I have tried different things on android both the readline and read buffer but these never return anything. Also I am calling the Method to receive packet from the main activity page.
C++ Code
int numBytes; // the number of bytes sent
// Sends the message to the connected host
try
{
string sendMsg = "This is a test \r\n";
if (numBytes = send(socketId, sendMsg.c_str(), sendMsg.size(), 0) == -1)
{
int errorCode = 0;
string errorMsg = "error calling send():\n";
detectErrorSend(&errorCode,errorMsg);
CExceptionEx socketSendException(errorCode,errorMsg);
throw socketSendException;
}
}
catch(CExceptionEx& excp)
{
excp.response();
exit(1);
}
return numBytes;
Android Code
public void RecievePacket()
{
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(mSock.getInputStream()));
String test = br.readLine();
/* BufferedInputStream input = new BufferedInputStream(mSock.getInputStream());
byte[] buffer = new byte[10]; // 10 bytes buffer
int bytesRead = 0;
while( (bytesRead=input.read(buffer)) !=-1 ) { // read up to 10 bytes
String str = new String(buffer,0,bytesRead); // convert bytes to String using default encoding
//System.out.println("Data received: " + str);
}
*/
}
catch (Exception ex)
{
}
}
Note
The commented code block is the the other way I tried to receive the message, also please note that I am able to send packets from Android to C++.
Edit
The send() is sending 0 bytes but i am able to receive packets from android so there must be a connection.
"\r \n"with a space in between them. I don't think that's the cause of any problems, but it's probably not what you want to be doing.send()returns the number of bytes sent, or-1on error. It might be useful to print out the return value so you can see whether it actually sent everything.socketIDisn't your server socket, is it?