I am trying to make a simple app in Python with sockets, but clients only receive the message "Test" sent from the server if they're in the LAN. I tried to run the client (the server is running on my PC) from my laptop and from my PC. In both cases I received the message "Test", but when a friend tries to connect he doesn't receive the message.
Here is my server.py:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(), 7908))
s.listen(5)
while True:
clientsocket, address = s.accept()
print(f"Connection from {address} established")
clientsocket.send(bytes("Test", "utf-8"))
And here is my client.py:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("my_public_ip_address", 7908))
print(s.recv(8).decode("utf-8"))
I compile client.py with pyinstaller before sending it, so that the script can run without Python being installed on the machine (I don't even have Python on my laptop)
Thanks for taking the time to read and awnsering this :) (Sorry if my english is bad, I'm french)
I guess your friend is outside your LAN: if so you should open/portforward port 7908 on the router to the server.
Open port 7908 on the sever PC firewall.
Your script in this way should work.
Related
I made a program using Python that should connect to the server using a socket. I run the script on the same machine and when I tried with private ip it worked but public doesn't.
Client:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("46.126.xx.xxx",9454)) #I put the public ip of the server (same machine)
msg = s.recv(1024)
print(msg.decode("utf-8"))
Server
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("0.0.0.0",9454))
s.listen(5)
while True:
clientsocket, address = s.accept()
print(f"Connection from {address} has been established!")
clientsocket.send(bytes("Welocme to the server", "utf-8"))
It does not give me any errors. I made lots of reasearch but couldn't figure out what the problem is.
Well I just put the public ip of the server in the client socket.connect() and expected it to connect but it didn't. I am running this on the same machine.
recently I've been messing around with sockets in python and i needed to connect to a remote server for a project. I know there are plenty of questions about this topic but none of the solutions worked for me and i am about to go mad if i can't get this to work.
Server code:
import socket
import threading
FORMAT = "UTF-8"
PORT = 55555
SERVER = ''
ADDR = ('0.0.0.0', PORT)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
def handle_client(conn, addr):
print(f"[NEW CONNECTION] {addr} connected.")
conn.send("Test".encode(FORMAT))
def start():
server.listen()
print(f"[LISTENING] Server is listening on {PORT}")
while True:
connection, adress = server.accept()
thread = threading.Thread(target=handle_client, args=(connection, adress))
thread.start()
print(f"[ACTIVE CONNECTIONS] {threading.activeCount() - 1}")
print("[STARTING] server is starting...")
start()
Client Code:
import socket
import threading
FORMAT = "UTF-8"
PORT = 55555
SERVER = "xx.xxx.xxx.xxx" # public ip
print(f"\nconnecting... {PORT}\n")
ADDR = (SERVER, PORT)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
client.connect(ADDR)
except:
print("Couldnt connect.")
print(client.recv(1024).decode(FORMAT))
When i change the SERVER variable in client script to my local ip (192.168.1.34), i can run these two scripts in two different pcs in the same LAN and it works well, i recieve the "Test" message in my client pc.
However, when i change the SERVER variable to my public ip and run the server in my server pc, i can't connect to the client pc. Here, my server and client pcs are NOT in the same network. Server is connected to my router whereas client is in another network. When i run the client script nothing happens and after a while i get [WINERROR 10057]
I've done port forwarding to port 55555. I tried disabling all firewalls and even creating a new rule in windows firewall to allow connections from port 55555. It still doesn't work and i can't figure out why.
If there is anyone who can see the problem here i would really appreciate it.
The only thing I can see that maybe is causing a problem is in your ADDR variable. I recently did a similar project that was successful, and in my sever code I did the equivalent of:
ADDR('',PORT)
I don't know for sure this would fix your problem, but it is my best guess from the info you provided.
I want to create a server-client chat program using python sockets. I was trying to connect server(me) and client(my friend) through the internet, but still I can't understand the way to do it. Please help me.
Server:
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((my host, 12345))
s.listen(1)
conn, addr=s.accept()
while 1:
msg=input(">>")
conn.send(msg.encode())
print("Client:"+conn.recv(1024).decode())
Client:
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((my host, 12345))
while 1:
print("Server:"+s.recv(1024).decode())
msg=input(">>")
s.send(msg.encode())
I recommend using ngrok, it acts as a port forwarder without having to do it yourself. Download ngrok to your system32 folder and in your command prompt enter the following:
ngrok tcp %PORT%
This will create a TCP socket on localhost, ('0.0.0.0') so now you will have to do the following to your program:
SERVER:
s.bind(('0.0.0.0', %PORT%)) # The port you used for ngrok`
CLIENT:
s.connect(('NGROKHOSTIP', %NGROK FORWARDED PORT%))
The NGROKHOSTIP can be found with a domain to IP program. You can do this yourself with Python.
Also, sorry I couldn't explain this better, I'm new to stackoverflow.
I am new to python coding and tried to create a simple python socket server.
I coded a client.py and a server.py on my laptop and it seems to work (It does what it is supposed to do....), but if i try to run the server on my laptop and the client on my other computer, it sometimes times out.
client.py
import socket
s = socket.socket()
host = '192.168.178.87'
port = 12345
s.connect((host, port))
print s.recv(1024)
s.sendall("greetings")
server.py
import socket
s = socket.socket()
host = ''
port = 12345
s.bind((host, port))
s.listen(5)
i = 0
while i < 5:
c, addr = s.accept()
print 'Got connection from', addr
c.send('Thank you for connecting')
data = c.recv(1024)
print data
c.close()
i += 1
print i
s.close()
I am using a FritzBox 7390, Both computers are in the same local network, both firewalls are turned off and no antivirus is installed.
I am using windows 7 and python 2.7 on both computers.
My problem summed up:
If I run the server on my laptop(192.168.178.87) and open the client.py via cmd on the same computer , it works (I tried to use 127.0.0.1, 192.168.178.87 and my official ipv4 address(portforwarding in the router)) and everything works.
But if I try to use the client.py on my other computer (192.168.178.131) it only works if I am the fifth to try to connect and I have no idea why.
I can connect to the server via browser, and it sometimes works, but mostly there is an timeout error (Errno 10060).
What is the problem with my code?
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.