从C服务器向Java客户端发送消息
我试图从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);
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)代码>并获得启发。