so ive just started leaning about socketing when I came across an odd problem. I can make a connection between my Window computer and my Raspberry pi when the Pi is running my server script and my Windows running my client script, but when I try to run the server script on my Windows and client on my Pi they do not connect (after switching the host of course). Anyone have any idea why this is happening?
#server code
import socket
import time
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
host = '192.168.0.23'
port = 2613
serversocket.bind((host, port))
serversocket.listen(5)
while True:
clientsocket, addr = serversocket.accept()
print("Got a connection from %s" % str(addr))
currentTime = time.ctime(time.time()) + "\r\n"
clientsocket.send(currentTime.encode('ascii'))
clientsocket.close()
and
#client code
import socket
clientsocket = socket.socket()
host = '192.168.0.23'
port = 2613
clientserver.connect((host,port))
print s.recv(1024)
clientserver.close
In your client code you have a clientsocket which you then call clientserver then call s. Both clientserver and s are undefined. Try:
#client code
import socket
clientsocket = socket.socket()
host = '192.168.0.23'
port = 2613
clientsocket.connect((host,port))
print (clientsocket.recv(1024))
clientsocket.close
Also try running the server script on Windows with Administrator privileges so it can open the port and check your firewall settings on Windows for port 2613.
Related
I have a simple server-client combo running on 2 computers in 2 different networks. The server (a Raspberry) has a TCP tunnel running.
Now running on the code on localhost is fine, on 2 different machines with correct IP and port (I can ping the server via telnet and establish connection via ssh using the same IP and port), I just get the following on the client side AND NOTHING ELSE:
Received SSH-2.0-OpenSSH_7.9p1 Raspbian-10+deb10u2
I should instead receive: Received b'Hello, world'
On the serverside I receive no message at all.
What do I need to run it in 2 different networks? Below the source codes.
server.py:
#!/usr/bin/env python3
import socket
HOST = '' # Standard loopback interface address (localhost)
PORT = 65432 # 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)
if not data:
break
conn.sendall(data)
client.py:
#!/usr/bin/env python3
import socket
HOST = 'xxx.xxx.xxx.xxx' # The server's hostname or GLOBAL IP address
PORT = 12345 # The port used by the server, not actually 12345
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b"Hello, world")
data = s.recv(1024)
print("Received", repr(data))
I was programming a simple client-server socket program that worked on two different computers.
The server is a desktop with a static ip address, and the client is a laptop connected to a Wi-Fi. Both are using Windows 10 as operating system.
I also opened the firewall port.
Here is my code.
This code works well within one computer, but WinError 10057 occurs when another computer(my laptop) tries to connect to the server.
server.py
from socket import *
import sys
HOST = '0.0.0.0'
PORT = 16161
BUFSIZE = 1024
ADDR = (HOST, PORT)
CLIENT_NUM = 5
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(ADDR)
print('bind')
serverSocket.listen(CLIENT_NUM)
print('listen')
while True:
try:
connectionSocket, addr_info = serverSocket.accept()
print('accept')
print('--client information--')
print(connectionSocket)
data = connectionSocket.recv(BUFSIZE)
print('Received data:', data.decode())
connectionSocket.send('OK'.encode())
connectionSocket.close()
except KeyboardInterrupt:
sys.exit(0)
client.py
from socket import *
import sys
HOST = '*.*.*.*' # server's ip address
PORT = 16161
BUFSIZE = 1024
ADDR = (HOST, PORT)
clientSocket = socket(AF_INET, SOCK_STREAM)
try:
clientSocket.connect_ex(ADDR)
clientSocket.send('Hello!'.encode()) # WinError 10057 occurs
except Exception as e:
print(e)
print('%s:%s' % ADDR)
sys.exit(1)
print('connect is success')
receive = clientSocket.recv(BUFSIZE)
print(receive.decode())
clientSocket.close()
I've fixed it. I asked my organization to open the firewall ports, and the connection was successful when the firewall ports were opened.
I recently started learning socket programming with python. Starting with the most basic scripts of server and client on the same computer, I wrote the following code.
Server.py
import socket
import time
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 9999
serversocket.bind((host,port))
serversocket.listen(5)
while True:
clientsocket, addr = serversocket.accept()
print("Got a connection from %s" %str(addr))
currentTime = time.ctime(time.time()) + "\r\n"
clientsocket.send(currentTime.encode('ascii'))
clientsocket.close()
Client.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 9999
s.connect((host,port))
tm = s.recv(1024)
s.close()
print("The time got from the server is %s" %tm.decode('ascii'))
I'm using spyder IDE. Whenever I run the client in the IPython Console, this is what I get:
"ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it."
and whenever I run the Server I get an unending process.
So, what should I do to make this work?
Thank you for any help!
Credits :- http://www.bogotobogo.com/python/python_network_programming_server_client.php
Try changing socket.gethostname() to socket.gethostbyname(socket.gethostname()). gethostbyname returns the ip for the hostname. You want to setup a socket to connect to an ip, port. Alternatively, since you are running everything locally, just set your host to "127.0.0.1" directly for both the client/server.
So i would like to create a small online game. (I am a complete noob with networking)
I got a server-client connection going if they are both on the same network but would need to do it over the internet. How do i do this?
server.py
import socket
s = socket.socket()
host = "0.0.0.0"
port = 5000
s.bind((host,port))
s.listen(5)
while True:
c, addr = s.accept()
print("Connection from", addr)
c.close()
client.py
import socket
s = socket.socket()
host = "192.168.1.66"
port = 5000
s.connect((host, port))
s.send(bytearray("Message", "ascii"))
I have tried using my public IP but i cannot connect.
I've tried to connect two computers with a socket in Python and I don't know why it doesn't work. The files are from internet and it compiles for me but without any results.
The server.py:
#!/usr/bin/python
import socket
s = socket.socket()
host = ''
port = 12345
s.bind((host, port))
s.listen(5)
while True:
c, addr = s.accept()
print 'Got connection from', addr
c.send('Thank you for connecting')
c.close()
and the client.py:
#!/usr/bin/python
import socket
s = socket.socket()
host = # here I put the ip of the server's laptop
port = 12345
s.connect((host, port))
print s.recv(1024)
s.close()
What's wrong?
You have to run the server first. Then run the client at the same time with the IP of the server (I used localhost because it was running on one computer, maybe you should try if that works). The code worked fine for me, every time I ran the client, the server printed a message. If it doesn't work for you, maybe your firewall is not letting you open ports.
Just for the future, please always post any error messages you see.
BTW, isn't this the Python Documentation example for sockets?