简体   繁体   English

获取主机的IP地址

[英]Getting IP address of host

I created a little game and now I am trying to make a multiplayer game out of it.我创建了一个小游戏,现在我正在尝试用它制作多人游戏。 One android phone should host the game and the other phone should be able to connect to this server.一部安卓手机应该托管游戏,另一部手机应该能够连接到这个服务器。 My question is how can I tell the Client to which IP he should connect.我的问题是如何告诉客户端他应该连接哪个 IP。 Is there a way to scan for hosts?有没有办法扫描主机?

For Android games there is a perfect (and simple) library called kryonet .对于 Android 游戏,有一个完美(且简单)的库,称为kryonet

It has a method - discoverHost - for scanning local network for devices that are listening for incoming connections.它有一个方法-discoverHost -用于扫描本地网络以查找正在侦听传入连接的设备。

Also, it contains classes such as:此外,它还包含以下类:

that makes implementing simple communication protocol ( on both UDP and TCP protocols ) really easy.这使得实现简单的通信协议(在 UDP 和 TCP 协议上)非常容易。

As many people uses this library (including me) - you can find a lot of examples :很多人都在使用这个库(包括我)——你可以找到很多例子

... and many others. ......以及许多其他人。 :) :)

Update 1:更新 1:

There is also a great library from Google called Nearby Connections API that allows to autodiscover devices in local network and allows to create multiplayer (or multiscreen) games.还有一个来自 Google 的名为Nearby Connections API的很棒的库,它允许自动发现本地网络中的设备并允许创建多人(或多屏)游戏。

Furthermore, Google has created nice (multiplatform) APIs for multiplayer games over the WAN :此外,谷歌已经为 WAN 上的多人游戏创建了很好的(多平台) API

Sample for Nearby Connections and Real-time Multiplayer APIs you can find here .您可以在此处找到附近连接和实时多人 API 的示例。

If I understand you correctly, you want the game host to advertise itself in the reachable network to clients to join.如果我理解正确,您希望游戏主机在可访问的网络中宣传自己以供客户加入。

There are many possibilities around this on different levels of the network protocol stack.在网络协议栈的不同级别上,围绕这一点有很多可能性。

Somethings to look at, that come to my mind:一些要看的东西,我想到了:

You see, there are quite some possibilities.你看,有相当多的可能性。 Maybe you want to narrow down you question to your specific needs:也许您想将问题缩小到您的特定需求:

  • Who is the audience, is it a "LAN-party", other phones somewhere in the mobile space?谁是观众,是“局域网派对”,还是移动空间中的其他手机?
  • How much effort do you want to put into it?你想为此付出多少努力?
  • What platforms do you need to support?需要支持哪些平台?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM