Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

VS 2010 Concverting Console program to Form Program

$
0
0
I have a console program listed below that is working perfectly, taking in 2 different sets of data over ethernet and continually updating and displaying the data stream. The problem is, in my main program im using a form based setup and im having a hard time converting the code over to work in the form based environment.

Console App Code:
Code:

Imports System.Net.Sockets
Imports System.Text

Module Module1

    Sub Main()


        Dim tcpClient As New System.Net.Sockets.TcpClient()
        tcpClient.Connect("10.200.158.31", 14142)
        Dim networkStream As NetworkStream = tcpClient.GetStream()

            If networkStream.CanWrite And networkStream.CanRead Then
                ' Do a simple write.
                Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there")
                networkStream.Write(sendBytes, 0, sendBytes.Length)

            Do
                ' Read the NetworkStream into a byte buffer.
                Dim bytes(13) As Byte
                networkStream.Read(bytes, 0, CInt(13))

                ' Output the data received from the host to the console.
                Dim returndata As String = Encoding.ASCII.GetString(bytes)
                Console.WriteLine((returndata))
            Loop

        Else
            If Not networkStream.CanRead Then
                Console.WriteLine("cannot not write data to this stream")
                tcpClient.Close()
            Else
                If Not networkStream.CanWrite Then
                    Console.WriteLine("cannot read data from this stream")
                    tcpClient.Close()
                End If
            End If
        End If

        ' pause so user can view the console output
        Console.ReadLine()



    End Sub

End Module

Current Form App Code:
Code:

Imports System.Net.Sockets
Imports System.Text

Class TCPCli

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim tcpClient As New System.Net.Sockets.TcpClient()
        tcpClient.Connect("10.200.158.31", 14142)
        Dim networkStream As NetworkStream = tcpClient.GetStream()

        If networkStream.CanWrite And networkStream.CanRead Then

            ' Do a simple write.
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there")
            networkStream.Write(sendBytes, 0, sendBytes.Length)

            Dim bytes(20) As Byte

            Do
                ' Read the NetworkStream into a byte buffer.
                networkStream.Read(bytes, 0, CInt(20))
                ' Output the data received from the host to the console.
                Dim returndata As String = Encoding.ASCII.GetString(bytes)
                Console.WriteLine((returndata))
                Label1.Text = Convert.ToString(returndata)

            Loop
        End If
    End Sub
End Class

Im seeing some data printed to the output screen but not all of the data is there (its only printing at less then half the rate of the console, and im missing the second data set alltogether).

Also im quite new to VB, and having a tough time trying to figure out how to initiate the procedure above without using a button click event, because when i click the button, the window freezes as its running a continuous neverending loop.

Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>