简体   繁体   中英

Java : Opening client socket

I work on a java application. I got a java socket server mapped with a @ServerEndpoint("/wsock")

Form my javascript code I access the WebSocket from this URL : ws://192.9.200.73:8084/socketserver/wsock

I want now access to this socket from my java code. But how can I specify the address "socketserver/wsock" ? I've tried something but I got every time an error message.

This is my test :

Socket s = new Socket("localhost/socketserver/wsock", 8084);

But it doesn't work, I got everytime an error message: ".UnknownHostException: localhost/socketserver/wsock"

Any idea?

Thank's

public static boolean pingHost(String host, int port, int timeout) {
    try (Socket socket = new Socket()) {
        socket.connect(new InetSocketAddress(host, port), timeout);
        return true;
    } catch (IOException e) {
        return false; // Either timeout or unreachable or failed DNS lookup.
    }
}

Try this. Its something like ping. If you get true its connected. But your server should be ready.

You use a Websocket client. You can't use a Socket directly for this. There is a superimposed protocol.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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