I am trying to learn how to send TCP packets across the internet to another computer. So say, computer 1 sends data across the internet to computer 2 (using TCP). Assuming that both computer have port forwarding correctly set, how would I go about establishing a TCP connection between the two computers (in Ruby preferably)? I have it working on my LAN, but when I try over the internet, it doesn't seem to work.
My attempt (basically):
Computer 1:
server = TCPServer.new 32500
client = server.accept
Computer 2:
TCPSocket.new PUBLIC_IP_OF_COMPUTER_1, 32500
Problem is that TCPSocket never connects to TCPServer.
I read in the Ruby doc that TCPServer.new's syntax is
new(remote_host, remote_port, local_host=nil, local_port=nil)
What happens if I just leave local_host=nil and local_port=nil (rather than assign them the private IP and port 32500 number on Computer 1)?
If somebody could point me in the right direction, that's be great! I hope I my approach is at least somewhat correct.
You created a server which is listening only on the loopback Interface.
Try this out:
curl ifconfig.me
You will get your external IP address, how it is visible from outside, for example 123.123.123.123
server = TCPServer.new 2000
Now You have a server listening on port 2000
lsof -i :2000
for example:
ruby 37186 wopi 6u IPv4 0xcf0818acc2bdc38d 0t0 TCP *:callbook (LISTEN)
now connect from THE SAME machine to localhost
telnet localhost 2000
this works
telnet 123.123.123.123 2000 # substitute your real external IP address
this will not work
That is why You can't connect from outside.
Checkout how ngrep, netcat and tcpdump are working, invaluable tools for network debugging.
Related
I have installed the peerjs server on my windows server as per github, and successfully get the message:
“Started PeerServer on ::, port: 9000, path: /myapp”.
I then opened port 9000 in the windows firewall, and find the following two entries in the netstat listing:-
TCP 0.0.0.0:9000 vmBCE3892:0 LISTENING
[node.exe]
TCP [::]:9000 vmBCE3892:0 LISTENING
[node.exe]
My server has 2 different IP addresses for 2 different web applications under IIS.
To check peerjs connectivity I used telnet as follows:-
telnet ipaddress 9000
with each IP address, neither would connect.
I cannot see how to tie the peerjs server to one of the ipaddresses or the corresponding domain name.
It seems that my ISP blocks all ports other than 80 & 443, despite them appearing to be available in my firewall. As for the two IP adresses, either would have reached peerjs server had the port been open.
I want to get the port number and ip address of a server
For ip address i can
ping the xyz.com and to get the ip address resolved.
but how do i include the port number
From this Serverfault answer:
You can't ping ports, as Ping is using ICMP which doesn't have the
concept of ports. Ports belong to the transport layer protocols like
TCP and UDP. However, you could use nmap to see whether ports are open
or not
nmap -p 80 example.com
Edit: As flokra mentioned, nmap is more than
just a ping-for-ports-thingy. It's the security auditers and hackers
best friend and comes with tons of cool options. Check the doc for all
possible flags.
Try:
telnet xyz.com 80
If it's showing output like
Connected to xyz.com.Escape character is '^]'.
Means your domain (xyz.com) is listening on port 80.
Specifically, I have a Windows server (Windows 7), but the netstat -an command only shows whether ports are TCP or UDP. I thought these were the only kinds of ports, but node.js seems to distinguish between HTTP ports and TCP ports (example at bottom of linked page). I'm currently using node.js in a program that will run on my server, and it opens HTTP ports by default. These appear as TCP ports under netstat -an.
Is there a command line trick for distinguishing whether an open port on this server is HTTP or TCP? I make requests to my Information Technology office about ports that I need on this server, and they need to know whether these ports will be TCP, UDP, etc.
If necessary to use a remote client, I have a Mac that can do the job.
HTTP is an application protocol. Its definition presumes an underlying and reliable transport layer protocol. The transmission Control Protocol is commonly used. However HTTP can use unreliable protocols too (example SSDP).
Now to answer to your question:
netstat -lt : List TCP Listening Ports
netstat -lu : List UDP Listening Ports
If you want to know wether a TCP Port is running HTTP or not, you can check the standard port on HTTP (grep :80). The standard HTTP port is 80. The standard HTTPS port is 443.
All HTTP traffic is transmitted over TCP Ports. I think what may be causing some confusion is that the first node.js example uses the http module and the second example uses the net module.
The http module is built on top of the net module (which handles the TCP layer underlying HTTP).
Refer to: nodejs - net or http module
EDIT:
Only one process can bind to a port. If you're having difficulties connecting, shut down any other applications that may be using the same port, such as your Java Hello World server. Run netstat -an to make sure you don't see the port listed that you're trying to listen on in you node.js TCP server (port 1337) in the example. If nothing is listening on the port, fire up your node.js TCP server and run netstat -an again to make sure it's actually listening on the intended port.
Another suggestion - temporarily turn off the software firewall then use the telnet client on the local server to attempt to connect to the port using the command telnet 127.0.0.1 1337 (replace 1337 with your port) from the command prompt. This will eliminate any network devices such as firewalls between the client (telnet in this case) and the server. If the screen goes blank, you've connected. If you get the message Could not open connection to the host, on port then it's still not listening on the TCP Port. If you can connect locally from Telnet but you cannot connect remotely then there is something blocking your connection.
I have a client which is intended to connect to a server. For the client, the remote TCP port number is fixed(i.e. 102). I can NOT change it(while I can change the remote IP address). However, the TCP Port number the server is listening on is fixed as well(i.e. 1024) and I can NOT change it too. These two port numbers are different. I want to make the client connect to the server smoothly.
At the first, I had a idea that I setup a proxy listening on localhost:102 and the client connect to 127.0.0.1:102. Then this proxy redirect these TCP traffic to the real address RemoteServerIP:1024. But I found on my windows, there was already a process which is listening on 0.0.0.0:102 and I can NOT change its listening port. So this idea can NOT work.
Thank you very much.
if you cannot do it on the same windows machine running client, why not try to do it on another (linux maybe) machine?
Can I establish more than 1 TCP connections to same server on same port ?
For example :
Connection 1: machine A to Machine B on port 445
Connection 2: machine A to Machine B on port 445
Without terminating previously established connection 1.
If yes, do i need to do any setting to enable such scenario ?
Yes, you can. Without any special setting.
That's exactly what happens when you start, let's say Internet Explorer and Mozilla Firefox and navigate to the same site with both.
Behind the scene to connect to the server, machine A opens a socket using whatever port number and tries to connect to machine B port 445. Machine B, who was listening on port 445 can accept this incoming connection, and uses another socket using whatever other port number to connect it to. In the end, the actual TCP connection is established between whatever port en machine A and whatever port on machine B. On machine B, port 445 is left for listening only (and can accept other connections).
Yes you can, a TCP/IP session is identified by the following 5-tuple:
(src-ip, src-port, dst-ip, dst-port, protocol).
In your case, 4 of the 5 values have to be the same: src-ip (A), dst-ip (B), dst-port (445), protocol (tcp). As such, the src-port still gives you a degree of freedom, by variating this, you can set up multiple sessions. This is what should happen by default, your OS will select a new, unique source port for each connection to the same server.
TCP/IP standard supports this with ephemeral ports. You connect to fixed port on server side, but port on client side is not under your control. TCP/IP stack will choose client port, and connection is then defined and distinguished from other connections with 4 bits of information: client IP address, client port, server IP address, and server port.