如何从Java服务器实时将数据实时发送到C#客户端
我正在研究一个项目,C#客户端将在其中将命令发送到Java服务器,Java Server将在连接的手机中执行命令,并将输出发送到C#客户端。 我现有的代码将数据发送到服务器,并且服务器在设备中执行并在终端中输出。 但是在这里,我想在接收到的设备收到数据后立即发送数据。 如果我执行LS命令,则客户端应实时获取每个文件名。 我的代码要从客户端接收数据
ServerSocket ss = new ServerSocket(3000);
Socket s = null;
try
{
// socket object to receive incoming client requests
s = ss.accept();
scanner = new Scanner(s.getInputStream());
pw = new PrintWriter(s.getOutputStream(), true) ;
dataFromClient = scanner.nextLine();
//pw.write("thanks") // this is working
}
以发送数据我创建了一种方法
public static void senddata(String toreturn)
{
pw.write(toreturn);
}
并从这里调用它
Runtime run = Runtime.getRuntime();
Process pr = run.exec(cmd);
pr.waitFor();
BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = "";
while ((line=buf.readLine())!=null) {
ClientHandler2.senddata(line); //sending data
output=output+System.lineSeparator()+line;
previuseline=line;
}
,但是我在客户端的服务器中没有收到任何东西。 我真的不知道该如何实现这一目标,因为我的输出始终是100至500行。 请提出同样的任何想法。
I'm working on a project where the c# client will send a command to the java server and the java server will execute the command in the connected mobile phone and send the output to the c# client.
My existing code sends the data to the server and the server executes it in the device and gives the output in the terminal.
But here I want to send the data as soon as I receive it from the connected device.
If I execute the ls command then the client should get each filename in real-time.
My code to receive data from client
ServerSocket ss = new ServerSocket(3000);
Socket s = null;
try
{
// socket object to receive incoming client requests
s = ss.accept();
scanner = new Scanner(s.getInputStream());
pw = new PrintWriter(s.getOutputStream(), true) ;
dataFromClient = scanner.nextLine();
//pw.write("thanks") // this is working
}
to send data I created a method
public static void senddata(String toreturn)
{
pw.write(toreturn);
}
and call it from here like this
Runtime run = Runtime.getRuntime();
Process pr = run.exec(cmd);
pr.waitFor();
BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = "";
while ((line=buf.readLine())!=null) {
ClientHandler2.senddata(line); //sending data
output=output+System.lineSeparator()+line;
previuseline=line;
}
But I don't receive anything from the server at the client.
I really don't know how to achieve this as my output is always a minimum of 100 to 500 lines.
please suggest any idea on the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论