无法将Android连接到桌面服务器

时间:2016-06-04 23:40:40

标签: android libgdx kryonet

我正在使用libGdx和Kryo​​net开发一款游戏,我正在使用服务器< - > android连接。

首先我在计算机上运行服务器,如果我在同一台计算机上运行该程序的两个实例(因此IP将是本地IP:“127.0.0.1”),我没有遇到任何问题,并且连接正常。

但是如果不是在桌面上运行游戏,我在Android设备上运行它,连接不会发生。这两个设备都连接到同一个Wi-Fi网络,所以要发现IP,我要做的是在控制台上键入“ipconfig”,并检查“Wireles LAN适配器Wi-Fi”下的“IPv4 Adress”行“(我认为就是这个)。

我在android清单文件中添加了这3行(他们真的需要吗?):

//create a SKView, which takes up the same frame as our view
let spriteView = SKView(frame: self.view.bounds)

//adding spriteView as a child view of our view
self.view.addSubview(spriteView)

//Here, we create a scene, which is the root object of
//the graph
let scene = SKScene(size: spriteView.frame.size)
scene.backgroundColor = .orangeColor()

//As said, we present the scene with our SKView
spriteView.presentScene(scene)

//Here, we create a node, which will be added by our scene
//This node takes up a size that you originally created and has the 
//background color set to blue
let nodeFrame = CGRect(x: 0, y: 0, width: 200, height: 100)
let node = SKSpriteNode(color: .blueColor(), size: nodeFrame.size)

//The following just serves to show how we can adjust the node's   
//position; I will leave that to you
node.position = CGPointMake(scene.frame.size.width - 200, scene.frame.size.height - 100)

//Finally, we add the node to our scene
scene.addChild(node)

这是重要的错误日志:

enter image description here 我是否需要在设备上做一些额外的代码才能建立android连接?或许更多权限?

以下是相关代码: MPServer:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

MPClient:

public class MPServer {
Server server;
Array<PlayerInfo> playersInfo;
int numNewPlayers;

public MPServer() throws IOException {
    numNewPlayers = 0;
    playersInfo = new Array<PlayerInfo>();
    server = new Server();

    Network.registerPackets(server);
    addListeners();
    server.bind(Network.PORT);
    server.start();
}

private void addListeners() {
    server.addListener(new Listener() {
        @Override
        public void connected(Connection connection) {

        }

        @Override
        public void disconnected(Connection connection) {

        }

        @Override
        public void received(Connection c, Object object) {

        }
    });
}

public static void main(String[] args) {
    try {
        new MPServer();
        Log.set(Log.LEVEL_DEBUG);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

1 个答案:

答案 0 :(得分:1)

如果您使用的是Windows操作系统,原因可能是Windows防火墙,如果您想将移动设备连接到本地服务器,请尝试禁用它。