簡體   English   中英

如何從服務器到任何客戶端運行獨立Java應用程序

[英]How to Run Stand Alone Java Application from server to any Client

我已經開發了一個獨立的應用程序(.jar),它正在Linux環境中運行,我希望將此應用程序保存在服務器(Linux)上並希望通過其他系統進行訪問。

請建議這是否可行。

你考慮過Java Webstart嗎? 它允許客戶從Web服務器下載您的應用程序並在本地運行它。

它傳統上與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通信。 這種類型的通信方法在具有多種軟件的企業中非常常見。

希望有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM