I'm trying to socket connection between a Ruby Server and a Java Client. The connection is setup with success but I'm finding difficulties when sending a message from server to client.
This is how my Ruby Server looks like:
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
When I try with this Java Client:
Socket socket = new Socket(InetAddress.getByName(ip), port);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.write("Hello from client!");
socket.close();
the connection is established and the message is sent with success from client to server. But when the server reaches this line:
server.puts("Hi from server!")
it throws this exception:
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>'
Another user experience is when I try with this Java Client:
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();
Here, I'm trying to get the message from the server. But when the client reaches this line:
String serverMsg = in.readLine();
it just hangs there forever.
Does anyone know how to deal with this?
EDIT (full client code)
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();
}
}
@Dionis I am not familiar with java. Have written a java client and ruby server I guess this will help you.
Ruby TCP server code:
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 Client code:
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();
}
}
Let me know if resolve the issue Thanks!