java - 如何在openshift中运行java服务器socket程序(类文件)

标签 java sockets openshift

我想在我的 openshift 服务器中运行套接字程序。

我的action_hooks/start:-

#!/bin/bash
# The logic to start up your application should be put in this
# script. The application will work only if it binds to
# $OPENSHIFT_DIY_IP:8080
#nohup $OPENSHIFT_REPO_DIR/diy/testrubyserver.rb $OPENSHIFT_DIY_IP $OPENSHIFT_REPO_DIR/diy |& /usr/bin/logshifter -tag diy &
java -cp $OPENSHIFT_REPO_DIR/diy/EchoServer

我的服务器程序:-

import java.net.*; 
import java.io.*; 

public class EchoServer
{ 
 public static void main(String[] args) throws IOException 
   { 
    ServerSocket serverSocket = null; 

    try { 
         serverSocket = new ServerSocket(); 
         serverSocket.bind(new InetSocketAddress("xxx.xxx.xx.xx",8080));
        } 
    catch (IOException e) 
        { 
         System.err.println("Could not listen on port: 8080."); 
         System.exit(1); 
        } 

    Socket clientSocket = null; 
    System.out.println ("Waiting for connection.....");

    try { 
         clientSocket = serverSocket.accept(); 
        } 
    catch (IOException e) 
        { 
         System.err.println("Accept failed."); 
         System.exit(1); 
        } 

    System.out.println ("Connection successful");
    System.out.println ("Waiting for input.....");

    PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), 
                                      true); 
    BufferedReader in = new BufferedReader( 
            new InputStreamReader( clientSocket.getInputStream())); 

    String inputLine; 

    while ((inputLine = in.readLine()) != null) 
        { 
         System.out.println ("Server: " + inputLine); 
         out.println(inputLine); 

         if (inputLine.equals("Bye.")) 
             break; 
        } 

    out.close(); 
    in.close(); 
    clientSocket.close(); 
    serverSocket.close(); 
   } 
}

我的客户端程序:-

import java.io.*;
import java.net.*;

public class EchoClient {
    public static void main(String[] args) throws IOException {

        String serverHostname = new String ("xxxxxxxxxxx.rhcloud.com");

        if (args.length > 0)
           serverHostname = args[0];
        System.out.println ("Attemping to connect to host " +
        serverHostname + " on port 8080.");

        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            echoSocket = new Socket(serverHostname, 8080);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                                        echoSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: " + serverHostname);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for "
                               + "the connection to: " + serverHostname);
            System.exit(1);
        }

    BufferedReader stdIn = new BufferedReader(
                                   new InputStreamReader(System.in));
    String userInput;

        System.out.print ("input: ");
    while ((userInput = stdIn.readLine()) != null) {
        out.println(userInput);
        System.out.println("echo: " + in.readLine());
            System.out.print ("input: ");
    }

    out.close();
    in.close();
    stdIn.close();
    echoSocket.close();
    }
}

我的日志文件“diy.log”

[2015-12-22 11:59:03] INFO  WEBrick 1.3.1
[2015-12-22 11:59:03] INFO  ruby 1.8.7 (2013-06-27) [x86_64-linux]
[2015-12-22 11:59:03] INFO  WEBrick::HTTPServer#start: pid=44992 port=8080
[2015-12-22 12:05:36] INFO  going to shutdown ...
[2015-12-22 12:05:36] INFO  WEBrick::HTTPServer#start done.

我不知道服务器程序是否自动启动,但是当我通过ssh运行服务器程序时,程序正在运行但不响应客户端。

最佳答案

您似乎正在尝试使用客户端连接到端口 8080:

echoSocket = new Socket(serverHostname, 8080);

服务器仍然需要绑定(bind)到 8080 才能从外部访问,但客户端实际上应该连接到 80 或 8000(websockets)。请参阅this diagram了解有关如何在 OpenShift 上路由请求的详细信息。

在推送代码时检查终端输出,以查看启动服务器的操作 Hook 是否正常工作。确保有the hook file executable .

关于java - 如何在openshift中运行java服务器socket程序(类文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37103017/

相关文章:

java - Openshift 中的 Jersey JAX-RS REST 404,在本地 Tomcat 中运行良好

javascript - 使用 Node.js 和 Express 进行 OpenShift 崩溃循环退避

java - 多线程聊天服务器中迭代器出现 ConcurrentModificationException 异常

java - Spark Worker 节点自动停止

java - 使用java nio的部分消息问题

node.js - WebSocket 握手期间出错 : 'Connection' header is missing

javascript - OpenShift WebSocket 下降到 xhr-polling no websocket

java - 如何在 adobe indesign 中创建我们自己的插件

java - 如何解决 Java 和 Spring Boot 中的 slf4j logback 类路径错误?

C 客户端/服务器与 fprintf 通信?