owen
owen

Reputation: 126

C++ Server Send message to Android Client using TCP

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.

Upvotes: 4

Views: 5042

Answers (2)

brenns10
brenns10

Reputation: 3369

There must be something wrong that's not part of the code you've given us. I wrote my own minimal example running on my own computer using your code, and everything worked as expected. Check if this runs correctly on your computer (run the C++, then the Java). Also check if there's anything I do in my C++ code that you're missing.

socket.cpp: (I'm more of a C programmer, so I just used C++ for the code you provided).

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <string>

void connection(int sock)
{
  // Pretty much your C++ code verbatim.
  std::string sendMsg = "This is a test \r\n";
  int amt = send(sock, sendMsg.c_str(), sendMsg.size(), 0);
  printf("Send %d bytes.\n", amt);
  close(sock);
}

int main(int argc, char *argv[])
{
  int sock, csock;
  struct sockaddr_in sin;
  char *host = "127.0.0.1";
  unsigned short port = 1234;

  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    exit(EXIT_FAILURE);
  }

  sin.sin_family = AF_INET;
  sin.sin_port = htons(port);
  if (inet_pton(AF_INET, host, &sin.sin_addr) != 1) {
    perror("inet_pton");
    exit(EXIT_FAILURE);
  }

  if (bind(sock, (struct sockaddr*) &sin, sizeof(sin)) != 0) {
    perror("bind");
    exit(EXIT_FAILURE);
  }

  if (listen(sock, SOMAXCONN) != 0) {
    perror("listen");
    exit(EXIT_FAILURE);
  }

  if ((csock = accept(sock, NULL, NULL)) == -1) {
    perror("accept");
    exit(EXIT_FAILURE);
  }

  connection(csock);
  close(sock);
  return EXIT_SUCCESS;
}

SockTest.java:

import java.net.Socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class SockTest {
    public static void main(String[] args) {
        try {
            Socket sock = new Socket("127.0.0.1", 1234);

            // Your Java Code Verbatim:
            BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            String test = br.readLine();

            System.out.println(test);
            sock.close();
        } catch (Exception ex) {}
    }
}

Output for the C++:

$ ./socket
Send 17 bytes.

Output for the Java:

$ java SockTest
This is a test 

Upvotes: 2

abhi-rao
abhi-rao

Reputation: 2785

shouldn't it be

 if (numBytes = send(socketId, sendMsg.c_str(), (sendMsg.size() + 1), 0) == -1) ...

Note that +1

Upvotes: 0

Related Questions