java - 使用 Eneter Messaging 和 Protocol Buffers 在 3g 网络上的端口 80 上启动 tcp 服务器的权限被拒绝

标签 java android sockets tcp protocol-buffers

我正在开发一个项目,我的 android 应用程序应该充当其他 android 客户端的服务器。我正在使用 Eneter messaging framework它使用套接字和谷歌 Protobuf .. 在 wifi 上一切都像一个魅力但是一旦我切换到 3g 网络(尝试使用手机的 3g 公共(public) ip 地址和端口 80/81 启动服务器)我收到以下错误:EACCES(权限被拒绝)

E/EneterMessaging: ~24216 eneter.messaging.messagingsystems.tcpmessagingsystem.internal.TcpListenerProvider.startListening TcpListenerProvider  failed to start listening.
                   Exception:
                   java.net.BindException: bind failed: EACCES (Permission denied)
                   libcore.io.IoBridge.bind(IoBridge.java:99)
                   java.net.PlainSocketImpl.bind(PlainSocketImpl.java:132)
                   java.net.ServerSocket.bind(ServerSocket.java:335)
                   eneter.messaging.messagingsystems.tcpmessagingsystem.NoneSecurityServerFactory.createServerSocket(NoneSecurityServerFactory.java:51)
                   eneter.messaging.messagingsystems.tcpmessagingsystem.internal.TcpListenerProvider.startListening(TcpListenerProvider.java:123)
                   eneter.messaging.messagingsystems.tcpmessagingsystem.TcpInputConnector.startListening(TcpInputConnector.java:134)
                   eneter.messaging.messagingsystems.simplemessagingsystembase.internal.DefaultDuplexInputChannel.startListening(DefaultDuplexInputChannel.java:96)
                   eneter.messaging.infrastructure.attachable.internal.AttachableDuplexInputChannelBase.attachDuplexInputChannel(AttachableDuplexInputChannelBase.java:40)
                   com.inactivity.magamine.teatime.services.TCPService.StartServer(TCPService.java:261)
                   com.inactivity.magamine.teatime.services.TCPService$6.run(TCPService.java:279)
                   java.lang.Thread.run(Thread.java:818) 

我的 list 中有这些权限:

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

这是启动服务器的代码:

public void StartServer() throws Exception
    {
        // Create sender sending MyRequest and as a response receiving MyResponse
        // Instantiate Protocol Buffer based serializer.
        ISerializer aSerializer = new ProtoBufSerializer();

        // Create sender sending MyRequest and as a response receiving MyResponse
        // The sender will use Protocol Buffers to serialize/deserialize messages.
        IDuplexTypedMessagesFactory aRecieverFactory = new DuplexTypedMessagesFactory(aSerializer);
        myReceiver = aRecieverFactory.createDuplexTypedMessageReceiver(MessageDeclarations.MyMsg.class, MessageDeclarations.MyMsg.class);
        myReceiver.messageReceived().subscribe(myOnRequestHandler);

        if(aMessaging == null)aMessaging = new TcpMessagingSystemFactory();

        IDuplexInputChannel anInputChannel
                = aMessaging.createDuplexInputChannel(Service_URL);//Service_URL = "tcp://xxx.xxx.x.xx:80/ with public ip adress


        // Attach the output channel to the sender and be able to send
        // messages and receive responses.
        myReceiver.attachDuplexInputChannel(anInputChannel);

        isConnected = true;
        showNotification("Server Started","url: "+Service_URL,Color.GREEN);
    }

异常发生在

myReceiver.attachDuplexInputChannel(anInputChannel);

这就是 Eneter 启动套接字服务器的地方。

EDIT: From this I found that to bind on ports < 1024 you need root privileges. So I tried ports like 1025, 8080, 8081 , I now get this exception instead: Cannot assign requested address, is it that I cant use 3G network public ip adress to start a socket server or am I missing something ?

E/EneterMessaging: ~25710 eneter.messaging.messagingsystems.simplemessagingsystembase.internal.DefaultDuplexInputChannel.startListening DefaultDuplexInputChannel 'tcp://105.66.131.38:8080/'  failed to start listening.
                   Exception:
                   java.net.BindException: bind failed: EADDRNOTAVAIL (Cannot assign requested address)
                   libcore.io.IoBridge.bind(IoBridge.java:99)
                   java.net.PlainSocketImpl.bind(PlainSocketImpl.java:132)
                   java.net.ServerSocket.bind(ServerSocket.java:335)
                   eneter.messaging.messagingsystems.tcpmessagingsystem.NoneSecurityServerFactory.createServerSocket(NoneSecurityServerFactory.java:51)
                   eneter.messaging.messagingsystems.tcpmessagingsystem.internal.TcpListenerProvider.startListening(TcpListenerProvider.java:123)
                   eneter.messaging.messagingsystems.tcpmessagingsystem.TcpInputConnector.startListening(TcpInputConnector.java:134)
                   eneter.messaging.messagingsystems.simplemessagingsystembase.internal.DefaultDuplexInputChannel.startListening(DefaultDuplexInputChannel.java:96)
                   eneter.messaging.infrastructure.attachable.internal.AttachableDuplexInputChannelBase.attachDuplexInputChannel(AttachableDuplexInputChannelBase.java:40)
                   com.inactivity.magamine.teatime.services.TCPService.StartServer(TCPService.java:261)
                   com.inactivity.magamine.teatime.services.TCPService$6.run(TCPService.java:279)
                   java.lang.Thread.run(Thread.java:818)

我会感谢你的帮助。

亲切

最佳答案

原始问题的答案

端口分配失败,因为如您所述,80 是特权端口。

更新问题的答案

IP 地址分配失败,因为事实证明,您的代码中有错误的 IP 地址。一旦您关闭 WiFi 网络(大概)以进入移动网络,您使用的地址就会从系统中删除,并且您的无线运营商会为您分配一个不同的地址。使用 0.0.0.0 本质上告诉您的服务器库,“我想在所有接口(interface)上监听”。 (另一种选择是找出您的移动 IP 地址,然后使用它。)但这将我们带到了最后一个问题:

对您当前问题的评论

无法连接的原因可能有很多。我怀疑可能是 NAT ,如 this question 中所述.长话短说,很少有应用程序(如果有的话)使用您选择的架构(在连接到移动网络的用户手机上运行服务器)。由于 NAT,手机几乎总是必须“拨出”才能形成连接(在移动网络上)。而且您的服务器没有拨出;它在听。

只要您不跨越 NAT 边界,这些限制就不适用于您的本地 WiFi 网络。

要解决这个问题,您可能需要了解网络架构,并接受一些网络调试方面的培训。像 Wireshark 这样的捕获工具是一个很大的帮助。

如果您在这方面需要更多帮助,而 SO 上的其他答案都没有帮助您,那么最好开始一个新问题。

关于java - 使用 Eneter Messaging 和 Protocol Buffers 在 3g 网络上的端口 80 上启动 tcp 服务器的权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52823670/

相关文章:

java - 如何从Java(而不是System.properties)读取 "OS variables"

java - 何时调用job.getJobID()?

java - Fragment 中的 findViewById 返回 Null

c - 从socket读取到buffer,再从buffer逐行读取实现http语言

.net - SocketAsyncEventArgs "pooled byte[]"样式是否有助于减少内存需求?

java - 在javacv中使用cvSetMouseCallback

java - 不断尝试插入现有键值是一种不好的做法吗

android - 如何查看磨损应用程序的 CRASHES 和 ANRS

android - RecyclerView - 如何平滑滚动到某个位置的项目顶部?

linux - 从外壳关闭套接字