我开发了一个在 Linux 环境下运行的独立应用程序 (.jar),我想将此应用程序保留在服务器 (Linux) 上并希望通过其他系统访问。
请建议这是否可能。
我开发了一个在 Linux 环境下运行的独立应用程序 (.jar),我想将此应用程序保留在服务器 (Linux) 上并希望通过其他系统访问。
请建议这是否可能。
你考虑过Java Webstart吗?它将允许客户端从网络服务器下载您的应用程序并在本地运行它。
它传统上与 GUI(Swing 等)应用程序一起使用,但我用它在本地运行守护程序和服务器进程。
它会自动处理应用程序更新,因此您的客户只会在需要时下载一个版本。否则,他们将访问其本地缓存的版本。
Linux 系统实现了 Berkeley 套接字 API,所以是的,您可以为其他机器打开通信。
为此,您可以使用包 java.net。对于套接字连接,我们可以使用:Socket、ServerSocket 和 SocketAddress。
Socket 用于客户端,ServerSocket 用于创建套接字服务器,而 SocketAddress 用于提供将用作目标套接字的信息。
如图所示,请找到以下项目:
第一个项目 SocketServerApp.java - 构建它然后运行 java -jar SocketServerApp.jar
package socketserverapp;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketServerApp {
public static void main(String[] args) throws IOException {
//we defines all the variables we need
ServerSocket server = null;
Socket client = null;
byte[] receivedBuff = new byte[64];
int receivedMsgSize;
try
{
//activate port 8881 as our socket server
server = new ServerSocket(8881);
System.out.println("Server started");
//receiving connection from client
client = server.accept();
System.out.println("Client connected");
}
catch (IOException e)
{
System.out.println(e.getMessage());
System.exit(-1);
}
//prepare data stream
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
//greet user if there is a client connection
String data;
data = "Hello from the Server!";
out.write(data.getBytes());
//accepting data from client and display it in the console.
java.util.Arrays.fill(receivedBuff, (byte)0);
while (true) {
receivedMsgSize = in.read(receivedBuff);
data = new String(receivedBuff);
//if client type "exit", then exit loop and close everything
if (data.trim().equals("exit"))
{
out.write(data.getBytes());
break;
}
java.util.Arrays.fill(receivedBuff, (byte)0);
System.out.println ("Client: " + data);
}
//close all resources before exiting
out.close();
in.close();
client.close();
server.close();
}
}
第二个项目是 SocketClientApp.java - 构建它然后运行 java -jar SocketClientApp.jar
package socketclientapp;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class SocketClientApp {
public static void main(String[] args) throws IOException {
Socket client = null;
InputStream in = null;
OutputStream out = null;
byte[] receivedMsg = new byte[64];
try {
client = new Socket("localhost", 8881);
in = client.getInputStream();
out = client.getOutputStream();
} catch (UnknownHostException e) {
System.err.println(e.getMessage());
System.exit(1);
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(1);
}
String fromServer;
String fromUser;
in.read(receivedMsg);
fromServer = new String(receivedMsg);
System.out.println("Server: " + fromServer);
fromUser = "Hello from Client";
System.out.println("Sent to server: " + fromUser);
out.write(fromUser.getBytes());
fromUser = "exit";
out.write(fromUser.getBytes());
System.out.println("Sent to server: " + fromUser);
out.close();
in.close();
client.close();
}
}
简而言之,这是一种 TCP/IP 通信。这种通信方式在拥有多种软件的企业中非常常见。
希望有帮助。