Click here to Skip to main content
15,885,899 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

Here I have a client whose code is as below. This code is connects to server and runs perfectly when server is up. Issue is if server is down then it gives
System.StackOverFlowException

after 100 loops. However, if server is up and gets disconnects in between RemoteConnect loop works till max counter.


VB
'Globally Defined 
 Dim lTCPClientRemote As New TcpClient
 Dim lServerIp As String
 Dim lServerPort As String
 Dim lNetworkStreamRemote As System.Net.Sockets.NetworkStream
 Dim lRemoteWriteStream As System.IO.StreamWriter
 Dim lRemoteReadStream As System.IO.StreamReader
 Dim lRemoteReceiverThread As Threading.Thread = Nothing
 Dim lconnecttries As Integer = 0


 Private Sub frm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    Dim lRemoteThread As New System.Threading.Thread(AddressOf RemoteConnect)
    lRemoteThread.IsBackground = True
    lRemoteThread.Start()
           
 End Sub

 Private Sub RemoteConnect()
        Try
            lTCPClientRemote = New TcpClient()
            lServerIp = "127.0.0.1"
            lServerPort = "1000"
            lTCPClientRemote.Connect(lServerIp, lServerPort)'System.StackOverFlowException is here after 100 loops

           If lTCPClientRemote.Connected() Then
             
                lconnecttries = 0
                lNetworkStreamRemote = lTCPClientRemote.GetStream()
                lRemoteReadStream = New System.IO.StreamReader(lNetworkStreamRemote)
                lRemoteWriteStream = New System.IO.StreamWriter(lNetworkStreamRemote)
                lRemoteReceiverThread = New Threading.Thread(AddressOf ProcessMsg)
                lRemoteReceiverThread.Start()
           Else
               'Function to display remote connection status
                UpdateStatusMess("Unable to Connect Remote Machine", "2")
           End If
       Catch Ex As SocketException
            'Function to display remote connection status
            UpdateStatusMess("Unable to Connect Remote Machine", "2")
            
            lconnecttries  = lconnecttries  + 1

            If lconnecttries  <= 1000 Then
                Threading.Thread.Sleep(5000) ' 5 second
                RemoteConnect()
            Else
                Exit Sub
            End If
        End Try
    End Sub


Kindly help and suggest if any changes need to done.

What I have tried:

I have tried to dispose
lTCPClientRemote.Dispose()
in exception, but it did not work.
Posted
Updated 27-Jul-21 3:00am

You have a recursive call in your code:
VB
If lconnecttries  <= 1000 Then
    Threading.Thread.Sleep(5000) ' 5 second
    RemoteConnect()

Which will likely lead to a stack overflow error.
 
Share this answer
 
Comments
Magic Wonder 27-Jul-21 9:01am    
Yes, I have to call it recursively up to 1000 times. And same is working when it disconnects in between.
Richard MacCutchan 27-Jul-21 9:22am    
No, you do not need to call it recursively; you should use a properly controlled loop.
Magic Wonder 30-Jul-21 0:45am    
Thank you for your suggestion.
Magic Wonder 30-Jul-21 0:46am    
Used controlled loop.
Quote:
System.StackOverFlowException is here after 100 loops
That isn't a loop: it's a recursive call.
So each time you call RemoteConnect it takes a bit more stack space - and it's not massive to start with - so at some point it will run out of stack and you get an overflow exception.

There is no good reason for that code to be recursive: a simple loop would be both more obvious and efficient.
And move local variables into the method they are used in - it's pointless having them at class level if they are only accessed or relevant in a single method!
 
Share this answer
 
Comments
Magic Wonder 27-Jul-21 9:08am    
1. Can we clear stack in exception?
2. variables kept class level as using them in other functions also on same connection for requests and responses.
OriginalGriff 27-Jul-21 9:34am    
1 No. The stack is a small area of memory that is part of the thread, and is used for local variables and method return addresses. You have no direct access to it, and if you did you would screw your app up royally if you tried to modify it!
2) Some, yes - if they are class related. But "retry counts" aren't, and most of the others should be passed through as method parameters or return values rather than assigned as "class level globals". That way your code is more structured, more flexible, easier to maintain, and less "interlocked".
Magic Wonder 30-Jul-21 0:45am    
Thank you for your valuable details.
Magic Wonder 27-Jul-21 10:00am    
1) ok
2) I will check and try to make use of proper loop instead of recursive call.
3) Retry count, here declared for understanding purpose, however same is also in use globally.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900