0

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... :/

8
  • 2
    You might want to read about the sizeof operator in C. Commented Mar 25, 2016 at 18:05
  • 1
    Is the C server sending a newline character over the stream? That is what readLine is blocking on in Java. Commented Mar 25, 2016 at 18:06
  • 1
    Wireshark...................... Commented Mar 25, 2016 at 18:15
  • 1
    "the server sends the topics properly" how did you verify this? Commented Mar 25, 2016 at 18:22
  • 2
    Add a final write(sd, '\n', 1); and get enlightened. Commented Mar 25, 2016 at 18:24

1 Answer 1

2
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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.