Nicolas B
Nicolas B

Reputation: 315

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

Upvotes: 0

Views: 90

Answers (2)

user207421
user207421

Reputation: 310957

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

Upvotes: 0

mBogaz
mBogaz

Reputation: 136

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.

Upvotes: 3

Related Questions