简体   繁体   English

从C客户端向Java服务器发送字符串

[英]Sending a string from C client to Java server

I'm trying to send a string from my c client to a Java server, after which the server sends a text file to the client. 我正在尝试从我的c客户端向Java服务器发送一个字符串,之后服务器将一个文本文件发送到客户端。

This is the part of client code that sends the string. 这是发送字符串的客户端代码的一部分。

int n = write(sock_fd,"Ready",5);
if (n < 0) 
     printf("ERROR writing to socket\n");
 recv_file(sock_fd, filename);

And this is the server part of java code: 这是java代码的服务器部分:

InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
System.out.println("Message received from client is " + message);
String FILENAME = "data.txt";
sendFile(socket, "data.txt");

Now I have verified that if I remove the part in the server code where it tries to read the string from c client, the rest of the code works fine and the file is transmitted. 现在我已经验证了如果我删除服务器代码中它试图从c客户端读取字符串的部分,其余代码工作正常并传输文件。 But if do not comment the string receiving code, both the server and client keep waiting. 但如果不注释字符串接收代码,服务器和客户端都会等待。

I will be grateful if somebody solves this issue for me. 如果有人为我解决这个问题,我将不胜感激。

PS I know this question has been asked before but that didn't help me, so I started a new thread. PS我知道之前已经问过这个问题,但这对我没有帮助,所以我开始了一个新的主题。

br.readLine() wants to read a line. br.readLine()想要读一行。 The client never sends a newline, so the server is waiting for a newline... forever! 客户端永远不会发送换行符,因此服务器正在等待换行符......永远!

Add a newline to the command sent by the client: 在客户端发送的命令中添加换行符:

int n = write(sock_fd,"Ready\n", 6);

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

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