Openshift java.net.SocketException:权限被拒绝

时间:2016-09-01 08:19:56

标签: java sockets openshift openshift-cartridge

我正在使用Java8并且聊天服务器在我的 localhost 上完美运行,但是当我将其部署到OpenShift服务器时,我收到以下错误:< / p>

  

java.net.SocketException:Permission denied

09-01 16:11:32.085 3255-3255/com.example.matamatamata.mdpbtlayout D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
09-01 16:11:32.225 3255-3255/com.example.matamatamata.mdpbtlayout D/BluetoothChatService: connect to: C4:43:8F:98:9A:9A
09-01 16:11:32.225 3255-3255/com.example.matamatamata.mdpbtlayout D/BluetoothChatService: setState() 1 -> 2
09-01 16:11:32.225 3255-15464/com.example.matamatamata.mdpbtlayout I/BluetoothChatService: BEGIN mConnectThread SocketType:Insecure

                                                                                           [ 09-01 16:11:32.225  4143: 4210 E/         ]
                                                                                           ### ASSERT : external/bluetooth/bluedroid/main/../btif/src/btif_dm.c line 2025 unhandled search services event (6) ###
09-01 16:11:32.235 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothUtils: isSocketAllowedBySecurityPolicy start : device null
09-01 16:11:32.235 3255-15464/com.example.matamatamata.mdpbtlayout W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
09-01 16:11:32.235 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothSocket: connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[54]}
09-01 16:11:34.315 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothSocket: close() in, this: android.bluetooth.BluetoothSocket@1dbcf50f, channel: -1, state: INIT
09-01 16:11:34.315 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothSocket: close() this: android.bluetooth.BluetoothSocket@1dbcf50f, channel: -1, mSocketIS: android.net.LocalSocketImpl$SocketInputStream@14b9199c, mSocketOS: android.net.LocalSocketImpl$SocketOutputStream@23459fa5mSocket: android.net.LocalSocket@19e76f7a impl:android.net.LocalSocketImpl@3dddd32b fd:FileDescriptor[54]
09-01 16:11:34.315 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothSocket: Closing mSocket: android.net.LocalSocket@19e76f7a impl:android.net.LocalSocketImpl@3dddd32b fd:FileDescriptor[54]
09-01 16:11:34.315 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothChatService: start
09-01 16:11:34.315 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothSocket: close() in, this: android.bluetooth.BluetoothSocket@1dbcf50f, channel: -1, state: CLOSED
09-01 16:11:34.315 3255-15464/com.example.matamatamata.mdpbtlayout D/BluetoothChatService: setState() 2 -> 1
09-01 16:11:34.325 3255-15464/com.example.matamatamata.mdpbtlayout E/BluetoothChatService: Connection Fail

我查看了OpenShift网络套接字指南here,该指南说明应该使用端口8000。但我仍然得到错误。

在Openshift上我在 WildFly Application Server 10 盒式磁带上运行聊天服务器

任何建议都表示赞赏。

这是我的代码:

WebAppInitializer.java

2016-09-05 10:36:11,300 INFO  [stdout] (Thread-125) Starting Chat server on localhost:8000 ...
2016-09-05 10:36:13,194 ERROR [stderr] (Thread-125) Exception in thread "Thread-125" java.net.SocketException: Permission denied
2016-09-05 10:36:13,194 ERROR [stderr] (Thread-125)     at sun.nio.ch.Net.bind0(Native Method)
2016-09-05 10:36:13,195 ERROR [stderr] (Thread-125)     at sun.nio.ch.Net.bind(Net.java:433)
2016-09-05 10:36:13,195 ERROR [stderr] (Thread-125)     at sun.nio.ch.Net.bind(Net.java:425)
2016-09-05 10:36:13,195 ERROR [stderr] (Thread-125)     at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
2016-09-05 10:36:13,195 ERROR [stderr] (Thread-125)     at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
2016-09-05 10:36:13,196 ERROR [stderr] (Thread-125)     at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:125)
2016-09-05 10:36:13,196 ERROR [stderr] (Thread-125)     at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:476)
2016-09-05 10:36:13,196 ERROR [stderr] (Thread-125)     at io.netty.channel.DefaultChannelPipeline$HeadHandler.bind(DefaultChannelPipeline.java:1000)
2016-09-05 10:36:13,196 ERROR [stderr] (Thread-125)     at io.netty.channel.DefaultChannelHandlerContext.invokeBind(DefaultChannelHandlerContext.java:463)
2016-09-05 10:36:13,197 ERROR [stderr] (Thread-125)     at io.netty.channel.DefaultChannelHandlerContext.bind(DefaultChannelHandlerContext.java:448)
2016-09-05 10:36:13,197 ERROR [stderr] (Thread-125)     at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:842)
2016-09-05 10:36:13,197 ERROR [stderr] (Thread-125)     at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:195)
2016-09-05 10:36:13,197 ERROR [stderr] (Thread-125)     at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:338)
2016-09-05 10:36:13,198 ERROR [stderr] (Thread-125)     at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:370)
2016-09-05 10:36:13,198 ERROR [stderr] (Thread-125)     at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:353)
2016-09-05 10:36:13,198 ERROR [stderr] (Thread-125)     at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
2016-09-05 10:36:13,198 ERROR [stderr] (Thread-125)     at java.lang.Thread.run(Thread.java:745)

Server.java

    try {
        new Thread() {
            public void run() {
                com.jobs.spring.chat.Server chatServer = new com.jobs.spring.chat.Server();
                chatServer.startServer();
            }
        }.start();
    } catch (Exception e) {
        e.printStackTrace();
    }

this link OpenShift谈论端口绑定和代理。我并不是真的了解所有这些。看起来我应该使用端口8000(我是),但我不清楚我应该使用什么主机名。我正在使用我的应用程序URL名称(jbosswildfly-easyjobs.rhcloud.com)。这是对的吗?

如果我将地址更改为import java.net.Socket; import com.corundumstudio.socketio.AckRequest; import com.corundumstudio.socketio.Configuration; import com.corundumstudio.socketio.SocketIOClient; import com.corundumstudio.socketio.SocketIOServer; import com.corundumstudio.socketio.listener.ConnectListener; import com.corundumstudio.socketio.listener.DataListener; import com.corundumstudio.socketio.listener.DisconnectListener; import com.fasterxml.jackson.databind.ObjectMapper; /** * https://blog.openshift.com/paas-websockets/ * @author Richard * */ public class Server { //private static final String SERVER = "localhost"; private static final String SERVER = "jbosswildfly-easyjobs.rhcloud.com"; private static final Integer PORT = 8000; public static void main(String[] args) { startServer(); } public static void startServer() { Configuration config = new Configuration(); config.setHostname(SERVER); config.setPort(PORT); final SocketIOServer server = new SocketIOServer(config); server.addConnectListener(new ConnectListener() { @Override public void onConnect(SocketIOClient client) { System.out.println("onConnected"); client.sendEvent("chat_message:message", new Message("Welcome to the chat!")); } }); server.addDisconnectListener(new DisconnectListener() { @Override public void onDisconnect(SocketIOClient client) { System.out.println("onDisconnected"); } }); server.addEventListener("chat_message:send", String.class, new DataListener<String>() { @Override public void onData(SocketIOClient client, String data, AckRequest ackSender) throws Exception { Message message = null; try { message = new ObjectMapper().readValue(data.toString(), Message.class); } catch (Exception e) { e.printStackTrace(); } message.setDate(System.currentTimeMillis()); server.getBroadcastOperations().sendEvent("chat_message:message", message); } }); System.out.println("Starting Chat server on " + SERVER + ":" + PORT+" ..."); server.start(); System.out.println("Chat server started"); System.out.println("Chat server Environment Info: " + System.getenv()); try { Socket socket = new Socket(SERVER, PORT); printSocketInformation(socket); } catch (Exception e) { e.printStackTrace(); } } /** * Prints debug output (to stdout) for the given Java Socket. */ public static void printSocketInformation(Socket socket) { try { System.out.format("Port: %s\n", socket.getPort()); System.out.format("Canonical Host Name: %s\n", socket.getInetAddress().getCanonicalHostName()); System.out.format("Host Address: %s\n\n", socket.getInetAddress().getHostAddress()); System.out.format("Local Address: %s\n", socket.getLocalAddress()); System.out.format("Local Port: %s\n", socket.getLocalPort()); System.out.format("Local Socket Address: %s\n\n", socket.getLocalSocketAddress()); System.out.format("Receive Buffer Size: %s\n", socket.getReceiveBufferSize()); System.out.format("Send Buffer Size: %s\n\n", socket.getSendBufferSize()); System.out.format("Keep-Alive: %s\n", socket.getKeepAlive()); System.out.format("SO Timeout: %s\n", socket.getSoTimeout()); } catch (Exception e) { e.printStackTrace(); } } } (即前缀http://jbosswildfly-easyjobs.rhcloud.com),则会收到以下错误:

  

java.net.SocketException:未解析的地址

enter image description here

2 个答案:

答案 0 :(得分:2)

链接的博客帖子说你可以使用端口8000和8443连接到OpenShift上的websockets。但是,服务器本身只需要绑定到端口 8080 ,然后你可以从外部连接到上面的ws端口。您已找到的This diagram解释了配置。

WildFly已经使用了8080,因此您可能希望使用DIY cartridge来部署您的应用(不要忘记在8080上运行的启动操作挂钩中禁用默认的ruby服务器)或受到启发here

答案 1 :(得分:1)

我试图找出解决方案。我有一些解决方案。所有人都建议将-Djava.net.preferIPv4Stack=true添加到VM选项中。

Atlassian Documentation也得到了根本原因和解决方案:

  

<强>原因

     
      
  1. 根据this post,这是Java 7的已知问题之一。
  2.   
  3. 这也可能是由任何防病毒软件或防火墙软件引起的   安装在服务器上。
  4.         

    解决

         
        
    1. 使用-Djava.net.preferIPv4Stack=true JVM系统属性提供帮助   在Java 7上启用对IPv4的支持。
    2.   
    3. 检查服务器上的防病毒软件和防火墙软件是否正常   阻止Stash连接邮件服务器的能力。
    4.   

此解决方案适用于Java 7,但您使用的是Java 8. 所以我查找了堆栈跟踪。有一行

 at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:125)

这说明您使用的是 netty

但是netty对版本有一些要求。还有一些限制。

  

JDK 5(Netty 3.x)或6(Netty 4.x)就足够了。一些组件如   HTTP / 2可能有更多要求

Netty 4.x(link

的要求
  

Java目前不支持ALPN或NPN(is a tracking issue,所以请进行投票!)。由于缺乏对JDK的支持,我们需要   使用Jetty-ALPN(如果是Java&lt; 8则使用Jetty-NPN)bootclasspath   OpenJDK的扩展。为此,请添加Xbootclasspath JVM选项   引用Jetty alpn-boot jar的路径。

java -Xbootclasspath/p:/path/to/jetty/alpn/extension.jar ...
     

请注意,您必须使用release of the Jetty-ALPN jar特定的   到你正在使用的Java版本。

     

JDK Ciphers

     

Java 7不支持HTTP2 RFC the cipher suites recommended。为了解决这个问题,我们建议服务器使用Java 8   可能或使用替代JCE实现,例如Bouncy Castle。如果这不实用,可以使用其他   密码,但您需要确保您打算调用的服务   也支持HTTP / 2 RFC禁止的这些密码   评估了这样做的安全风险。

     

启用ALPN或NPN

     

SslContextBuilder有一个ApplicationProtocolConfig的setter,用于配置ALPN或NPN。   有关ALPN,请参阅HTTP/2 examples,有关NPN,请参阅SPDY examples   的使用。

有关详细说明,您可以浏览this link

建议:

请检查您的netty版本并根据上述问题做出决定。或者尽可能使用JDK 7.