如何从服务器到任何客户端运行独立Java应用程序

时间:2012-06-26 10:26:42

标签: java client-server localhost

我开发了一个独立的应用程序(.jar),它正在Linux环境中运行,我希望将此应用程序保存在服务器(Linux)上,并希望通过其他系统进行访问。

请建议这是否可能。

2 个答案:

答案 0 :(得分:6)

您考虑过Java Webstart了吗?它允许客户从Web服务器下载您的应用程序并在本地运行它。

它传统上与GUI(Swing等)应用程序一起使用,但我已经用它来在本地运行守护进程和服务器进程。

它会自动处理应用程序更新,因此您的客户只会在需要时下载该版本。否则,他们将访问他们的本地缓存版本。

答案 1 :(得分:1)

Linux系统实现了Berkeley套接字API,所以是的,你可以打开其他机器的通信。

为此,您可以使用包java.net。对于套接字连接,我们可以使用:Socket,ServerSocket和SocketAddress。

Socket用于客户端,ServerSocket用于创建套接字服务器,SocketAddress用于提供将用作目标套接字的信息。

如图所示,请查看以下项目:

First Project 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通信。这种通信方法在拥有多种软件的企业中非常常见。

希望有所帮助。