Ruby - How to send a text string from a TCP Server to a client on a different computer with Ruby? - ruby

I've been trying to learn this for days and nothing is working. When I write code like this
for the client.
require 'socket'
tcp_client=TCPSocket.new('localhost',2000)
while grab_string=tcp_client.gets
puts(grab_string)
end
tcp_client.close
and code like this for the server.
# Set up a server program.
require 'socket'
tcp_server=TCPServer.new(2000)
loop{
waiting_client=tcp_server.accept
path='C:\Users\Nil_Face\Music\Bad Religion - Anxiety.mp3'
test=File.new(path,'rb')
waiting_client.puts(File.basename(path))
test.close
waiting_client.close
}
It works just fine but when I use that server on my desktop and try to get that text string to appear on the client located on a different computer. It doesn't work. Why won't the client connect to the server when their both on different computers? And how can I get them to?

Your client is connecting to 'localhost', which is the same computer where the client runs.
tcp_client=TCPSocket.new('localhost',2000)
The client should be connecting to the hostname/ip of the machine on which the server is running. Change 'localhost' with the IPv4 address / inet addr of the server machine, in this case '192.168.1.10'.
On windows:
ipconfig
Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix . : local.domain
IPv4 Address. . . . . . . . . . . : 192.168.1.10
On Linux:
# /sbin/ifconfig
eth0 Link encap:Ethernet HWaddr FF:FF:FF:FF:FF:FF
inet addr:192.168.1.10 Bcast:192.168.1.255 Mask:255.255.255.0
If you're getting connection timed out. You can try connecting with telnet from the command prompt:
telnet 192.168.1.10 2000
From the server computer, you can also verify that the server is running and is accepting connections with netstat:
netstat -an
Active Connections
Proto Local Address Foreign Address State
TCP 0.0.0.0:2000 0.0.0.0:0 LISTENING
If those are all correct, you probably have a firewall that is blocking the connection.

Related

Why doesn't this telnet command connect to this socket "server IP and SMTP port" in command line

C:\Users\Kyle>telnet 10.0.0.1 25
The response that I get is:
Connecting To 10.0.0.1...Could not open connection to the host, on port 25: Connect failed
Some ISP will block port 25 due to spamming concern. Consider to try with port 587 instead.
10.0.0.1 is a private IP address. You can only connect directly to that IP address if there is a server at that address on your local network. You should verify that there is a server at that IP address, and that the server is running a program that is listening for connections on port 25.

Able to open TCP port but not listening

Using Add rule in windows firewall, I was able to open TCP port 15537. When i am trying to executing command netstat -ano on terminal windows, this port is not listed. I tried to execute telnet command on terminal window (e.g. telnet IP port) but getting
Connecting To localhost...Could not open connection to the host, on port 15537: Connect failed
Then I downloaded PortQry application and execute it from different machine, this machine is also in the same network, the result I received was
"Not Listening".
I already spent more than 2 days and asked internal group but could not find solution.
Note: both machines are having Windows 10 OS.
No solution is needed as no problem is indicated in the question. You have opened a TCP port successfully. You have not made any attempt to cause anything to listen to that TCP port.
It's not clear what results you expected, but you got the results that you should have expected. Nothing is wrong. The port is open because you opened it. Nothing is listening on that port because you didn't set anything to listen on that port.
There may be some forwarding rules? Since the purpose of access is not on the local machine, the netstat command cannot see the port on listening, but it can see the next action based on this port, usually to do some forwarding
I am not very familiar with windows firewall configuration, but I know that if there is a forwarding rule in linux, like
-p tcp -m tcp --dport 8080 -j {other forwading chain}
we can not see 8080 listening on this host (netstat -tunpl), but telnet host:8080 may see connected
Use nmap instead of netstat for detecting opening port
nmap -p your_port_number your_local_ip
Run service on that port
For eg- In my case,in order to open port,I use
"service ssh start" or "service apache2 start "and it's open port 22 and 80 for connection respectively in my linux machine.
On using nmap in my lan network both ports opened.
Hope it help

how to forward request to docker machine in local network in OSX

I am currently running a docker machine in local network that its IP & port is 192.168.99.100:8080
right now. I want to connect this machine from somewhere out of the local network and access it, so the best way I found is to NAT the sent request from my laptop computer to docker machine.
In other words, each request that has been sent to my laptop computer will be sent to the docker machine on its own IP and port!
I use pfctl to do this but when I check the port 8585 to find out which service is listening to this port, I can't find anything and it sounds like the requests do not arrive(get to) the destination!
I did as below:
1- csrutil disable
2- my pf role :
/etc/pf.anchors/mazafard:
rdr pass on en0 inet proto tcp from any to 192.168.20.203 port 8585 -> 192.168.99.100 port 8080
3- my pf conf
/etc/pf-mazafard.conf
rdr-anchor "forwarding"
load anchor "forwarding" from "/etc/pf.anchors/mazafard"
and
sudo pfctl -vnf /etc/pf.anchors/mazafard
pfctl: Use of -f option, could result in flushing of rules
present in the main ruleset added by the system at startup.
See /etc/pf.conf for further details.
rdr pass on en0 inet proto tcp from any to 192.168.20.203 port = 8585 -> 192.168.99.100 port 8080
and
sudo pfctl -evf /etc/pf.anchors/mazafard
fctl: Use of -f option, could result in flushing of rules
present in the main ruleset added by the system at startup.
See /etc/pf.conf for further details.
No ALTQ support in kernel
ALTQ related functions disabled
rdr pass on en0 inet proto tcp from any to 192.168.20.203 port = 8585 -> 192.168.99.100 port 8080
pfctl: pf already enabled
and
telnet 192.168.20.203 8585
Trying 192.168.20.203...
telnet: connect to address 192.168.20.203: Operation timed out
telnet: Unable to connect to remote host
My OSX v: 10.11.4
Docker version 1.9.1, build a34a1d5
Boot2Docker version 1.11.0-rc3, build

How to open incoming port 50070 in firewall (google compute engine)

I have my Single node Hadoop installed on Google Compute Engine instance and i want to open port 50070 on that machine to access the hadoop dashboard. i configured in the firewall rule as tcp:50070 in compute engine networks. but still i am unable to access my port outside the network (ie . via internet). I tried nmap for the public ip of my GCE instance and i got a result which has only ssh port got opened all other ports are filtered .
Note: i am using debian 7.5 image
Make sure your daemon is listening on port 50070. If you have more than one networks in you project make sure the port is opened on the right network. You can run the following commands to check the information about your instance and network.
lsof -i
gcutil --project= getinstance
gcutil --project= listnetworks
gcutil --project= listfirewalls
gcutil --project= getfirewall
Check if IP/Port is allowed in iptables or not.
iptables -L
would show you all the records.
To allow port in iptables you can do the following:
sudo iptables -A INPUT -p tcp -m tcp --dport 50070 -j ACCEPT
sudo iptables-save -c
Short answer
In addition to configure the firewall rule at GCE web console make sure that your server is listening at 0.0.0.0 instead of 127.0.0.1
Long answer
In the context of servers, 0.0.0.0 means all IPv4 addresses on the local machine. If a host has two IP addresses, 192.168.1.1 and 10.1.2.1, and a server running on the host listens on 0.0.0.0, it will be reachable at both of those IPs - Source
In contrast 127.0.0.1 is the IP address used to stablish a connection to the same machine used by the user this address is usually referred as the localhost.
It's often used when you want a network-capable application to only serve clients on the same host. A process that is listening on 127.0.0.1 for connections will only receive local connections on that socket. - Source
Hence, if you try to stablish a connection to your server from internet and your server is listening at 127.0.0.1 at your GCE machine, then, from the server point of view a request has never been received and as a consequence Goocle Cloud Firewall will refuse the connection because there is no server listening at the opened port (in your case 50070).
I hope this answer helps to solve your problem. Best regards.

Modify destination port in TCP/IP headers of all outgoing packets

Used SSH Tunnel to route the traffic addressed to server1:port1 to server2:port2.
Now,the problem is that i want to redirect all TCP/IP packets from desktop addressed to server1:port1 to server2:port2.
using "hosts" file on windows, i mapped server1 ip as ipaddress of server2. [local DNS mapping]
http://server2:port2 //gives the desired page.
http://server1:port2 //gives the desired page as server1 ip is mapped as that of server2 ip.
Is it possible to rewrite the destination port of all TCP/IP packets addressed to some host?
[Transform destination port all TCP/IP packets with (destn ip as server1 and destn port as port1) to port2]
This is required as there is no direct access to server1 from the working desktop.
I can't use the same port number on server2 as that of server1 as that port is already taken on server2.
Please share your comments on this.
You can use iptables to rewrite the destination port either on the desktop machine or on an intermediate router.

Resources