Im using python socket to connect two computers,
it completely works when both systems are connected to one mobile hotspots or LAN,
But when i use two diffrent hotspots or try to connect to my friend's PC, it stops working and client cant find server.
(I find my IP using "ipconfig" command on cmd from "Wireless LAN adapter Wi-Fi: IPV4 Adress")
here are my codes for checking the connection:
#SERVER
import socket
s = socket.socket()
IP, port= MY IP, 8002
s.bind((IP,port))
while 1:
s.listen(1)
a,b=s.accept()
print(a,b)
print("client tried to connect")
#Client
import socket
s=socket.socket()
IP,port=SERVERS IP, 8002
s.connect((IP,port))
Can you guys help me please?
Related
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.
I am currently trying to learn how servers and clients work by making a trojan using python and sockets import, my client and server work perfectly on my computer but the moment I send the client to my other laptop the server does not connect. This happens even when i am on the same wifi network.
Server:
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ''
port = 1234
server.bind((host, port))
server.listen(5)
run = True
client, addr = server.accept()
print('Got connection from',addr)
while run:
try:
data = input('>>>')
client.send(data.encode('UTF-8'))
msg = client.recv(1024)
print(msg.decode('UTF-8'))
except ConnectionResetError:
print('Client lost server connection')
print('Trying to connect . . .')
client, addr = server.accept()
print('Got connection from',addr)
Client:
import socket
import os
server = socket.socket()
host = '127.0.0.1'
port = 1234
run = True
server.connect((host,port))
while run:
msg = server.recv(1024)
os.popen(msg.decode('UTF-8'))
server.send('Client online . . .'.encode('UTF-8'))
Your client is connecting to IP 127.0.0.1 (the IPv4 loopback address), which will work only when the server is on the same machine as the client.
When the client and server are on different machines, but still on the same LAN network, the client needs to connect to the server's LAN IP instead. Use netstat or similar tool on the server machine to find its LAN IP. Or, simply have your server code print out its local IPs.
When the client is on another network, it needs to connect to the public WAN IP of the server's LAN router, and that router needs to have port forwarding configured on it to route incoming connections from its WAN IP/Port to the server's LAN IP/port. To get the WAN IP, you will have to look at your router's config, or simply query an external site, like https://api.ipify.org, https://api.my-ip.io/ip, etc from a machine on the LAN, like your server.
Update your client to take in the target host/IP from user input, then it will be able to handle all of these scenarios without having to use different code each time.
[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 am practicing socket programming using python. I am fimiliar with how to make a simple tcp server and client in local address but I want to know how to make it possible so that I can connect to my own computer from a client app that I built. What modifications do I have to make in this server script? or client?
server:
import socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#The host is set to be the local machine.
address = ("127.0.0.1",1234)
s.bind(address)
s.listen(1)
c , addr = s.accept()
while True:
#do some stuff
c.close()
client:
import socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
address = ("127.0.0.1",1234)
s.connect(address)
while True:
#Do client stuff
s.close()
I'm not allowed to make comments yet. But if you have a client app on another device, you can make the HOST your machines IP that stores the server. IF you're using windows ENTER: ipconfig In your command line argument. I think that or Linux its if config. You should be able to set your address to your machines ip address in order to get the client to connect to your local machine. As you noted, localhost will not work.
I'm trying to write two short python scripts that will connect two or more machines to each other, one as the server and the others as clients. It worked perfectly when testing the client and the server script on the same computer, but when I tried it from another computer the client kept timing out; it couldn't connect to the server. Here's my server code:
import socket
server = socket.socket()
host = "computername"
port = 12345
server.bind((host, port))
server.listen(5)
client, addr = server.accept()
Client code:
import socket
server = socket.socket()
host = "computername"
port = 12345
server.connect((host, port))
Any clue as to why the machines can't connect?
I think, you are changing host variable properly when running both client and server scripts in different machines. Try by changing that properly/or using IP address of server machine.
The communication could be prohibited by a firewall.
To rule out DNS related problem, try IPs instead of hostnames:
# server: listen on all interfaces
server.bind(('', port))
and:
# client: specify server's IP address
server.connect(("192.168.XX.YY", port))
with a real IP address, of course.