Question: How to connect to the started language server via Java socket?
See original GitHub issueI am new to LSP. Previously I want to analyze programs with static analysis techniques (not developing a plugin or extension or editor), but I do not want to write one parser for each language, that leads me to the LSP.
Currently I have successfully started this language server with the following command under windows:
set CLIENT_PORT=8679
set CLIENT_HOST=localhost
$ java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=1044 -Declipse.application=org.eclipse.jdt.ls.core.id1 -Dosgi.bundles.defaultStartLevel=4 -Declipse.product=org.eclipse.jdt.ls.core.product -Dlog.level=ALL -noverify -Xmx1G -jar ./plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.1100.v20190715-0945.jar -configuration ./config_win -data D:\java
Listening for transport dt_socket at address: 1044
Then I write some client code to connect and send request to the server:
public static void main(String[] args) {
try (Socket socket = new Socket("localhost", 1044)) {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
sendInitJava(out, in);
// sendOpen(out, in);
// sendDefinition(out, in);
out.close();
in.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void sendInitJava(PrintWriter out, BufferedReader in) throws IOException {
JSONObject json = new JSONObject();
JSONObject params = new JSONObject();
json.put("jsonrpc", "2.0");
json.put("id", 0);
json.put("method", "initialize");
params.put("rootPath", "D:/java");
params.put("rootUri", "file:///D:/java");
JSONObject capabilities = new JSONObject();
JSONObject textCapabilities = new JSONObject();
JSONObject ref = new JSONObject();
ref.put("dynamicRegistration", true);
textCapabilities.put("references", ref);
JSONObject def = new JSONObject();
def.put("dynamicRegistration", true);
textCapabilities.put("definition", def);
capabilities.put("textDocument", textCapabilities);
JSONObject workspaceCapabilities = new JSONObject();
workspaceCapabilities.put("workspaceFolders", true);
capabilities.put("workspace", workspaceCapabilities);
params.put("capabilities", capabilities);
json.put("params", params);
String content = json.toString() + "\n";
String headers = "Content-Length: " + content.length() + "\r\n\r\n";
String request = headers + content;
out.write(request);
out.flush();
System.out.println("init request:");
System.out.println(request);
json = new JSONObject();
params = new JSONObject();
json.put("jsonrpc", "2.0");
json.put("method", "textDocument/didOpen");
JSONObject textDoc = new JSONObject();
textDoc.put("uri", "file:///D:/java/jj.java");
textDoc.put("languageId", "java");
textDoc.put("version", 0);
params.put("textDocument", textDoc);
json.put("params", params);
content = json.toString() + "\n";
headers = "Content-Length: " + content.length() + "\r\n\r\n";
request = headers + content;
out.write(request);
out.flush();
System.out.println("open request:");
System.out.println(request);
json.put("jsonrpc", "2.0");
json.put("id", 1);
json.put("method", "textDocument/documentSymbol ");
textDoc = new JSONObject();
textDoc.put("uri", "file:///D:/java/jj.java");
params.put("textDocument", textDoc);
json.put("params", params);
content = json.toString() + "\n";
headers = "Content-Length: " + content.length() + "\r\n\r\n";
request = headers + content;
out.write(request);
out.flush();
System.out.println("symbol request:");
System.out.println(request);
json.put("jsonrpc", "2.0");
json.put("id", 2);
json.put("method", "textDocument/definition");
textDoc = new JSONObject();
textDoc.put("uri", "file:///D:/java/jj.java");
textDoc.put("languageId", "java");
params.put("textDocument", textDoc);
JSONObject pos = new JSONObject();
pos.put("line", 24);
pos.put("character", 17);
params.put("position", pos);
// params.put(textDoc);
json.put("params", params);
content = json.toString() + "\n";
headers = "Content-Length: " + content.length() + "\r\n\r\n";
request = headers + content;
out.write(request);
out.flush();
System.out.println("def request:");
System.out.println(request);
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = in.readLine()) != null) {
sb.append(line);
System.out.println(line);
if(!line.startsWith("Content")&&line.length()>0){
line = line.substring(0, line.lastIndexOf("}")+1);
JSONObject response = JSONObject
.parseObject(line);
System.out.println(response.getString("jsonrpc"));
}
}
JSONObject response = JSONObject
.parseObject(sb.toString());
String s = JSON.toJSONString(JSON.parseArray(response.getJSONArray("result").toJSONString()));
System.out.println(s);
}
Then error happens, the Java console gives the following error:
java.net.SocketException: Software caused connection abort: recv failed
While the server side gives the following error:
Debugger failed to attach: handshake failed - received >Content-Length< - expected >JDWP-Handshake<
I found a related issue #181, but failed to find solution. Do you have any idea? @rcjsuen
Issue Analytics
- State:
- Created 4 years ago
- Comments:13 (5 by maintainers)
Top Results From Across the Web
Socket Programming in Java - GeeksforGeeks
1. First run the Server application as, $ java Server. Server started · 2. Then run the Client application on another terminal as,...
Read more >Writing the Server Side of a Socket (The Java™ Tutorials ...
When a request comes in, KKMultiServer accepts the connection, creates a new KKMultiServerThread object to process it, hands it the socket returned from...
Read more >Socket Programming between C server and Java client
1) Start the server · 2) start the client application · 3) User key in some words into the textfield and click send....
Read more >Java Socket Programming - Socket Server, Client example
A socket is one endpoint of a two-way communication link between two programs running on the network. The socket is bound to a...
Read more >Java Socket Programming Examples
When a client connects, the server sends the current datetime to the client. The connection socket is created in a try-with-resources block so ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@Symbolk In the
README.md
file in my language server, I have some bits of example code for connecting to a language server using stdio, network sockets, or JSON-RPC that may be helpful to you. They are written in TypeScript but I’m sure you’ll be able to transcribe them into Java or JavaScript.Is it possible to connect to the language server as a server. The docker example says a port has to be opened on the client and then the language server is started, which reverses their roles. I would like to connect to a remote LS using the method described in this post.