I recently visited heroku.com site and tried to deploy my first java program there , I actually had a good start using their java deployment tutorial, and had it run ok. now I have a server code which I need to deploy there , I tried to follow the example but I had some question in mind like,
1- what will be the host in this case , I already tried the app link as if its the host but it throws errors ,
here is my sample server code
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();
}
}
}
here is my client code
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);
}
}
I followed this tutorial https://devcenter.heroku.com/articles/java at their site to upload my server code is there something else I need to do ?!
thanks in advance
On Heroku, your application must bind to the HTTP port provided in the $PORT environment variable. Given this, the two major problems in your application code above are 1) you are binding to a hardcoded port (
6780
) and 2) your application is using TCP instead of HTTP. As shown in the tutorial, use something like Jetty to accomplish the HTTP equivalent of your application and useSystem.getenv("PORT")
to bind to the right port, like this: