繁体   English   中英

将消息从C服务器发送到Java客户端

[英]Sending messages from C server to Java client

我正在尝试从C服务器向Java客户端发送字符串数组( char ** topics )。 显然,服务器正确发送了主题,但是客户端没有收到它们。

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

    topics++;
}

客户看起来像这样:

/* 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);
    }
  }

客户端显示

s> Current Topics:

并一直在等待...:/

write(sd, *topics, sizeof (*topics))
  1. topics是一个char** ,因此*topics是一个指向char的指针。 因此, sizeof *topics是该指针的大小,根据您的体系结构为2或4或8个字节。 这不是您想要的。 您需要strlen(*topics) ,假设它们是以空字符结尾的字符串。

  2. 在接收方中读取线路时,需要在发送方中发送线路。 除非数据已经包含换行符,否则您需要在发送者中添加一个换行符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM