Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Create a websocket connection

I've been struggling with this for the last week. Anyone has some advices on how to do it?
Basically I have a website that connects with the browser using a websocket connection and start exchanging data. I want to reproduce that in my vb.net program.

How can I do that? From my research I know I need to initiate a GET request at first and upgrade it to websocket.

Should I use the Chilkat library? (thats what I've found on google, but I was not able to use it)
Or maybe I should use the System.net.websocket namespace? (again I tried and I cant understand how to use it)

I appreciate any help. Thanks! 🤝
This would be a lot easier outside of Vb.net. Do you HAVE to do it in .net?

https://www.example-code.com/vbnet/webso...frames.asp

I’d start with that
(Dec 26, 2019 10:34 AM)Charger Wrote:  This would be a lot easier outside of Vb.net. Do you HAVE to do it in .net?

https://www.example-code.com/vbnet/webso...frames.asp

I’d start with that

Yeah, I prefer to do it in Vb.net. What would be easier to use for this? Python?
Also, I know about that. It's the Chilkat library I was talking about. But I'm having some problems with it. I was hoping for a code example or someone to answer some questions
https://docs.microsoft.com/en-us/dotnet/...uest-class
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(Dec 26, 2019 11:34 AM)HFDone Wrote:  https://docs.microsoft.com/en-us/dotnet/...uest-class

Nothing useful here. I'm trying to make a websocket connection, not just create a webrequest.
Imports System.Net.Sockets
Imports System.Text
Module Module1
Sub Main()
Dim serverSocket As New TcpListener(8888)
Dim requestCount As Integer
Dim clientSocket As TcpClient
serverSocket.Start()
msg("Server Started")
clientSocket = serverSocket.AcceptTcpClient()
msg("Accept connection from client")
requestCount = 0

While (True)
Try
requestCount = requestCount + 1
Dim networkStream As NetworkStream =
clientSocket.GetStream()
Dim bytesFrom(10024) As Byte
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
Dim dataFromClient As String =

System.Text.Encoding.ASCII.GetString(bytesFrom)
dataFromClient =
dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
msg("Data from client - " + dataFromClient)
Dim serverResponse As String =

"Server response " + Convert.ToString(requestCount)
Dim sendBytes As [Byte]() =
Encoding.ASCII.GetBytes(serverResponse)
networkStream.Write(sendBytes, 0, sendBytes.Length)
networkStream.Flush()
msg(serverResponse)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End While


clientSocket.Close()
serverSocket.Stop()
msg("exit")
Console.ReadLine()
End Sub

Sub msg(ByVal mesg As String)
mesg.Trim()
Console.WriteLine(" >> " + mesg)
End Sub
End Module
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Dim clientSocket As New System.Net.Sockets.TcpClient()
Dim serverStream As NetworkStream

Private Sub Button1_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles Button1.Click
Dim serverStream As NetworkStream = clientSocket.GetStream()
Dim outStream As Byte() =
System.Text.Encoding.ASCII.GetBytes("Message from Client$")
serverStream.Write(outStream, 0, outStream.Length)
serverStream.Flush()

Dim inStream(10024) As Byte
serverStream.Read(inStream, 0, CInt(clientSocket.ReceiveBufferSize))
Dim returndata As String =

System.Text.Encoding.ASCII.GetString(inStream)
msg("Data from Server : " + returndata)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
msg("Client Started")
clientSocket.Connect("127.0.0.1", 8888)
Label1.Text = "Client Socket Program - Server Connected ..."
End Sub

Sub msg(ByVal mesg As String)
TextBox1.Text = TextBox1.Text + Environment.NewLine + " >> " + mesg
End Sub
End Class
(Dec 27, 2019 02:34 PM)brhack Wrote:  Imports System.Net.Sockets
Imports System.Text
Module Module1
   Sub Main()
       Dim serverSocket As New TcpListener(8888)
       Dim requestCount As Integer
       Dim clientSocket As TcpClient
       serverSocket.Start()
       msg("Server Started")
       clientSocket = serverSocket.AcceptTcpClient()
       msg("Accept connection from client")
       requestCount = 0

       While (True)
           Try
               requestCount = requestCount + 1
               Dim networkStream As NetworkStream =
                       clientSocket.GetStream()
               Dim bytesFrom(10024) As Byte
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
               Dim dataFromClient As String =

                       System.Text.Encoding.ASCII.GetString(bytesFrom)
               dataFromClient =
           dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
               msg("Data from client -  " + dataFromClient)
               Dim serverResponse As String =

                   "Server response " + Convert.ToString(requestCount)
               Dim sendBytes As [Byte]() =
                   Encoding.ASCII.GetBytes(serverResponse)
               networkStream.Write(sendBytes, 0, sendBytes.Length)
               networkStream.Flush()
               msg(serverResponse)
           Catch ex As Exception
               MsgBox(ex.ToString)
           End Try
       End While


       clientSocket.Close()
       serverSocket.Stop()
       msg("exit")
       Console.ReadLine()
   End Sub

   Sub msg(ByVal mesg As String)
       mesg.Trim()
       Console.WriteLine(" >> " + mesg)
   End Sub
End Module
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
   Dim clientSocket As New System.Net.Sockets.TcpClient()
   Dim serverStream As NetworkStream

   Private Sub Button1_Click(ByVal sender As System.Object,

       ByVal e As System.EventArgs) Handles Button1.Click
       Dim serverStream As NetworkStream = clientSocket.GetStream()
       Dim outStream As Byte() =
       System.Text.Encoding.ASCII.GetBytes("Message from Client$")
       serverStream.Write(outStream, 0, outStream.Length)
       serverStream.Flush()

       Dim inStream(10024) As Byte
serverStream.Read(inStream, 0, CInt(clientSocket.ReceiveBufferSize))
       Dim returndata As String =

       System.Text.Encoding.ASCII.GetString(inStream)
       msg("Data from Server : " + returndata)
   End Sub

   Private Sub Form1_Load(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles MyBase.Load
       msg("Client Started")
       clientSocket.Connect("127.0.0.1", 8888)
       Label1.Text = "Client Socket Program - Server Connected ..."
   End Sub

   Sub msg(ByVal mesg As String)
TextBox1.Text = TextBox1.Text + Environment.NewLine + " >> " + mesg
   End Sub
End Class

I'm not looking for a way to connect two applications through TCP... I need a way to connect through a websocket with a website.
I did it in python. Idk how to do it in VB.net
using System.Net.Sockets;
PHP Code

<?php 
Socket socket 
= new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(new System.Net.IPEndPoint(System.Net.Dns.GetHostAddresses("google.com")[0], 80));
byte[] buffer = System.Text.Encoding.UTF8.GetBytes("GET / HTTP/1.1\r\n\r\n");
socket.Send(buffer);
buffer = new byte[1024];
int count = socket.Receive(buffer);
Console.WriteLine("\nData:\n\n{0}", System.Text.Encoding.UTF8.GetString(buffer));
string data = System.Text.Encoding.UTF8.GetString(buffer);
Console.WriteLine("Header: {0}", data.Substring(0, data.IndexOf("\r\n\r\n")));
Console.WriteLine("Footer: {0}", data.Substring(data.IndexOf("\r\n\r\n")));
socket.Close(); 

more info https://searchitchannel.techtarget.com/f...-Framework
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by grweh3wrhFeb 4, 2020 01:48 PM 12 382 Feb 4, 2020 01:48 PM
Last Post: gigajew
[.NET] by v.e.n.o.mJul 5, 2019 11:53 AM 0 282 Jul 5, 2019 11:53 AM
Last Post: v.e.n.o.m
[C#] by TrinityAug 21, 2017 06:33 PM 23 1,090 Aug 21, 2017 06:33 PM
Last Post: DeadKube