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 8684609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:21:07+00:00 2026-06-12T22:21:07+00:00

I am trying to create the simplest WebSocket server with VB.NET. I manage to

  • 0

I am trying to create the simplest WebSocket server with VB.NET. I manage to implement handshake but fail to send message after handshake. The following is my code:

Dim serverTcp As TcpListener
Dim serverThread As Thread

Sub Main()
    '' Start server
    serverThread = New Thread(AddressOf serverProc)
    serverThread.Start()
End Sub

Private Sub serverProc()
    '' Listen to port 8181
    serverTcp = New TcpListener(8181)
    serverTcp.Start()

    Console.WriteLine("Listen to port 8181 ...")

    '' Accept any connection
    While (True)
        Dim curSocket As Socket = serverTcp.AcceptSocket()
        Dim thread As New Thread(AddressOf clientProc)
        thread.Start(curSocket)
    End While
End Sub

Private Sub clientProc(ByVal sck As Socket)
    Dim netStream As New NetworkStream(sck)
    Dim netReader As New IO.StreamReader(netStream)
    Dim netWriter As New IO.StreamWriter(netStream)

    Dim key As String = ""

    Console.WriteLine("Accept new connection ...")

    '' Reading handshake message
    While (True)
        Dim line As String = netReader.ReadLine()
        If line.Length = 0 Then
            Exit While
        End If

        If (line.StartsWith("Sec-WebSocket-Key: ")) Then
            key = line.Split(":")(1).Trim()
        End If

        Console.WriteLine("Data: " & line)
    End While

    '' Calculate accept-key
    key += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
    key = getSHA1Hash(key)

    '' Response handshake message
    Dim response As String
    response = "HTTP/1.1 101 Switching Protocols" & vbCrLf
    response &= "Upgrade: websocket" & vbCrLf
    response &= "Connection: Upgrade" & vbCrLf
    response &= "Sec-WebSocket-Accept: " & key & vbCrLf & vbCrLf
    netWriter.Write(response)
    netWriter.Flush()

    '' Sending Hello World message
    Dim message As String = "Hello World"
    Dim messageByte() As Byte = System.Text.Encoding.UTF8.GetBytes(message)
    Dim startByte() As Byte = {&H0}
    Dim endByte() As Byte = {&HFF}

    sck.Send(startByte, 1, 0)
    sck.Send(messageByte)
    sck.Send(endByte, 1, 0)
End Sub

Function getSHA1Hash(ByVal strToHash As String) As String
    Dim sha1Obj As New Security.Cryptography.SHA1CryptoServiceProvider
    Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)
    Dim result As String

    bytesToHash = sha1Obj.ComputeHash(bytesToHash)
    result = Convert.ToBase64String(bytesToHash)

    Return result
End Function

The standard says that I need to send 0x00 byte, following by byte of UTF8, and end with 0xFF byte. I have done as it say yet my HTML client cannot receive the message.

The following is my HTML5 code:

<script>
if('WebSocket' in window){
  connect('ws://localhost:8181/service');
}

function connect(host) {
  var ws = new WebSocket(host);
  ws.onopen = function () {
    alert('connected');
  };

  ws.onmessage = function (evt) {  
    alert('reveived data:'+evt.data);
  };

  ws.onclose = function () {
    alert('socket closed');
  };
};
</script>
  • 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-06-12T22:21:08+00:00Added an answer on June 12, 2026 at 10:21 pm

    The standard says that I need to send 0x00 byte, following by byte of UTF8, and end with 0xFF byte

    R̶e̶a̶l̶l̶y̶?̶ ̶W̶h̶e̶r̶e̶ ̶d̶i̶d̶ ̶y̶o̶u̶ ̶g̶e̶t̶ ̶t̶h̶a̶t̶?̶ ̶I̶ ̶d̶o̶n̶’̶t̶ ̶t̶h̶i̶n̶k̶ ̶t̶h̶e̶r̶e̶ ̶i̶s̶ ̶s̶o̶m̶e̶t̶h̶i̶n̶g̶ ̶l̶i̶k̶e̶ ̶t̶h̶i̶s̶ ̶i̶n̶ ̶t̶h̶e̶ s̶p̶e̶c̶i̶f̶i̶c̶a̶t̶i̶o̶n̶.

    Simonic statet in his comment that starting with 0x00 and ending with 0xFF was used in the deprecated Hixie-76 variant of the protocol.

    Have a look at the actual specification (RFC 6455).


    For better understanding, have a look at this great answer:

    The frames you’re sending need to be formatted according to the
    WebSocket framing format. For sending messages, this format is as
    follows:

    • one byte which contains the type of data (and some additional info
      which is out of scope for a trivial server)
    • one byte which contains the length
    • either two or eight bytes if the length does not fit in the
      second byte (the second byte is then a code saying how many bytes are
      used for the length)
    • the actual (raw) data

    I translated this java implementation for you (in a quick’n’dirty way, there may be some errors) to VB.Net, and it’s working:

    Sub SendMessage(sck As Socket, message as String)
        Dim  rawData =  System.Text.Encoding.UTF8.GetBytes(message)
    
        Dim frameCount = 0
        Dim frame(10) As byte
    
        frame(0) = cbyte(129)
    
        if rawData.length <= 125 Then
            frame(1) = CByte( rawData.length)
            frameCount = 2
        else if rawData.length >= 126 AndAlso rawData.length <= 65535
            frame(1) = CByte( 126)
            Dim len = cbyte(rawData.length)
            frame(2) = CByte(((len >> 8 ) & CByte(255)))
            frame(3) = CByte((len & CByte(255)))
            frameCount = 4
        else
            frame(1) = CByte( 127)
            Dim len = CByte( rawData.length)
            frame(2) = CByte(((len >> 56 ) & CByte(255)))
            frame(3) = CByte(((len >> 48 ) & CByte(255)))
            frame(4) = CByte(((len >> 40 ) & CByte(255)))
            frame(5) = CByte(((len >> 32 ) & CByte(255)))
            frame(6) = CByte(((len >> 24 ) & CByte(255)))
            frame(7) = CByte(((len >> 16 ) & CByte(255)))
            frame(8) = CByte(((len >> 8 ) & CByte(255)))
            frame(9) = CByte((len & CByte(255)))
            frameCount = 10
        End If
    
        Dim bLength = frameCount + rawData.length
        Console.WriteLine(frameCount)
        Console.WriteLine(rawData.length)
        Dim reply(bLength+1) as byte
    
        Dim bLim = 0
        for i=0 to frameCount-1
            Console.WriteLine(blim)
            reply(bLim) = frame(i)
            bLim += 1
        Next
        
        for i=0 to rawData.length-1
            Console.WriteLine(blim)
            reply(bLim) = rawData(i)
            bLim += 1
        Next
        
        sck.Send(reply)
    End Sub
    

    You can use it in your clientProc:

    '' Sending Hello World message
    SendMessage(sck, "Hello World")
    

    enter image description here

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

Sidebar

Related Questions

Trying to create a black line in my view to separate text blocks but
Trying to create a new Dedicated Cache Role in Windows Azure but get the
I'm trying to create a simplest hello world driver in Visual Studio 2012 with
I am trying my hardest to find the simplest way to create a basic
Trying to create the simplest EJB using NetBeans 7.0 and EJB 3 in Action
I am trying to create bindings to the FUSE library using JNA , but
I'm trying to test some Subversion commit hooks, but when I use the simplest
I'm trying to use one of the simplest forms of reflection to create an
I'm trying to create a stream of data to the browser using websocket. The
As a test I am trying to create the simplest webpage where I can

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.