I have created a chat application using python sockets and Tkinter and it all works perfectly locally however the Client is unable to connect to the server remotely (when I enter my public IP address as the host) I have already fully port-forwarded my network and I know how to port forward very well and when I run an online Port Open scanner that checks if a port is open it states that the port is open!?
I have port-forwarded my router on a number of ports and updating the client and server accordingly however the client and telnet could still not connect.. I have also disabled all my windows firewalls and I disabled all of the routers firewalls.
Here is a very simple socket client and server model which I am trying to troubleshoot my problem using.
Server
import socket
s = socket.socket()
host='0.0.0.0'
port = 2000
s.bind((host,port))
s.listen(5)
while True:
c, addr = s.accept()
print ("Got a connection from: ", addr)
c.send(bytes("Thanks for connecting",'utf8'))
c.close()
Client
import socket
s = socket.socket()
port = 2000
s.connect(("109.156.114.183", port))
print (s.recv(1024))
s.close
Telnet
C:\Users\Maks>telnet 109.156.114.183 2000
Connecting To 109.156.114.183...Could not open connection to the host, on port 2000: Connect failed
When I try to connect to the server on 127.0.0.1 or localhost or from within my local network the client connects perfectly and telnet can connect as well. I am 200% sure I have port forwarded correctly because when I run a Port Open scan it says that the port(2000) is open.
Please help!
Thanks-
Maks
I'm at home so there's no firewalls running on my network at all.
That comment was the missing piece to solve your problem. You actually don't connect from remote as your question implies but you try to connect from inside your local network to the externally visible address of your router.
Such a setup is supported by some routers and not by others. It looks like you router does not support it. For more information on this see NAT hairpinning (or NAT loopback).
Related
Good day everyone! I'm still doing research on this so please pardon if I make any mistake. I'm currently working on a small project that need socket connection between 2 device, problem is, when ever I used the client and the server on the same device, it worked out okay. But when I moved the client into a different device, then started the process again(same LAN connection), it just gave me the time out error [WinError 10060]. Here is my code:
Server side:
HOST = '10.0.0.32'
PORT = 44132
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))
server.listen()
client, address = server.accept()
Client side:
HOST = '10.0.0.32'
PORT = 44132
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((HOST, PORT))
I have tried disabling the Firewall and restart computer, changing port or trying to check in cmd if server is really Listening or not, is there anything that I'm missing here? Thank you.
The full error report is: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
edit:
Here is also my port listening on 44132 using netstat:
Proto Local Address Foreign Address State
TCP 0.0.0.0:44132 0.0.0.0:* LISTENING
edit2: Another update on my end, I've tried turning off the firewall on target machine and ping it, the ping now went through successfully but the client and server still refused to reconnect and continue on timing out. Could it be that there is another firewall between my 2 devices and are implemented by the router to prevent the connection taking place?
Probably you are using the wrong ip address, my advice is to use the command arp -a to check if the server's ip is correct (if you have access to the router you could check there otherwise).
Moreover, be aware when you use socket.gethostbyname(socket.gethostname()), take a look here Python socket.gethostname
I have been experimenting with the socket library for python. I made a simple program for the server and client where the client can message the server.
Here is my code for the server:
import socket
print("Host")
socket_main = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_main.bind(('127.0.0.1', 9999))
socket_main.listen(1)
conn, addr = socket_main.accept()
while True:
data = conn.recv(1204).decode()
print(data)
conn.close()
Here is my code for the client
import socket
print("Client")
socket_main = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_main.connect(('127.0.0.1', 9999))
while True:
message = input(": ")
socket_main.send(message.encode())
socket_main.close()
When I run these programs in two different terminals on one computer it works just fine, but when I try to run the server and client on different computers I get an error on the clients end saying, "No connection could be made because the target machine actively refused it".
I have tried changing the port multiple times but it didn't help. I have looked through a lot of other forums and I haven't been able to fix this problem for a while now so I decided to ask here.
when I try to run the server and client on different computers I get an error on the clients end
That is because you are using127.0.0.1 on both sides. That is the localhost loopback IP address. It works when the client and server are on the same machine, but it is not routable on the LAN network.
You need to:
change the server to listen on either 0.0.0.0 (to listen on all installed network interfaces), or its actual LAN IP address (just the network interface attached to the LAN).
change the client to connect to the server's hostname or IP address on the LAN.
I have tried changing the port multiple times but it didn't help
The problem is nit with the port, but with the IP address.
Clients on the same LAN can connect to the server just fine using the server machine's IPv4 address. However, when I have the clients use my router's external ip and port forward down to the server machine, they cannot connect. Any idea, as to why? I created a server and client in GML using the same port forward and it connected just fine using the external ip, so I am assuming I am missing something needed in Python. Here is the stripped down version of the server code:
# Server variables
host = socket.gethostbyname(socket.gethostname())
port = 65000
max_connections = 100
timeout = 5
# Run Server
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind((host, port))
socket.settimeout(timeout)
socket.listen(max_connections)
run()
I've tried setting the server ip to "" and "0.0.0.0" to no avail. I also tried using the default gateway ip for kicks and giggles. Any advice is appreciated.
I messed up. I thought I had disabled Windows Defender's firewall, but I had only turned off real-time protection. Bah...that's what happens when you're going fast and not paying enough attention. Now it works just fine.
I am trying to setup a very simply sockets app. My server code is:
import socket
s = socket.socket()
host = socket.gethostname()
port = 1234
s.bind((host,port))
s.listen(5) #Here we wait for a client connection
while True:
c, addr = s.accept()
print "Got a connection from: ", addr
c.send("Thanks for connecting")
c.close()
I placed this file on my remote Linode server and run it using python server.py. I have checked that the port is open using nap:
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
1234/tcp open hotline
I now run the client.py on my local machine:
import socket # Import socket module
s = socket.socket() # Create a socket object
port = 1234 # Reserve a port for your service.
s.connect(("139.xxx.xx.xx", port))
print s.recv(1024)
s.close # Close the socket when done
However I am not getting any kind of activity or report of connection. Could someone give me some pointers to what I might have to do? Do I need to include the hostname in the IP address I specify in the client.py? Any help would be really appreciated!
I've just summarize our comments, so your problem is this:
When you trying to using the client program connect to the server via the Internet, not LAN.
You should configure the
port mapping on your router.
And however, you just need configure the
port mapping for your server machine.
After you did that, then you can use the client program connect to your server prigram.
i already have a post which is quite similiar, but i am getting more and more frustrated because it seems nothing is wrong with my network setup. Other software can be seen from the outside (netcat listen servers etc.) but not my scripts.. How can this be??
Note: It works on LAN but not over the internet.
Server:
import socket
host = ''
port = 80001
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen(1)
print 'Listening..'
conn, addr = s.accept()
print 'is up and running.'
print addr, 'connected.'
s.close()
print 'shut down.'
Client:
import socket
host = '80.xxx.xxx.xxx'
port = 80001
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))
s.close()
Somebody please help me.
Any help is greatly appreciated.
Jake
Edited again to add:
I think you may be missing some basics on socket communication. In order for sockets to work, you need to ensure that the sockets on both your client and server will meet. With your latest revision, your server is now bound to port 63001, but on the local loopback adapter: 127.0.0.1
Computers have multiple network adapters, at least 2: one is the local loopback, which allows you to make network connections to the same machine in a fast, performant manner (for testing, ipc etc), and a network adapter that lets you connect to an actual network. Many computers may have many more adapters (virtual adapters for vlans, wireless vs wired adapters etc), but they will have at least 2.
So in your server application, you need to instruct it to bind the socket to the proper network adapter.
host = ''
port = 63001
bind(host,port)
What this does in python is binds the socket to the loopback adapter (or 127.0.0.1/localhost).
In your client application you have:
host = '80.xxx.xxx.xxx'
port = 63001
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))
Now what your client attempts to do is to connect to a socket to port 63001 on 80.xxx.xxx.xxx (which is your wireless internet adapter).
Since your server is listening on your loopback adapter, and your client is trying to connect on your wireless adapter, it's failing, because the two ends don't meet.
So you have two solutions here:
Change the client to connect to localhost by host = 127.0.0.1
Change the server to bind to your internet adapter by changing host = 80.xxx.xxx.xxx
Now the first solution, using localhost, will only work when your server and client are on the same machine. Localhost always points back to itself (hence loopback), no matter what machine you try. So if/when you decide to take your client/server to the internet, you will have to bind to a network adapter that is on the internet.
Edited to add:**
Okay with your latest revision it still won't work because 65535 is the largest post available.
Answer below was to the original revision of the question.
In your code posted, you're listening (bound) on port 63001, but your client application is trying to connect to port 80. Thats why your client can't talk to your server. Your client needs to connect using port 63001 not port 80.
Also, unless you're running an HTTP server (or your python server will handle HTTP requests), you really shouldn't bind to port 80.
In your client code change:
import socket
host = '80.xxx.xxx.xxx'
port = 63001
And in your Server Code:
import socket
host = ''
port = 63001
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostbyname(socket.gethostname()), port ))
In your server script you have port = 80, but you don't ever use it. It looks like the server is listening on 63001. And the client is connecting to 80.
If you're going to use 80, make sure you don't have an http server trying to use the port at the same time as well.