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. :)