none
Socket Network Stream Read Freezes/Crashes Project RRS feed

  • Question

  • I'm trying to use a third-party, socket based API and when I try to read the what is been returned from the server after I send a request, my project freezes. I'm able to connect, send a request, and close the connection without error. It only has problems when trying to read what the server sends back.

    I even tried creating my own basic server application/project and tried to use my client side code against my basic app and it works as expected. It just doesn't work when I try that same client project against the third-party API.

    Any ideas what could be causing this? 

    Here is my code:

    Imports System.Net.Sockets
    Imports System.Text
    
    Public Class Form1
        Dim VarSocketServer As New TcpClient
        Dim VarNetworkStream As NetworkStream
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            VarSocketServer.Connect("127.0.0.1", 28192)
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim VarNetworkStream As NetworkStream = VarSocketServer.GetStream()
    
            ' Send a msessage/request to the server. This doesn't return errors and appears to work.
            Dim VarClientRequest As String = "{My request is sent in JSON and includes credentials and key}"
            Dim VarSendByte As [Byte]() = System.Text.Encoding.ASCII.GetBytes(VarClientRequest)
            VarNetworkStream.Write(VarSendByte, 0, VarSendByte.Length)
            VarNetworkStream.Flush()
    
            ' Read the response from the server. This freezes at "VarNetworkStream.Read"
            Dim VarInStream(10024) As Byte
            Dim VarBytesRead = VarNetworkStream.Read(VarInStream, 0, VarInStream.Length)
            Dim VarDataFromServer As String = System.Text.Encoding.ASCII.GetString(VarInStream)
            VarNetworkStream.Flush()
    
            Console.WriteLine(">> Data from server: " + VarDataFromServer)
    	Console.ReadLine()
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            VarSocketServer.Close()
            Me.Close()
        End Sub
    End Class


    • Edited by Ptownbro Sunday, August 18, 2019 8:31 PM
    Sunday, August 18, 2019 8:31 PM

All replies

  • Hello,

    Try running Fiddler to see if there are errors or set a breakpoint before the line of code that stops the program and using the Local window inspect on the Read method.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, August 18, 2019 11:27 PM
    Moderator