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?
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.
I'm trying to get two computers (my PC and my laptop) to communicate over the Local Network using the Socket module in python.
This is the Server side code running on my PC (connected via LAN):
import socket
HOST = '192.168.1.3' #local PC IP
print(HOST)
PORT = 8080 # Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
print(data)
if not data:
break
conn.sendall(data)
And this is the Client side code, running on my Laptop (connected over WiFi):
import socket
TCP_IP = '192.168.1.3'
TCP_PORT = 8080
BUFFER_SIZE = 1024
MESSAGE = b"Hello, World!"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(MESSAGE)
data = s.recv(BUFFER_SIZE)
s.close()
print("received data:", data)
The thing is: when I execute both codes, the Server side stays idle waiting for a connection and the Client side, after a while stops and returns the following timeout error:
Traceback (most recent call last):
File "C:\Users\...\client.py", line 13, in <module>
s.connect((TCP_IP, TCP_PORT))
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
I can't understand why it won't connect from another device in the same network while it works perfectly if I execute the Client code on the same machine as the Server, even if when I run netstat -an in the CMD I can see the computer listening on that port:
TCP 192.168.1.3:8080 0.0.0.0:0 LISTENING
I tough it had something to do with the port forwarding so I tried playing around with it but I'm having troubles with that too (the ports seem to remain closed).
I really don't know what to do next, if you have some advice or know something else I could try please reply.
It actually was a firewall problem, I just needed to disable the windows defender firewall for the local network and now everything is working fine
In Windows 10, I had to open the port I was using for the socket, and it worked for me.
Here is a link to the instructions.
You're listening and connecting to the same IP - you need to listen to the client's IP(or just any IP with the correct port number) on the server and connect to the server's IP on the client.
For example, if the client's IP is 1.2.3.4 and the server's is 1.2.3.5, then
# server side
s.bind(('1.2.3.4', 8080)) # CLIENT_IP = '1.2.3.4'; PORT = 8080
# can also be s.bind(('0.0.0.0', 8080)) if you want multiple clients to connect.
# client side
s.connect(('1.2.3.5', 8080)) # SERVER_IP = '1.2.3.5'; PORT = 8080
I'm working on a client server program where the beginning of my client code is:
s = socket.socket()
host = [my ip address]
port = 30000
s.connect((host, port))
and the beginning of my server code is:
s = socket.socket()
host = [my ip address]
port = 30000
s.bind((host, port))
c = None
addr = None
s.listen(5)
while True:
c, addr = s.accept()
print ('New connection from: ', addr)
break
If I run the server program on my computer, and then run the client program on the same computer, the client successfully connects. But if I run the server program on my computer, and then go and run the client code on a different computer, the client will never connect and eventually time out. How do I go about fixing this?
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.
This is the server side program
import socket
s = socket.socket()
host = socket.gethostname()
port = 9077
s.bind((host,port))
s.listen(5)
while True:
c, addr = s.accept()
print("Connection accepted from " + repr(addr[1]))
c.send("Thank you for connecting")
c.close()
This is the client program
import socket
s = socket.socket()
host = socket.gethostname()
port = 9077
s.connect((host, port))
print s.recv(1024)
When i run these two programs on the same computer, it works perfectly.
But when i run the client and server programs in two different computers on the same network, the program doesn't work.
Can anyone please tell me how to send message from one computer to another on the same network.
This is the first time i'm doing any network programming. Any help would be appreciated
Thanks in advance
You are connecting from the client to the client's computer, or well attempting to, because you are using the client's hostname rather than the servers hostname/ip address.
So, to fix this change the line s.connect((host, port)) so that the host points to the servers ip address instead of the client's hostname.
You can find this by looking at your network settings on the server and doing the following:
host = "the ip found from the server's network settings"
host must be edited to the server's ip if the server is not the same computer.