EnKrypt
EnKrypt

Reputation: 777

Why is the handshake failing?

Im using the websocket4j library and I looked at an example on how to use it in server applications. I took the code from there and imeplemented it into my current project which was using raw sockets before. No matter which client I use , I seem to be getting An IOExcpetion saying that the handshake has failed. I have no clue whats wrong cause I took code from the example server ( which is obviously supposed to work ) , and I used a javascript example client to make sure that its not the client's fault. Heres the main method.

 public static void main(String args[]){
        BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("PORT ( default - 1000 ): ");
        int port=1000;
        try{
            port=Integer.parseInt(b.readLine());
        }
        catch(Exception e){
            System.out.println("Wrong format. Using 1000 instead");
            port=1000;
        }
        System.out.println("Server Started. Waiting for connections");
        try{
            WebServerSocket listener=new WebServerSocket(port);
            while(true){
                WebSocket server=listener.accept();
                U413ChatServer connection=new U413ChatServer(server);
                Thread t=new Thread(connection); 
                t.start();
            }
        }
        catch(Exception e){ e.printStackTrace();
        }
    }

And heres what comes on screen

Server Started. Waiting for connections
java.io.IOException: Handshake failed
        at websocket4j.AbstractWebSocket.<init>(Unknown Source)
        at websocket4j.server.WebSocket.<init>(Unknown Source)
        at websocket4j.server.WebServerSocket.accept(Unknown Source)
        at U413ChatServer.main(U413ChatServer.java:55)
Caused by: java.io.IOException: Unexpected header field: Sec-WebSocket-Key: yfOg
xCbblVyY8ARVFyMtOw==
        at websocket4j.server.WebSocket.handshake(Unknown Source)
        at websocket4j.AbstractWebSocket$HandshakeRunner.run(Unknown Source)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:47
1)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
        at java.util.concurrent.FutureTask.run(FutureTask.java:166)
        at java.lang.Thread.run(Thread.java:722)

Line 55 is:

WebSocket server=listener.accept();

Any help would be really appreciated

Upvotes: 1

Views: 4730

Answers (2)

Altanai
Altanai

Reputation: 1393

Yes assylias is right . The websocket4j kit that you are using is indeed outdated and requires two keys and the token value to make a response , which fails due to either number divided by zero exception or end of stream exception .

Upvotes: 0

andih
andih

Reputation: 5603

The WebSocket Protocol your client uses is incompatible with the Protocol version of the Server.

The Server implements the outdated (Expires: February 17, 2011) WebSocket protocol draft-ietf-hybi-thewebsocketprotocol-latest whereas your client may implement something like the RFC6455 (standard).

You should have a look at other implementations like Netty or the Jetty WebSocketServer

Upvotes: 1

Related Questions