首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >两个设备之间的局域网不工作

两个设备之间的局域网不工作
EN

Stack Overflow用户
提问于 2015-01-22 23:18:17
回答 1查看 252关注 0票数 0

我编写了一个简单的AS3程序来创建不同设备之间的无服务器P2P局域网连接。下面是它的要点:

客户端:

代码语言:javascript
复制
private function initLan():void 
    {
        //G is a class with two static variables - netConnection and netGroup
        G.netConnection = new NetConnection();
        G.netConnection.addEventListener(NetStatusEvent.NET_STATUS, setupLAN);
        G.netConnection.connect("rtmfp:");
    }

    private function setupLAN(e:NetStatusEvent):void
    {
        trace(e.info.code);
        switch (e.info.code)
        {
            case "NetGroup.Connect.Failed": 
            case "NetGroup.Connect.Rejected": 
            case "NetConnection.Connect.Rejected": 
            case "NetConnection.Connect.Failed": 
                trace("There was a problem :/\n+" + e.info.code);
                break;

            case "NetConnection.Connect.Success": 
                trace("Setting up LAN group..");
                setupGroup();
                break;

            case "NetGroup.Connect.Success": 
                trace("LAN group successful!");

                //Start bind requests
                beginBindRequests();

                break;

            case "NetGroup.SendTo.Notify":
                var msg:Object = e.info.message;

                if (msg.type == "BIND RESPONSE") {
                    endBindRequests(); //See beginBindRequests();
                    G.netConnection.removeEventListener(NetStatusEvent.NET_STATUS, setupLAN);
                    G.netGroup.removeEventListener(NetStatusEvent.NET_STATUS, setupLAN);

                    //Connection established.
                }

                break;
        }
    }

    private function setupGroup():void
    {
        var groupspec:GroupSpecifier = new GroupSpecifier("LAN CONNECTION");
        groupspec.postingEnabled = true;
        groupspec.routingEnabled = true;
        groupspec.ipMulticastMemberUpdatesEnabled = true;
        groupspec.addIPMulticastAddress("225.225.0.1:30303");

        G.netGroup = new NetGroup(G.netConnection, groupspec.groupspecWithAuthorizations());
        G.netGroup.addEventListener(NetStatusEvent.NET_STATUS,setupLAN);
    }

    private function beginBindRequests():void 
    {
        function requestBind():void {
            G.netGroup.sendToAllNeighbors( { type:"BIND REQUEST" } );
        }

        //Code running requestBind() until endBindRequests() is called
    }

服务器:

代码语言:javascript
复制
private function initLAN():void
    {
        //G is a class with two static variables - netConnection and netGroup
        G.netConnection = new NetConnection();
        G.netConnection.addEventListener(NetStatusEvent.NET_STATUS, setupLAN);
        G.netConnection.connect("rtmfp:");
    }

    private function setupLAN(e:NetStatusEvent):void
    {
        switch (e.info.code)
        {
            case "NetGroup.Connect.Failed": 
            case "NetGroup.Connect.Rejected": 
            case "NetConnection.Connect.Rejected": 
            case "NetConnection.Connect.Failed": 
                trace("There was a problem :/\n+" + e.info.code);
                break;

            case "NetConnection.Connect.Success": 
                trace("Setting up LAN group..");
                setupGroup();
                break;

            case "NetGroup.Connect.Success": 
                trace("LAN group successful!");

                //Add hot controller function
                G.netConnection.addEventListener(NetStatusEvent.NET_STATUS, bindResponses);
                G.netGroup.addEventListener(NetStatusEvent.NET_STATUS, bindResponses);

                G.netGroup.removeEventListener(NetStatusEvent.NET_STATUS, setupLAN);
                G.netConnection.removeEventListener(NetStatusEvent.NET_STATUS, setupLAN);
                break;
        }
    }

    private function setupGroup():void
    {
        var groupspec:GroupSpecifier = new GroupSpecifier("LAN CONNECTION");
        groupspec.postingEnabled = true;
        groupspec.routingEnabled = true;
        groupspec.ipMulticastMemberUpdatesEnabled = true;
        groupspec.addIPMulticastAddress("225.225.0.1:30303");

        G.netGroup = new NetGroup(G.netConnection,groupspec.groupspecWithAuthorizations());
        G.netGroup.addEventListener(NetStatusEvent.NET_STATUS, setupLAN);
    }

    private function bindResponses(e:NetStatusEvent):void
    {
        detailText.text = e.info.code;
        switch (e.info.code)
        {
            case "NetGroup.SendTo.Notify":
                if (e.info.message.type == "BIND REQUEST")
                {
                    //Send bind response
                    G.netGroup.sendToNearest({type: "BIND RESPONSE"}, e.info.from);
                }
                break;
        }
    }

当在同一台PC上运行两个已编译的主权财富基金的实例时,这种方法工作得很好,但当客户端在运行时(例如,一部装有AIR的手机--是的,设置了<uses-permission android:name="android.permission.INTERNET" /> )和服务器在PC上运行(同时作为AIR应用程序)时,它就不能工作了。

Tracing e.info.code at setupLan()已经表明,当在不同的设备中,这两个实例甚至没有“看到”对方(NetGroup.Neighbor.Connect从未被触发)。编辑:我已经测试过,两台运行客户机/服务器AIR应用程序的PC机也看不到对方。

最糟糕的是,相同的代码(尽管在不同的FlashDevelop项目中)曾经工作过,显示了它的工作能力。不知何故。我真的很困惑和沮丧。帮助。请。

PS。如果可能的话(我开始怀疑不是这样),一个不涉及对路由器/防火墙设置进行干预的解决方案将是非常感谢的,因为大多数终端用户都不会去使用应用程序。另外,我测试了两个不同的路由器,问题依然存在。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-25 16:41:38

问题在于计算机本身,而不是编码或部署。局域网正在正确地创建,只是在错误的网络上。按照this link中的步骤解决了问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28100463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档