nash
nash

Reputation: 533

Sending messages from C server to Java client

I'm trying to send an array of string (char ** topics) from a C server to a Java client. Apparently, the server sends the topics properly, but the client does not receive them.

/* SERVER */
while (*topics != NULL) {
    printf("  > Sending topic '%s'.. ", *topics);
    if(write(sd, *topics, sizeof(*topics)) == -1) {
        perror("write");
        exit(1);
    }
    printf("[OK]\n");

    topics++;
}

The client looks like this:

/* CLIENT */
static void server_connection() {
        String topic = null;

        try {
            Socket _sd = new Socket(_server, _port); // Socket Descriptor

            // Create input stream
            DataInputStream _in = new DataInputStream(_sd.getInputStream());
            BufferedReader _br = new BufferedReader(new InputStreamReader(_in));

            System.out.println("s> Current Topics:");

            while ((topic = _br.readLine()) != null) {
                System.out.println(topic);
            }

            if(topic == null) {
                System.out.println("Not topics found");
            }



            // Close socket connection
            _out.close();
            _in.close();
            _sd.close();

        } catch(IOException e) {
      System.out.println("Error in the connection to the broker " + _server + ":" + _port);
    }
  }

The client shows

s> Current Topics:

and remains waiting... :/

Upvotes: 0

Views: 448

Answers (1)

user207421
user207421

Reputation: 311048

write(sd, *topics, sizeof (*topics))
  1. topics is a char**, so *topics is a pointer to char. sizeof *topics is therefore the size of that pointer, either 2 or 4 or 8 bytes depending on your architecture. This is not what you want. You want strlen(*topics), assuming these are null-terminated strings.

  2. As you're reading lines in the receiver, you need to send lines in the sender. Unless the data already contains a newline,, you need to add one in the sender.

Upvotes: 2

Related Questions