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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:32:45+00:00 2026-05-16T08:32:45+00:00

I’m trying to get a VB.net (express 2010) app to connect to a socket

  • 0

I’m trying to get a VB.net (express 2010) app to connect to a socket server (keeping the connection open), how would I go about this? Right now it works using flash XMLsocket, I’m trying to build a new client without flash, using the existing server.

Currently I’m just using a simple window displaying messages, and a place to send messages.

It says I’m connected, but it displays no messages, and sent messages appear to have no effect, when I telnet to the server using the same IP and port, I can see messages coming in to me, so I know I can connect to the server. Here is my code:

Imports System.Text
Imports System.Net.Sockets


Public Class Form1
    Inherits System.Windows.Forms.Form

    Public Delegate Sub DisplayInvoker(ByVal t As String)

    Private mobjClient As TcpClient
    Private marData(1024) As Byte
    Private mobjText As New StringBuilder()

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        mobjClient = New TcpClient("example.com", 7777)
        DisplayText("Connected to host " & "example.com")

        mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoRead, Nothing)
    End Sub

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Send(txtSend.Text)
        txtSend.Text = ""
    End Sub

    Private Sub Send(ByVal t As String)
        Dim w As New IO.StreamWriter(mobjClient.GetStream)
        w.Write(t & vbCr)
        w.Flush()
        DisplayText(vbNewLine & "Sent " & t)
    End Sub

    Private Sub DoRead(ByVal ar As IAsyncResult)
        Dim intCount As Integer
        Try
            intCount = mobjClient.GetStream.EndRead(ar)
            If intCount < 1 Then
                MarkAsDisconnected()
                Exit Sub
            End If

            BuildString(marData, 0, intCount)

            mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoRead, Nothing)
        Catch e As Exception
            MarkAsDisconnected()
        End Try
    End Sub

    Private Sub BuildString(ByVal Bytes() As Byte, ByVal offset As Integer, ByVal count As Integer)
        Dim intIndex As Integer

        For intIndex = offset To offset + count - 1
            If Bytes(intIndex) = 10 Then
                mobjText.Append(vbLf)

                Dim params() As Object = {mobjText.ToString}
                Me.Invoke(New DisplayInvoker(AddressOf Me.DisplayText), params)

                mobjText = New StringBuilder()
            Else
                mobjText.Append(ChrW(Bytes(intIndex)))
            End If
        Next
    End Sub

    Private Sub MarkAsDisconnected()
        txtSend.ReadOnly = True
        btnSend.Enabled = False
        DisplayText(vbNewLine & "Dissconnected")
    End Sub

    Private Sub DisplayText(ByVal t As String)
        txtDisplay.AppendText(t)
    End Sub
End Class
  • 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-16T08:32:45+00:00Added an answer on May 16, 2026 at 8:32 am

    As long as both apps speak TCP/IP, one has a listening server socket, and the other knows the IP and port number of that server socket and isn’t blocked from connecting to it, it doesn’t matter what language either app is written in. The point of having a protocol like TCP/IP is that it’s effectively independent of platform, OS, framework, language, or much of anything else.

    As for your code, a few things stand out:

    • You’re creating a new StreamWriter attached to the network stream every time you send anything. If the writer closes and disposes itself on finalization, which most IDisposables do, it would close the underlying stream (which, in the case of the TcpClient’s stream, will close the connection). If you’re going to use a writer to send data, keep one as an instance variable and reuse it rather than creating a new one each time.

    • From reading about the XmlSocket protocol, it seems the strings sent and received should be null-terminated. That is, your loop inside BuildString should be looking for 0 rather than 10 when breaking up the data into strings, and Send should be appending a nul character (Chr(0)) rather than a vbCr to every string it sends.

    • You really should be using an encoding to convert bytes to chars. Your existing code (when fixed as above) should at least show you some data, assuming there’s any to be sent. You may well find the data corrupted, though, due to the assumption that 1 byte == 1 char — which is rarely the case since Unicode hit it big. 🙂 I’d suggest you use a StreamReader rather than reading directly from the stream — the StreamReader uses an Encoding behind the scenes (the UTF-8 one by default, IIRC), and will handle most of the gory details, so you don’t need to worry about how many bytes to read to get a char. But StreamReaders don’t have the stuff built into them to do async reads. You’d have to change your stuff a bit, and spawn a thread for it, in order to use a StreamReader.

    You could use a Decoder directly, which is pretty much what a StreamReader does. Use it something like this:

    ''// This is important!  Keep the Decoder and reuse it when you read this socket.
    ''// If you don't, a char split across two reads will break.
    Private _decoder As Decoder = UTF8Encoding.GetDecoder()
    
    
    Private Sub BuildString(bytes() As Byte, offset As Integer, byteCount As Integer)
    
        ''// Here's where the magic happens.  The decoder converts bytes into chars.
        ''// But it remembers the final byte(s), and doesn't convert them,
        ''// until they form a complete char.
        Dim chars(bytes.Length) As Char
        Dim charCount as Integer = _decoder.GetChars(bytes, offset, byteCount, chars, 0)
    
        For i as Integer = 0 to charCount - 1
            if chars(i) = chr(0) then           ''// The fix for bullet #2
                mObjText.Append(vbLf)
    
                Dim params() As Object = {mobjText.ToString}
                Me.Invoke(New DisplayInvoker(AddressOf Me.DisplayText), params)
    
                ''// You don't have to make a new StringBuilder, BTW -- just clear it.
                mObjText.Length = 0
            else
                mObjText.Append(chars(i))
            end if
        Next
    End Sub
    

    (BTW, the comments are started funny so that the syntax highlighting acts less stupid.)

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

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

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.