简体   繁体   English

客户端上的Java服务器与Javascript之间的套接字通信

[英]Socket Communication between Java server on client and Javascript

I want to establish a socket communication between javascript running on a web page and a java SocketServer running on my client machine so that as soon as the connection is established between the two, an excel sheet is opened on the client machine. 我想在网页上运行的javascript和客户端计算机上运行的java SocketServer之间建立套接字通信,以便在两者之间建立连接后立即在客户端计算机上打开excel工作表。 I know that it would cause security issues, but since the communication would be on localhost so I am Ok with that. 我知道这会导致安全问题,但是由于通信将在localhost上进行,所以我对此表示满意。

Here is my java server running on client: 这是我在客户端上运行的Java服务器:

import java.io.*;
import java.net.*;

public class ServerExcelOpenOnJavaScriptConnect {
    public static void main(String args[]) throws Exception {
        ServerSocket welcomeSocket = new ServerSocket(12345);


            Socket connectionSocket = welcomeSocket.accept();
            Process p =
                    Runtime.getRuntime()
                            .exec("C:\\Program Files (x86)\\Microsoft Office\\Office14\\excel.exe c:\\users\\rahulserver\\desktop\\abcd.xlsx");
            System.out.println("Waiting for excel file ...");
            p.waitFor();
            System.out.println("Excel file done.");

            //Runtime.getRuntime().exec();

    }
}

Here is my html with javascript: 这是我的Javascript HTML:

    <html>
<head>
    <title>TCP Socket test</title>
    <script type="text/javascript">
    function connect(){
                var host = 'localhost';
                var port = 12345;
        var socket = new io.Socket('localhost',{'port':12345});
        socket.connect();
        alert("connected");
}



    </script>
</head>

<body>
    <button onclick="connect()">Connect</button>
</body>
</html>

The connection is not getting established as the server keeps on waiting for connection on port 12345. So how should it be done? 由于服务器一直在等待端口12345上的连接,因此未建立连接。那么该怎么办呢?

You're kind of on a dirt road and you need to get yourself on a freeway. 您有点在土路上,需要让自己走上高速公路。

javascript can communicate to java code in the specific case where the browser got the web page and javascript from a host and then the javascript connects back to that same host to get a connection for more data. 在特定情况下,当浏览器从主机获取网页和javascript时,javascript可以与Java代码进行通信,然后javascript连接回到同一主机以获取更多数据的连接。

In your case, you'd like the same machine to be both client and server. 对于您的情况,您希望同一台计算机既是客户端又是服务器。 But it is easier to understand what's required if you realize this idea of two machines: a client running the browser and the server which runs java. 但是,如果您同时意识到两台计算机的概念,则更容易理解所需的内容:运行浏览器的客户端和运行Java的服务器。

So, you can find many examples of this on the web. 因此,您可以在网上找到许多示例。 For example, take a look at java running in Tomcat, providing a web page and javascript and then the javascript reading json data using a different URL on the Tomcat server. 例如,看一下在Tomcat中运行的Java,提供一个网页和javascript,然后该javascript使用Tomcat服务器上的另一个URL读取json数据。

If you think of the main-stream uses of web technology you can find videos demonstrating what you'd like to see. 如果您想到网络技术的主流用途,则可以找到演示您想要看到的内容的视频。 Look around, for example, at the AngularJS demos and RESTful Web Services demos. 环顾四周,例如AngularJS演示和RESTful Web Services演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM