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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:07:01+00:00 2026-05-30T05:07:01+00:00

My client program gets stuck on NetworkStream.Read even though the server is reporting it’s

  • 0

My client program gets stuck on NetworkStream.Read even though the server is reporting it’s sending the message. The client and server are running on the same computer.

Full server code (console application)

Imports System.Net.Sockets
Imports System.Text

Module Module1
    Sub Main()
        'Dim address As New System.Net.IPAddress("127.0.0.1")
        'Dim serverSocket As New TcpListener(address, 8888)
        Dim serverSocket As New TcpListener(8888)
        Dim clientSocket As TcpClient
        Dim counter As Integer

        serverSocket.Start()
        msg("Server Started")
        counter = 0
        While (True)
            counter += 1
            clientSocket = serverSocket.AcceptTcpClient()
            msg("Client No:" + Convert.ToString(counter) + " started!")
            Dim client As New handleClient
            client.startClient(clientSocket, Convert.ToString(counter))
        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

    Public Class handleClient
        Dim clientSocket As TcpClient
        Dim clNo As String
        Public Sub startClient(ByVal inClientSocket As TcpClient, ByVal clineNo As String)
            Me.clientSocket = inClientSocket
            Me.clNo = clineNo
            Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat)
            ctThread.Start()
        End Sub
        Private Sub doChat()
            Dim requestCount As Integer
            Dim bytesFrom(10024) As Byte
            Dim dataFromClient As String
            Dim sendBytes As [Byte]()
            Dim serverResponse As String
            Dim rCount As String
            requestCount = 0

            While (True)
                Try
                    requestCount = requestCount + 1
                    Dim networkStream As NetworkStream = clientSocket.GetStream()
                    networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
                    dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
                    dataFromClient =  dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
                    msg("From client-" + clNo + ": " + dataFromClient)
                    rCount = Convert.ToString(requestCount)
                    serverResponse = "Server to client(" + clNo + ") " + rCount
                    sendBytes = 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

        End Sub
    End Class

End Module

Full client code (Forms Application)

Public Class CSocketClient
    Dim clientSocket As New System.Net.Sockets.TcpClient()
    Dim serverStream As NetworkStream

    'Connect
    Public Sub init(ByVal address As String, ByVal port As Integer)
        clientSocket.Connect(address, port)
    End Sub

    'Run once
    Public Sub runOnce()
        sendMSG("Message1 from Client$")
        receiveMSG()
        sendMSG("Message2 from Client$")
        receiveMSG()
    End Sub

    'Send msg
    Public Sub sendMSG(ByVal msg As String)
        Dim serverStream As NetworkStream = clientSocket.GetStream()
        Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes(msg)
        serverStream.Write(outStream, 0, outStream.Length)
        serverStream.Flush()
    End Sub

    'Receive msg
    Public Sub receiveMSG()
        Dim inStream(10024) As Byte
        Dim buffSize As Integer = clientSocket.ReceiveBufferSize
        MsgBox("X")
        serverStream.Read(inStream, 0, buffSize)
        MsgBox("Y")
        Dim returndata As String = System.Text.Encoding.ASCII.GetString(inStream)
        MsgBox("Data from Server : " & returndata)
    End Sub
End Class

To use the client:

Dim client As New CSocketClient
client.init("127.0.0.1", 8888)
client.runOnce()

I added MsgBox("X") and MsgBox("Y") to verify it doesn’t get past serverStream.Read. “X” pops up just fine, it never reaches Y.

Can anyone tell why this is happening?

  • 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-30T05:07:02+00:00Added an answer on May 30, 2026 at 5:07 am

    Fixed it. Send and receive need to share the same NetworkStream.. silly mistake!

    Public Sub runOnce()
        Dim serverStream As NetworkStream = clientSocket.GetStream()
        sendMSG("Message1 from Client$", serverStream)
        receiveMSG(serverStream)
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A client is running my company's program and it is halting before it gets
I am interacting with a web server using a desktop client program in C#
I've made a simple dummy server/dummy client program using IOCP for some testing/profiling purpose.
So I am coding this client/server program. This code is from the client side.
I am writing a simple C# tcp client and server program. The server will
I am receiving a struct from a server app and when the client program
I'm designing a server program in C++ to receive multiple client connections and pass
I have a program running on a server communicating with another program running on
I'm writing a program that basically perform server-client relationship. When i run my client
I have client code which gets a response from the server using UDP and

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.