2013-05-15 17 views
5

私は最近heroku.comサイトを訪れ、そこで最初のJavaプログラムをデプロイしようとしましたが、実際にはJavaデプロイメントチュートリアルを使い始めるのが良かったです。今私はそこにデプロイする必要があるサーバーコードを持っていますが、私はその例に従おうとしましたが、私はいくつか質問しましたが、Herokuにシンプルなサーバーコードをデプロイ

1-この場合のホストは何ですか、そのホストは、それがエラーをスローした場合、ここで

はここ
public class DateServer { 

    /** Runs the server. */ 
    public static void main(String[] args) throws IOException { 
     ServerSocket listener = new ServerSocket(6780); 
     try { 
      while (true) { 
       Socket socket = listener.accept(); 
       try { 
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true); 
        out.println(new Date().toString()); 
       } finally { 
        socket.close(); 
       } 
      } 
     } finally { 
      listener.close(); 
     } 
    } 
} 

が、私はこのチュートリアルに従っ

public class DateClient { 

    /** Runs the client as an application. First it displays a dialog box asking for the IP address or hostname of a host running the date server, then connects to it and displays the date that it serves. */ 
    public static void main(String[] args) throws IOException { 
     //I used my serverAddress is my external ip address 
     Socket s = new Socket(serverAddress, 6780); 
     BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream())); 
     String answer = input.readLine(); 
     JOptionPane.showMessageDialog(null, answer); 
     System.exit(0); 
    } 
} 

私のクライアントコードである私のサンプルサーバコードです自分のサイトにをアップロードしてサーバーコードをアップロードする必要がありますか? Herokuの上で事前

答えて

5

おかげで、あなたのアプリケーションがbind to the HTTP port provided in the $PORT environment variableをしなければなりません。上記のアプリケーションコードの2つの大きな問題は、1)ハードコードされたポート(6780)にバインドしていること、2)アプリケーションがHTTPではなくTCPを使用していることです。 tutorialに示すように、アプリケーションのHTTP相当を達成するために桟橋のようなものを使用してSystem.getenv("PORT")を使用してこのように、右側のポートにバインドする:

import java.util.Date; 
import java.io.IOException; 
import javax.servlet.ServletException; 
import javax.servlet.http.*; 
import org.eclipse.jetty.server.Server; 
import org.eclipse.jetty.servlet.*; 

public class HelloWorld extends HttpServlet { 

    @Override 
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
      throws ServletException, IOException { 
     resp.getWriter().print(new Date().toString()); 
    } 

    public static void main(String[] args) throws Exception{ 
     Server server = new Server(Integer.valueOf(System.getenv("PORT"))); 
     ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); 
     context.setContextPath("/"); 
     server.setHandler(context); 
     context.addServlet(new ServletHolder(new HelloWorld()),"/*"); 
     server.start(); 
     server.join(); 
    } 
} 
+0

OKこれで通常のHTTP接続クライアントコードになります場合?!これは実際にかなり簡単な例です。私が実際にやっていることは、クライアントがサーバーに接続して遊ぶことができるマルチプレイヤーゲームを構築することです。マルチプレイヤーをサポートするようにコードを拡張しようとしているので、助けていただければ幸いです。 –

+0

はい、クライアントもHTTPを使用する必要があります。 HerokuアプリケーションはアウトバウンドTCP接続(データベースなど)を行うことができますが、すべてのインバウンド接続はHTTP経由で行う必要があります。詳細については、[Heroku HTTPルーティングに関する情報](https://devcenter.heroku.com/articles/http-routing)を参照してください。 – ryanbrainard

関連する問題