Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从C服务器向Java客户端发送消息_Java_C_Sockets_Tcp - Fatal编程技术网

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

从C服务器向Java客户端发送消息,java,c,sockets,tcp,Java,C,Sockets,Tcp,我试图从C服务器向Java客户机发送字符串数组(char**topics)。显然,服务器正确地发送了主题,但客户端没有收到它们 /* SERVER */ while (*topics != NULL) { printf(" > Sending topic '%s'.. ", *topics); if(write(sd, *topics, sizeof(*topics)) == -1) { perror("write"); exit(1);

我试图从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))
  • topics
    是一个
    char**
    ,因此
    *topics
    是指向
    char
    的指针<因此,code>sizeof*topics是该指针的大小,根据您的体系结构可以是2或4或8字节。这不是你想要的。您需要
    strlen(*topics)
    ,假设这些字符串以null结尾

  • 当你在读接收器中的行时,你需要在发送器中发送行。除非数据已包含换行符,否则需要在发件人中添加一个换行符

  • topics
    是一个
    char**
    ,因此
    *topics
    是指向
    char
    的指针<因此,code>sizeof*topics是该指针的大小,根据您的体系结构可以是2或4或8字节。这不是你想要的。您需要
    strlen(*topics)
    ,假设这些字符串以null结尾

  • 当你在读接收器中的行时,你需要在发送器中发送行。除非数据已包含换行符,否则需要在发件人中添加一个换行符


  • 您可能想了解C中的
    sizeof
    操作符。C服务器是否在流上发送换行符?这就是Java中
    readLine
    阻止的内容。Wireshark……“服务器正确发送主题”您如何验证这一点?添加一个最终
    写入(sd),\n',1)并获得启发。您可能想了解C中的
    sizeof
    操作符。C服务器是否在流上发送换行符?这就是Java中
    readLine
    阻止的内容。Wireshark……“服务器正确发送主题”您如何验证这一点?添加一个最终
    写入(sd),\n',1)并获得启发。