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

  • SEARCH
  • Home
  • 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 3351942
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:58:11+00:00 2026-05-18T01:58:11+00:00

I seem to be having trouble getting a ‘proper’ connection between my Java server

  • 0

I seem to be having trouble getting a ‘proper’ connection between my Java server and my JavaScript client. It appears to connect okay, the client sends its header okay, but that’s as far as it gets. The onopen or onmessage functions are never triggered at all.

Here’s the code for the Java server:

import java.net.*;
import java.io.*;
public class Server {
    static DataOutputStream out;
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(8112);
            System.out.println("Server Started");
            while(true) {
            Socket socket = serverSocket.accept();
            System.out.println("A client connected");
            out = new DataOutputStream(socket.getOutputStream());

            //Send a simple-as-can-be handshake encoded with UTF-8
            String handshake = "HTTP/1.1 101 Web Socket Protocol Handshake\r" +
            "Upgrade: WebSocket\r" +
            "Connection: Upgrade\r" +
            "WebSocket-Origin: http://localhost\r" +
            "WebSocket-Location: ws://localhost:8112/\r" +
            "WebSocket-Protocol: sample\r\n\r\n";
            out.write(handshake.getBytes("UTF8"));
            System.out.println("Handshake sent.");

            //Send message 'HI!' encoded with UTF-8
            String message = "HI!";
            out.write(0x00);
            out.write(message.getBytes("UTF8"));
            out.write(0xff);
            System.out.println("Message sent!");

            //Cleanup
            socket.close();
            out.close();
            System.out.println("Everything closed!");
        }
    } catch(Exception e) {
        System.out.println(e.getMessage());
    }
}

And here’s the code for the client:

<html>
  <head>
    <meta charset="UTF-8">
    <script>
      function load() {
        var ssocket = new WebSocket("ws://localhost:8112/");
        socket.onopen = function(e) { alert("opened"); }
        socket.onclose = function(e) { alert("closed"); }
        socket.onmessage = function(e) { alert(e.data); }
      }
    </script>
  </head>
    <body onload="load();">
    </body>
</html>

Why won’t onopen or onmessage trigger? I’ve tried a lot of different things I just can’t seem to do it.

What am I doing wrong?

  • 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-18T01:58:12+00:00Added an answer on May 18, 2026 at 1:58 am

    There’s a challenge-response aspect to the protocol that you appear to be missing – the client sends two extra headers and some random data:

    GET /demo HTTP/1.1
    Host: example.com
    Connection: Upgrade
    Sec-WebSocket-Key2: 12998 5 Y3 1  .P00
    Sec-WebSocket-Protocol: sample
    Upgrade: WebSocket
    Sec-WebSocket-Key1: 4 @1  46546xW%0l 1 5
    Origin: http://example.com
    
    ^n:ds[4U
    

    and the server is expected to derive a MD5 response by:

    • extract digits from each key (4146546015, 1299853100)
    • divide by number of spaces in key
    • serialize keys as 4-byte integers, concatenate with data
    • calculate MD5 digest

    producing a response like:

    HTTP/1.1 101 WebSocket Protocol Handshake
    Upgrade: WebSocket
    Connection: Upgrade
    Sec-WebSocket-Origin: http://example.com
    Sec-WebSocket-Location: ws://example.com/demo
    Sec-WebSocket-Protocol: sample
    
    8jKS'y:G*Co,Wxa-
    

    This process is designed to prevent non-WebSocket requests from being handled by WebSocket servers – see section 1.3 of the WebSocket protocol for details.

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

Sidebar

Related Questions

I'm having trouble getting my head around algorithm analysis. I seem to be okay
I'm having trouble getting the following to work in SQL Server 2k, but it
I'm having trouble getting the follow code to work in Internet Explorer, it doesn't
I'm having trouble getting a texture to map onto geometry properly with OpenGL. In
I'm having trouble getting a JTextArea to scroll. I'm not sure how you can
I'm having trouble getting the following JQuery script to function properly - its functionality
I'm having trouble getting a rails app on Dreamhost's Passenger to see compiled libraries
I'm having trouble getting regular expressions with leading / trailing $'s to match in
I'm having trouble getting the RelayCommand to enable/disable the attached control properly. I've got
I'm having trouble getting this code to set the prompt: // Create a PeoplePicker

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.