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?