So far I have been able to work out a basic socket in python 3.2. The client sends some data, an X and a Y coordinate, to the server, and the server takes the data and sends back a confirmation message. But the trouble I'm having is getting it to listen between computers. My server and client work perfect when I run them side-by-side on the same computer, but I want to get them to connect while running on different computers.
I have one computer upstairs, and one computer downstairs, both using the same wireless internet. Is there a way I can connect my server and client from one of each of these computers?
I have already tried changing the server IP to the IP address of my wireless modem, but that did not work.
Here is my code so far, the only difference is I changed the IP address back to a standard loop-back address, since just changing it to my IP did not work:
Client:
import pygame, sys
from socket import socket, AF_INET, SOCK_DGRAM
from time import gmtime, strftime
from pygame.locals import *
SERVER_IP = '127.0.0.1'
PORT_NUMBER = 5000
SCREEN_X = 400
SCREEN_Y = 400
SIZE = 1024
PIC_PATH = "picture/path/goes/here.bmp"
print ("Test client sending packets to IP {0}, via port {1}\n".format(SERVER_IP, PORT_NUMBER))
mySocket = socket( AF_INET, SOCK_DGRAM )
x = y = 0
screen = pygame.display.set_mode((SCREEN_X, SCREEN_Y)) #Make the screen
ending = False
word = "True"
clock = pygame.time.Clock() #tick-tock
grid = pygame.image.load(PIC_PATH) #Load the sheet
gridRect = grid.get_rect()
screen.blit(grid, gridRect)
pygame.display.flip()
while ending==False:
for event in pygame.event.get():
if event.type == KEYDOWN: # key down or up?
if event.key == K_RIGHT: x+=1
elif event.key == K_LEFT: x-=1
elif event.key == K_UP: y-=1
elif event.key == K_DOWN: y+=1
if event.key == K_ESCAPE:
ending=True # Time to leave
print("Stopped Early by user")
if ending==True: word="False"
localTime = strftime( "%H:%M:%S", gmtime() )
mySocket.sendto( bytes(str(x), 'UTF-8') , (SERVER_IP, PORT_NUMBER) )
mySocket.sendto( bytes(str(y), 'UTF-8') , (SERVER_IP, PORT_NUMBER) )
mySocket.sendto( bytes(word, 'UTF-8') , (SERVER_IP, PORT_NUMBER) )
print ("Sending packet... " + localTime)
clock.tick(10)
try:
(data, addr) = mySocket.recvfrom( SIZE )
print ("Received packet from: " + str(addr))
print ("Received: " + data.decode('UTF-8'))
except: ending=False
if ending==True:
pygame.quit()
sys.exit()
Server:
from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM
import sys
PORT_NUMBER = 5000
SIZE = 1024
hostName = gethostbyname( 'localhost' )
mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.bind( (hostName, PORT_NUMBER) )
print ("Test server listening on port {0}\n".format(PORT_NUMBER))
flag="True"
while flag=="True":
#Show that data was received:
(data, addr) = mySocket.recvfrom( SIZE )
xer=data.decode('UTF-8')
print ("Received packet from: " + str(addr) + ", X value:" + str(xer))
(data, addr) = mySocket.recvfrom( SIZE )
yer=data.decode('UTF-8')
print ("Received packet from: " + str(addr) + ", Y value:" + str(yer))
#Check to see if the other program wants to close:
(flagger, addr) = mySocket.recvfrom( SIZE )
flag=flagger.decode('UTF-8')
#Send a message back to the client:
if flag=="False": s="Good-bye!"
else: s="Read you loud and clear"
mySocket.sendto( bytes(s, 'UTF-8') , (addr) )
sys.exit()
Just in case you're wondering why the client is so big, it's because I'm trying to make a little game in pygame that will be multi-player. Not internet but LAN or wireless between my two computers. I am very new to sockets and web related stuff (I don't know if LAN is basically the same as wireless or what) so any help on this is greatly appreciated. :)
Don't bind to localhost. This means that your server will only listen to itself. If you bind to 0.0.0.0
, this will ensure that your server is listening to every computer that can reach it. (Warning: potentially insecure.)
An overzealous firewall rule could also be to blame.
Make sure you are connecting to your server's IP in your router's subnet. To do so, run ifconfig
on Mac/Linux or ipconfig
on Windows, which will probably show you a 192.168.x.x
-style IP (which will not be the same as your router's IP). You can also see what computers and IPs are connected to your router via its administrative page.
At any given point in time, a computer that is connected to the Internet has many IPs. The loop-back address is only 'visible' to your computer. An IP like 10.x.x.x
or 192.168.x.x
would be visible to any computers connected to your router. Most other IPs are public IPs.
You should be using the IP addresses of the computers on the local network. The local IP address should be something like:
| Device | IP |
|:----------:|:-----------:|
| router | 192.168.1.1 |
| computer_1 | 192.168.1.2 |
| computer_2 | 192.168.1.3 |
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With