2016-05-01 16 views
1

私はpython(pygameとpodsixnet)を使ってマルチプレイヤーゲームを開発しています。クライアントがサーバーに接続する必要があるたびに、サーバーのホスト名とポートを入力する必要があります。クライアントにこの情報を自動的に提供することでこの段階を回避したいと考えています。私のサーバーのアドレスはどのようにして見つけることができますか? https://www.raywenderlich.com/38732/multiplayer-game-programming-for-teens-with-pythonサーバのIPアドレスはどのように知ることができますか?

def __init__(self): 
    self.justplaced=10 
    n = self.dialog() 
    print n 
    pygame.init() 
    self.clock = pygame.time.Clock() 
    self.screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT)) 
    pygame.display.set_caption('Scrabble') 
    self.gameid=None 
    self.num=None 

    self.lastx, self.lasty = 0, 0 

    self.mousex = 0 # used to store x coordinate of mouse event 
    self.mousey = 0 # used to store y coordinate of mouse event 
    #print BGCOLOR 

    self.screen.fill(BGCOLOR) 
    #pygame.display.update() 

    self.is_Placed = self.generateBoxData(False) 
    self.placed_Letter = self.generateBoxData("") 
    self.is_Formed = self.generateBoxData(False) 
    self.owner = self.generateBoxData(99) 
    self.is_Clicked = self.generateBoxData(False) 

    address=raw_input("Address of Server: ") 

    try: 
     if not address: 
      host, port="localhost", 8000 
     else: 
      host,port=address.split(":") 
     self.Connect(("localhost" ,int(port))) 
    except: 
     print "Error Connecting to Server" 
     print "Usage:", "host:port" 
     print "e.g.", "localhost:31425" 
     exit() 
    print "Boxes client started" 
+0

[[Pythonのstdlibを使用してローカルIPアドレスを検索する]](http://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib) – tknickman

答えて

0

私もmaltiplayerゲームを作るように:ゲームはチュートリアルに従うことによって開発されました! :)

これは、あなたがsocket.gethostbyname(ホスト名)

を探している可能性がありどのような完全な小さなスクリプトは次のようになり、主に次のとおりです。

import socket 
hostname = 'maps.google.com' 
addr = socket.gethostbyname(hostname) 
print 'The address of ', hostname, 'is', addr 

は、この情報がお役に立てば幸い!

関連する問題