I'am learning ruby socket programming and I'am on linux OS(Ubuntu 16.04 to be exact).
The following code:
require 'socket'
socket = TCPSocket.new('0.0.0.0', 8080)
client = socket.accept
puts "New client! #{client}"
client.write("Hello from server")
client.close
is giving me the following error:
sock2.rb:3:in initialize': Connection refused - connect(2) for "0.0.0.0" port 8080 (Errno::ECONNREFUSED)
from sock2.rb:3:innew'
from sock2.rb:3:in `'
Should I use a different IP and port? or do I have to make some sort of configuration on my unix system for the client and server to speak to each other.
TCPSocket.new tries to connect to the host specified in the arguments. It looks like you are trying to set up a server listening on that port, for that you should use TCPServer instead:
socket = TCPServer.new('0.0.0.0', 8080)
You can then connect to this server with another client. That client could possibly use TCPSocket.
Related
I'm trying to use the library HTTParty, but whenever I run the code below I receive an error.
Code:
require 'httparty'
response = HTTParty.get('http://example.com')
When I run the code I receive the error: Errno::ECONNREFUSED (Failed to open TCP connection to :80 (Connection refused - connect(2) for nil port 80)).
I don't get any error when I run the same code in net/http. I don't know if this helps, but the system I'm running is Linux Mint 18.3 Cinnamon 64-bit .
I know that this is late, but using https://example.com instead of http://example.com was the solution to my problem.
I am running an Akka HTTP service on port 8080 in my EC2 instance. It expects web socket connections, but normal HTTP should return 400 bad request.
I use this code to start Akka service:
Http().bindAndHandle(route, "localhost", 8080)
From the remote terminal I have this expected behavior, but not from outside.
$ wget ec2-XX-XXX-XX-XXX.compute-1.amazonaws.com:8080
--2017-10-01 15:27:31-- http://ec2-XX-XXX-XX-XXX.compute-1.amazonaws.com:8080/
Resolving ec2-XX-XXX-XX-XXX.compute-1.amazonaws.com... XX.XXX.XX.XXX
Connecting to ec2-XX-XXX-XX-XXX.compute-1.amazonaws.com|XX.XXX.XX.XXX|:8080... failed: Connection refused.
Here is my inbound rules configuration:
If I do netstat --listen -p the port 8080 does not appear.
Thanks!
I found out the problem. I should be binding the service to the private IP address of the EC2 instance.
Recently Iv'e been trying to program a simple TCP server to later build into a chat room. But every time I launch the server (server.rb), and then I try to use the client (client.rb) I get this error:
Sam#ANDERSAMERPC C:\Users\Sam\Documents\Coding
> client.rb
C:/Users/Sam/Documents/Coding/client.rb:6:in `initialize': No connection could be made because the target machine actively refused it. - connect(2) for "localhost" port 2001 (Errno::ECONNREFUSED)
from C:/Users/Sam/Documents/Coding/client.rb:6:in `open'
from C:/Users/Sam/Documents/Coding/client.rb:6:in `<main>'
I am using CMD to run this and I've tried turning off firewall briefly.
Here is the code for both of the programs...
This is server.rb
require 'socket'
server = TCPServer.open(2000) # Socket to listen on port 2000
loop {
Thread.start(server.accept) do |client|
client.puts(Time.now.ctime) # Send time to the client
client.puts "Closing connection. Bye!"
client.close
end
}
Here's client.rb:
require 'socket'
hostname = "localhost"
port = 2000
s = TCPSocket.open(hostname, port)
while line = s.gets # Reads lines from socket
puts line.chop # And print with platform line terminator
end
s.close # Close socket when done
(This code is from http://www.tutorialspoint.com/ruby/ruby_socket_programming.htm just so you know.)
I guess there are 2 options:
Run your server.rb before client.rb.
Are you sure those are the exact same files that you are running. Because the error message says it can not connect to port 2001, whereas in both of your files they refer to 2000. Is it possible that you are editing and running different files? You will be surprised, how common this is here on SO :)
Can't make TCPSocket connect to TCPServer:
Client
require 'socket'
require 'io/console'
TCPSocket.open('127.0.0.1', '2000') { |socket|
#socket.puts('abcdefghijklmn')
puts socket.read
}
Server
require 'socket' # Get sockets from stdlib
server = TCPServer.open(2000) # Socket to listen on port 2000
loop { # Servers run forever
Thread.start(server.accept) do |client|
client.puts(Time.now.ctime) # Send the time to the client
client.puts "Closing the connection. Bye!"
client.close # Disconnect from the client
end
}
Error:
send_socket.rb:4:in `initialize': No connection could be made because the target machine actively refused it. - connect(2) (Errno::ECONNREFUSED)
from send_socket.rb:4:in `open'
from send_socket.rb:4:in `<main>'
When connecting to port 80 where IIS is listeninig, TCPSocket works fine, so I suppose there is something wrong with TCPServer example.
Switching the firewall off doesn't help. Moreover - when I stop IIS and set TCPServer to port 80 error is the same.
I am going through exercises in the book "Sinatra Up & Running" and am trying to send HTTP messages to Sinatra using Telnet.
This is what I am trying to do
[~]$ telnet 0.0.0.0 4567
However, I get the error:
Trying 0.0.0.0...
telnet: connect to address 0.0.0.0: Connection refused
telnet: Unable to connect to remote host
Sinatra is listening on localhost:4567 instead of 0.0.0.0:4567 and I think that is indicative of the problem.
I found some documentation at http://www.sinatrarb.com/configuration.html that talks about being able to specifically configure the development environment to listen on 0.0.0.0. I passed in:
ruby server.rb -o set :bind, '0.0.0.0'
And was able to adjust where Sinatra was listening to 0.0.0.0:4567 but telnet 0.0.0.0 4567 still gives the same error messages.
When you tell the server to listen on 0.0.0.0, that isn't actually a specific address, you're really telling it to bind to all available network interfaces. To connect to it, use either 127.0.0.1 or localhost, which are special addresses that always mean "this host":
telnet 127.0.0.1 4567
If you bind Sinatra to 0.0.0.0:4567 and it is listening they you will be able to externally hit your machine via the machine's ip.
E.g. machine-01 (192.168.122.100) is running Sinatra, which is bound to 0.0.0.0:4567 then from machine-02 (192.168.122.200) you'll be able to telent 192.168.122.100:4567 (provided there are no firewalls).
As the other post suggested, if you're trying to do it all on machine-01, then you want to bind Sinatra to localhost:4567 (127.0.0.1:4567), then you will be able to telenet localhost:4567