套接字编程:Java客户端-Ruby Server

时间:2018-06-21 08:08:30

标签: java android ruby sockets messaging

我正在尝试在Ruby服务器和Java客户端之间建立套接字连接。连接已成功建立,但在从服务器向客户端发送消息时遇到困难。

这是我的Ruby Server的样子:

class ServerSocket 
  loop do
    server = TCPServer.new(ip, port).accept
    while server
      line = server.recv(65000)

      puts "Message: #{line}"

      server.flush

      server.puts("Hi from server!")

      server.flush
    end 
  end
end

当我尝试使用此Java客户端时:

Socket socket = new Socket(InetAddress.getByName(ip), port);

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.write("Hello from client!");

socket.close();

建立连接,并成功地将消息从客户端发送到服务器。但是,当服务器到达此行时:

server.puts("Hi from server!")

它抛出此异常:

lib/server_socket.rb:11:in `write': Broken pipe (Errno::EPIPE)
    from lib/server_socket.rb:11:in `puts'
    from lib/server_socket.rb:11:in `block in <class:ServerSocket>'
    from lib/server_socket.rb:2:in `loop'
    from lib/server_socket.rb:2:in `<class:ServerSocket>'
    from lib/server_socket.rb:1:in `<top (required)>'
    from /home/dionis/.rvm/gems/ruby-2.4.1@sample/gems/railties-5.1.4/lib/rails/commands/runner/runner_command.rb:34:in `load'
    from /home/dionis/.rvm/gems/ruby-2.4.1@sample/gems/railties-5.1.4/lib/rails/commands/runner/runner_command.rb:34:in `perform'
    from /home/dionis/.rvm/gems/ruby-2.4.1@sample/gems/thor-0.20.0/lib/thor/command.rb:27:in `run'
    from /home/dionis/.rvm/gems/ruby-2.4.1@sample/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command'
    from /home/dionis/.rvm/gems/ruby-2.4.1@sample/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch'
    from /home/dionis/.rvm/gems/ruby-2.4.1@sample/gems/railties-5.1.4/lib/rails/command/base.rb:63:in `perform'
    from /home/dionis/.rvm/gems/ruby-2.4.1@sample/gems/railties-5.1.4/lib/rails/command.rb:44:in `invoke'
    from /home/dionis/.rvm/gems/ruby-2.4.1@sample/gems/railties-5.1.4/lib/rails/commands.rb:16:in `<top (required)>'
    from bin/rails:9:in `require'
    from bin/rails:9:in `<main>'

当我尝试使用此Java客户端时,另一种用户体验:

Socket socket = new Socket(InetAddress.getByName(ip), port);

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.write("Hello from client!");


BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String serverMsg = in.readLine();

System.out.println("Server: " + serverMsg);

socket.close();

在这里,我正在尝试从服务器获取消息。但是当客户到达这一行时:

String serverMsg = in.readLine();

它永远挂在那里。

有人知道如何处理吗?

编辑(完整的客户端代码)

ClientSocket.java

public class ClientSocket implements Runnable{

    @Override
    public void run() {
        System.out.println("Connecting...");
        try {

            Socket socket = new Socket(InetAddress.getByName(ip), port);

            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            out.write("Hello from client!");

            socket.close();

            System.out.println("Closing...");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Main.java

public class Main{

    public static void main(String[] args) {

        Thread cThread = new Thread(new ClientSocket());
        cThread.start();


    }

}

1 个答案:

答案 0 :(得分:0)

@Dionis我对Java不熟悉。 已经编写了Java客户端和ruby服务器,我想这会为您提供帮助。

Ruby TCP服务器代码:

require 'socket'
server = TCPServer.new(12345).accept
line = server.recv(50)
puts "Message from java Client: #{line}"
server.flush
server.puts("Hi from server!")
server.flush

Java TCP客户端代码:

import java.net.*;
import java.io.*;

class Main{

    public static void main(String[] args) throws Exception {

    Socket socket = new Socket("localhost", 12345);
    DataInputStream is = new DataInputStream(socket.getInputStream());
    DataOutputStream os = new DataOutputStream(socket.getOutputStream());
    os.writeBytes("Hello \n");
    String line = is.readLine();
    System.out.println("Msg from ruby Server :" + line);
    socket.close();
    }
}

让我知道是否可以解决问题 谢谢!