Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6254961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:16:36+00:00 2026-05-24T14:16:36+00:00

I am trying to create my own WebSocket Server with Java. When my client

  • 0

I am trying to create my own WebSocket Server with Java.

When my client connects, I get following request:

(14): GET / HTTP/1.1
(18): Upgrade: WebSocket
(19): Connection: Upgrade
(20): Host: localhost:8483
(24): Origin: http://localhost
(45): Sec-WebSocket-Key1: P3$04 H85Zf# 9 9d a0 x10[
(34): Sec-WebSocket-Key2: 416393 2  560Y
(0): 

(The numbers in brackets, the brackets, the colons and the spaces thereafter only being something I add for the System.out.println() command). The numbers in brackets are the length of the line in bytes.

I first process the request using this function:

public boolean processHandshake(int lineNumber, String line){

    if(handshakeProcessed || lineNumber > 9 || lineNumber < 1){

        return false;

    }

    switch(lineNumber){

        case 1:{ handshakeGetLocation = line.replace("GET ", "").replace(" HTTP/1.1", ""); break; }
        case 2:{ handshakeUpgrade = line.replace("Upgrade: ", ""); break; }
        case 3:{ handshakeConnection = line.replace("Connection: ", ""); break; }
        case 4:{ handshakeHost = line.replace("Host: : ", ""); break; }
        case 5:{ handshakeOrigin = line.replace("Origin: ", ""); break; }
        case 6:{ handshakeSecWebSocketKey1 = line.replace("Sec-WebSocket-Key1: ", ""); break; }
        case 7:{ handshakeSecWebSocketKey2 = line.replace("Sec-WebSocket-Key2: ", ""); handshakeProcessed = false; break; }
        case 8:{ handshakeProcessed = true; }
        case 9:{ handshakeProcessed = true; }

    }

    return true;

}

Now, according to this article and assuming it’s the first version of the protocol I need to process, I’ve been wondering how to deal with the quotients:

The thing is, for each key, I need to divide the number of digits by that of the spaces. I’ve been doing it like that:

private double calculateKeyReply(String key){

    double numCount = key.replaceAll("[^0-9]", "").length();
    double spaceCount = key.replaceAll("[^\\ ]", "").length();

    System.out.println(numCount+"/"+spaceCount+"="+numCount/spaceCount);

    return numCount/spaceCount;

}

And calling following function (replyHandshake()):

String handshake;

handshake = "HTTP/1.1 101 WebSocket Protocol Handshake\n";
handshake += "Upgrade: "+handshakeUpgrade+"\n"; // handshakeUpgrade and the following variables are instance variables I set when I process the request
handshake += "Connection: "+handshakeConnection+"\n";
handshake += "Sec-WebSocket-Origin: "+handshakeOrigin+"\n";
handshake += "Sec-WebSocket-Location: "+handshakeOrigin.replace("http", "ws")+handshakeGetLocation+"\n";
handshake += "Sec-WebSocket-Protocol: sample\n";
// handshake += "\n";

String nums = calculateKeyReply(handshakeSecWebSocketKey1)+""+calculateKeyReply(handshakeSecWebSocketKey2);

MessageDigest md5Digestor = MessageDigest.getInstance("MD5");
String md5 = new String(md5Digestor.digest(nums.getBytes()));

handshake += md5;

return handshake;

And then, somewhere else:

out.println(replyHandshake());

Am I doing something wrong? I’m testing it with the latest version of Google Chrome.

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T14:16:38+00:00Added an answer on May 24, 2026 at 2:16 pm

    If you go the extra mile and implement a server for yourself from scratch today, I would target the latest version of the protocol (version 8, draft 10).

    The above handshake is from an outdated version.

    Chrome 14 and Firefox 7/8 support the latest. Firefox 6 has a (disabled by default) old version. Chrome might very well drop support for any version <8.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a websocket server where each client establish its own redis
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
I'm trying to create my own slideshow. The following code fades from one image
Im trying to create my own url scheme so my android app can get
I am trying to create my own module, i followed the following tutorial: LINK
I want to create my own WebSocket server, but it will only include a
I'm trying to create my own version of HashMap with some utility methods. Foo.java
I'm trying to create my own error window for the project I am working
Trying to create a list to return some JSON data to a view. Following
Trying to create my own custom AttachedProperty for a WPF DependencyObject failed to actually

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.