Trying to get into sockets and networking.
I wrote some simple server and client scripts and ran them,
which was working when connected locally and client-server communicated just fine, yet when my client script tries to connect to my_external_ip:open_port it gets a "Connection Refused" [WinError 10061]
I've opened a port (5234 in this case) and checked it using those port-scanning sites, what the server seems to react to and even accept connections.
Yet when I run my client script, it throws an exception, and the server doesn't seem to respond or even be aware of the connection attempt.
I've shut down my firewall temporarily and made sure I'm listening on 0.0.0.0:5234 (which to my understanding should be what I'm doing).
Am I missing something? doesn't make sense to me that the script runs locally, the server takes incoming external connections, yet this doesn't work.
Maybe the problem is that the client's outbound connection attempt is somehow blocked?
I cleaned up some unrelated code, but that's about it:
SERVER:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server = "" #Also tried socket.gethostname() and 0.0.0.0
port = 5234
s.bind((server,port))
s.listen()
connection, address = s.accept()
CLIENT:
def __init__(self):
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server = my_public_ip
self.port = 5234
self.client.connect((self.server,self.port)
Related
I'm experimenting with socket server-client communication using python 3 socket module. Just to test if an eventual connection can be made, I've tried to program the simplest scenario possible. Thus the server code has the only purpose to print new connections; its code is:
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("0.0.0.0", 30000))
while True:
server.listen()
print("listening...")
conn, addr = server.accept()
print(f"{addr} connected")
while the client only tries to connect to the server using the public IPv4 address of my computer:
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Trying to connect...")
client.connect(("99.99.99.99", 30000))
client.close()
When I try to run the server and then the client nothing happens: the server just waits for conections, while the client displays nothing. After a while on the client side it is displayed the socket timeout error. I've tried to establish the connection even running the server and the client on the same computer, I've tried changing the ip of the server to "localhost" or "127.0.0.1", but nothing happens. Have you got any ideas on how this issue can be solved?
P.S. Thank you in advance for eventual answers and excuse my poor English, I'm still practising it!
I've been trying to implement a server-client connection, here is my code-
Server.py
import socket
server = socket.socket()
server.bind(('', 2112))
server.listen(5)
print('Server created with port 2112')
server.accept()
print('Connected')
Client.py
import socket
##Public_IP is the Public IP address of my router
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((Public_IP, 2112))
Both Server.py and Client.py are running on my computer(server). It works just fine when I replace Public_IP with '192.168.0.143', but when I use Public_IP, it gives the following error-
TimeoutError: [WinError 10060] 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
Is it because I'm running both Server.py and Client.py on the same computer? Please let me know what I'm doing wrong.
Setting up port forwarding -
Public IP
Port forwarding
Private IP address of server
I followed this tutorial for setting up port forwarding.
I also tried replacing the empty string in Server.py with my private IP address-
server.bind(('192.168.0.143', 2112))
Edit: I have tried-
Running Client.py externally (on a different LAN) with firewall down on both the ends, but didn't make any difference (it gave a timeout error).
Running Client.py on the same LAN with firewall down, that seemed to work fine.
Running the Client.py on the same device also worked fine with the firewall down.
[WinError 10061] and [WinError 10060]. These are the errors im getting when i send a client app to my friend. Remotely it just doesn't work. Locally it works fine. Can i get a full explanation step by step please cause i couldn't find any soulution and i'm getting mad.
SERVER:
import socket as s
HOST = 'ppp.ppp.p.ppp'
PORT = 33000
server_socket = s.socket(s.AF_INET, s.SOCK_STREAM)
server_socket.bind((HOST, PORT))
server_socket.listen()
while True:
client_socket, address = server_socket.accept()
print(f'Connected {address}')
CLIENT:
import socket as s
HOST = 'ppp.ppp.p.ppp'
PORT = 33000
client_socket = s.socket(s.AF_INET, s.SOCK_STREAM)
client_socket.connect((HOST, PORT)) # here is error
PS: CLIENT GETS ERRORS NOT SERVER
welcome to SO. You and your friend are probably not on the same network. Therefore, when sending a connection request, your router (or your friends) does not know to relay the packet to the correct device in it's network. There are multiple things you need to do:
Make sure that the IP is not your local network IP but rather the IP of your router. check on wahtismyip. Use that as the ip. Note that the ip will change every so often.
Then, the difficult part. You need to tell your router (or your friends if the server is in his network) to relay the packets arriving at port 33000 to the server. This is different from device to device, try googling "Port forwarding ".
Hopefully this resolves your issues.
I've been working on a project that requires a bit of networking between a server (hosted on GCE) and multiple clients. I created a Compute Engine Instance to run a Python script as shown in this video: https://www.youtube.com/watch?v=5OL7fu2R4M8.
Here is my server-side script:
server = socket.gethostbyname(socket.gethostname()) # 10.128.X.XXX which is the Internal IP
print(server)
port = 5555
clients = 0
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((server, port))
s.listen(2)
print("Waiting for connection...")
while True:
conn, addr = s.accept()
print("Connected to: ", addr)
conn.send(str.encode(f"{clients}"))
clients += 1
and here is my client side-script:
class Network:
def __init__(self):
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server = "10.128.0.2"
self.port = 5555
self.addr = (self.server, self.port)
self.id = int(self.connect())
def connect(self):
self.client.connect(self.addr)
return self.client.recv(2048).decode()
network = Network()
print(f"Connected as client {network.id}")
I know this script works because I have tested it with my computer being the server and 1 client, and another computer being the 2nd client. But when I use the GCE as the server, I get this error in the client script:
TimeoutError: [WinError 10060] 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
Could this be because I am using the internal IP address and not the external?
After this, I tried changing the firewall settings (added 'python-socket') of the GCE and this is what they look like:
But the error still persists...
As answered by W_B, I tried to run these commands on my VM and got the following outputs:
From your description it's evident it's the connection problem.
First of all you have to check if the firewall rule you created is still there. If it's "too broad" and allows for very wide access then it might be removed automatically even without you knowing it. It's on you'r screenshot but check it again just to be sure.
If it's there select the protocol you're goint to be using (I assume it's TCP) - some protocols are always blocked by default by GCP (you can't change this) so creating a rule with "any protocol" allowed is risky. Also - put one or two target IP's (not all inside this VPC) - this is not a must but improves security of your network.
Second - make sure port 5555 you're trying to connect to is accessible from other computers. You can scan the target host with nmap -p 5554 put.server.ip.here
You can scan it from the Internet or other VM's in the same VPC network.
You should get something like this:
root#localhost:~$ nmap -p 443 192.168.1.6
Starting Nmap 7.70 ( https://nmap.org ) at 2020-06-25 17:12 UTC
Nmap scan report for 192.168.1.6
Host is up (0.00091s latency).
PORT STATE SERVICE
443/tcp open https
Nmap done: 1 IP address (1 host up) scanned in 0.04 seconds
If you see 5555/tcp filtered freeciv this means that something blocks the port.
Run nmap on the server (I assume you run some version of Linux) and if you don't want to install any non-essencial software you can use sudo netstat -tulpn | grep LISTEN to get a list of open ports (5555 should be on the list).
Also make sure firewall on your server doesn't block this port. You can use iptables for that.
My script is launching multiple clients in a machine-1 and tries to connect to a server running on different machines on a specific port. These machines running the server are launched by boto3 and then wait for 5 min for the instances to be in running state. Then a sftp connection is made to transfer files. Then through ssh the server script is executed. Then when multiple clients are launched in machine-1, and it tries to connect to the server, only one connection established and others got connection refused error ConnectionRefusedError.
Here is my code:
Client:
def Client(datas):
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((datas[0], 81))
datasent =''+datas[1]+'-'+datas[2]+'#'
client.send(datasent.encode("UTF-8"))
for dataToRun in dataForMP:
hp = Process(target=Client, args=(dataToRun,))
hp.start()
hp.join()
Server:
if __name__ == '__main__':
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setblocking(0)
sock.bind(("", 81))
sock.listen(128)
I am launching t1.micro instances for server. I have also waited for 10 min before starting the client and server after launching the instances but I'm getting the same error.
The issue was regarding the time mismatch between starting the server and client. Actually its takes little while to start the server and in the meantime the client request to connect. So I put a 2 sec delay before starting the client and the issue resolved.