0

I'm connecting to a specific TCP port of a server, do stuff, and I need to disconnect at some point, then wait for the server to open its port again and reconnect.

My program loops forever.

To disconnect from the server, I sent from the server the string "TCP:OFF" and the close the port (server side). Whenever my program reads this line, I want it to close its connection and wait the server to open its port again. This is because of the TCP/IP protocol.

The problem is that the specific ASCII string sent from my server is not read until I put my mouse over my window (weird). At the end, I want my program to work by itself and minimized, without human intervention.

There must be a problem in the timing of my networkStream reading.

Code

While tcpClient.Connected() = False
        Try
            tcpClient.ReceiveTimeout = 1000
            tcpClient.SendTimeout = 1000
            tcpClient.Connect("192.168.10.100", 10003)
            Console.WriteLine("Connection OK")
            System.Threading.Thread.Sleep(1000)
        Catch ex As Exception
            Console.WriteLine("ERRORR : Server unreachable")
            System.Threading.Thread.Sleep(1000)
        End Try
    End While

Dim networkStream As NetworkStream = tcpClient.GetStream()

If networkStream.CanRead And networkStream.DataAvailable Then
        Try
            ReadData = ""
            Dim myReadBuffer As Byte() = New Byte(7) {}
            Dim numberOfBytesRead = networkStream.Read(myReadBuffer, 0, myReadBuffer.Length)
            ReadData = Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead)
            Console.Write(ReadData & vbCrLf)
        Catch ex As Exception
            Console.Write("Error reading data")
        End Try
    End If

    If ReadData = "TCP:OFF" Then
        tcpClient.Close()
        ReadData = ""
        Console.WriteLine("Closing connection")
        System.Threading.Thread.Sleep(5000)
        tcpClient = New TcpClient
    End If

I'm doing a Windows form program in latest Visual Studio.

Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
  • 1
    I guess you are running this code on UI thread. Try running it in separate thread. – Manpreet Singh Dhillon Jun 08 '18 at 14:06
  • Sorry but i'm not that familiar with multiples threads, I forgot to tell i'm quite a Newbie to coding... For now, my code runs in this sub : `Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)` So that means I have to separate the network part of my code and the rest? – Alexis Robin Jun 08 '18 at 14:26
  • @AlexisRobin yes. If you run some sleep or wait for a function to come back, the UI will freeze unless you put that logic in a thread. – the_lotus Jun 08 '18 at 14:44
  • Why on earth are you running it in `WndProc`??? That's definitely not the right place to have it. At the very least you need to create your own separate method (= sub) for it. What you really need to do, though, is to run it in a separate thread. See [this](https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/concepts/threading/multithreaded-applications) and [this](https://stackoverflow.com/a/45571728/3740093) for more information. – Visual Vincent Jun 08 '18 at 14:51
  • Ok thanks for the advices guys! I'll read it this weekend to understand what I do^^ So i'm gonna put all my code in a separate thread, because i don't do anything with my UI (just a TxtBox with a freezed message) – Alexis Robin Jun 08 '18 at 15:18
  • As an update, I re-wrote all the code in a Console-like program, all is isolated within functions and it's all fine! – Alexis Robin Jun 11 '18 at 08:05

0 Answers0