2016-05-08 5 views
0

私は自分のopensshサーバでソケットプログラムを実行したいと思います。openshiftでjavaサーバソケットプログラム(クラスファイル)を実行する方法

私action_hooks /スタート: -

#!/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でサーバプログラムを実行すると、プログラムは動作していますが、クライアントには応答しません。

答えて

1

あなたがあなたのクライアントでポート8080に接続しようとしているようだ:

echoSocket = new Socket(serverHostname, 8080); 

サーバがまだ外部から到達可能であることを8080にバインドする必要がありますが、クライアントは80または8000(WebSocketを)を接続する必要があります実際にOpenShiftでリクエストをルーティングする方法の詳細については、this diagramを参照してください。

コードをプッシュするときにターミナル出力を調べ、サーバーを起動しているアクションフックが機能しているかどうかを確認します。 the hook file executableにしてください。

+0

私はポート8000​​と80(クライアントプログラム)で試しましたが、同じ問題があります。 –

+0

正確に何が失敗しているかの詳細については、[ログファイルの確認](https://developers.openshift.com/managing-your-applications/log-files.html)をお勧めします。質問を編集して詳細を追加することができます。 –

+0

ログファイルには何もありません –

関連する問題