2

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. I know that it would cause security issues, but since the communication would be on localhost so I am Ok with that.

Here is my java server running on client:

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:

    <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?

7
  • I don't know what java_socket_bridge is, but you cannot connect to an arbitrary TCP socket from javascript running in a browser, only HTTP. And while you could bridge TCP over HTTP, what you write in java would have to speak HTTP, not just a normal socket. Short answer: you can't. Commented Aug 5, 2013 at 23:40
  • @xaxxon I have corrected my code and removed the line of java_socket_bridge. So you mean that to make this work, I need to establish an HTTP socket in java. Let me give it a try. Commented Aug 6, 2013 at 2:55
  • javascript running in a web browser can only initiate http connections. But you can use ajax and websockets. However, writing your own server for doing websockets is challenging. Commented Aug 6, 2013 at 3:32
  • @xaxxon So you mean that there is no easy way to make javascript communicate with anything running on local client machine? Commented Aug 6, 2013 at 3:34
  • if by easy you mean without using some sort of browser plugin (like java or flash), then yes. It's not hard, it's impossible. That's a security issue. Commented Aug 6, 2013 at 3:43

1 Answer 1

1

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.

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.

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.

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man for answering. the time I asked this was when I just had started professional programming. Now I am quiet seasoned at it ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.