通过ServerSocket充当服务器的Javaclass可以在线托管吗?
Can a Java class that acts as a server via ServerSocket be hosted online?
我做了一个非常简单的命令聊天 java 基础应用程序。
尽管该项目由一个充当服务器,另一个充当发送套接字并从服务器接收套接字的客户端组成。
但服务器不是 Servlet 或任何其他类型,它只是一个简单的普通 java class,通过 ServerSocket 充当服务器...
如果它可以在线托管(最好是云),我怎么知道客户端要使用的新主机名???
端口会改成吗???
服务器class
public class ServerDoor {
public static void main(String args[]) throws IOException {
final int portNumber = 81;
ServerSocket serverSocket = new ServerSocket(portNumber);
while (true) {...}
}
}
客户端
public static void main(String args[]) throws IOException {
final String host = "localhost"; // the host name!
final int portNumber = 81;
System.out.println("Creating socket to '" + host + "' on port " + portNumber);
while (true) {...}
}
您可以在 Windows Azure 或 Amazon EC2 或其他云托管公司托管您的服务器。客户端需要连接的主机名是服务器的 IP 地址或 DNS 名称。也许更改您的客户端以从命令行参数而不是 hard-coding localhost.
接受主机名和端口
您的服务器绑定的端口需要与您的客户端连接的端口相同。也可以考虑为此使用命令行参数。
Java sockets tutorial 也很有用。
最简单的入门方法是创建一个 VM from the cloud hosting company. You can use SSH to remote into your cloud VM 以便 运行 命令。如果您不熟悉命令行应用程序,您应该稍微研究一下。您必须知道如何从命令行 运行 您的 Java 程序。
拥有 VM 后,使用 SCP or another copy utility to copy your Server Java jar or .class files onto your cloud VM. You can then run your Server program from the cloud VM. Make sure that your cloud VM allows connections from the port that you will be using. You can open or close ports for your VM from your cloud hosting VM settings。
服务器程序 运行 在您的云 VM 中运行后,您可以连接到 VM 的主机名和端口。主机名将是您创建的 VM 实例的域名。创建 VM 时,您可能必须为其指定一个唯一的名称。它也可能与您用于通过 SSH 连接到计算机的名称相同。
我做了一个非常简单的命令聊天 java 基础应用程序。
尽管该项目由一个充当服务器,另一个充当发送套接字并从服务器接收套接字的客户端组成。
但服务器不是 Servlet 或任何其他类型,它只是一个简单的普通 java class,通过 ServerSocket 充当服务器...
如果它可以在线托管(最好是云),我怎么知道客户端要使用的新主机名??? 端口会改成吗???
服务器class
public class ServerDoor {
public static void main(String args[]) throws IOException {
final int portNumber = 81;
ServerSocket serverSocket = new ServerSocket(portNumber);
while (true) {...}
}
}
客户端
public static void main(String args[]) throws IOException {
final String host = "localhost"; // the host name!
final int portNumber = 81;
System.out.println("Creating socket to '" + host + "' on port " + portNumber);
while (true) {...}
}
您可以在 Windows Azure 或 Amazon EC2 或其他云托管公司托管您的服务器。客户端需要连接的主机名是服务器的 IP 地址或 DNS 名称。也许更改您的客户端以从命令行参数而不是 hard-coding localhost.
接受主机名和端口您的服务器绑定的端口需要与您的客户端连接的端口相同。也可以考虑为此使用命令行参数。
Java sockets tutorial 也很有用。
最简单的入门方法是创建一个 VM from the cloud hosting company. You can use SSH to remote into your cloud VM 以便 运行 命令。如果您不熟悉命令行应用程序,您应该稍微研究一下。您必须知道如何从命令行 运行 您的 Java 程序。
拥有 VM 后,使用 SCP or another copy utility to copy your Server Java jar or .class files onto your cloud VM. You can then run your Server program from the cloud VM. Make sure that your cloud VM allows connections from the port that you will be using. You can open or close ports for your VM from your cloud hosting VM settings。
服务器程序 运行 在您的云 VM 中运行后,您可以连接到 VM 的主机名和端口。主机名将是您创建的 VM 实例的域名。创建 VM 时,您可能必须为其指定一个唯一的名称。它也可能与您用于通过 SSH 连接到计算机的名称相同。