Imports System.Net                      'TcpClient
Imports System.Net.Sockets              'TcpClient
Imports System.Text                     'Encoding


Public Class Form1


    Dim netStream As NetworkStream


    Dim MsgBuf As String


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

        '*******************************************************************************
        ' LAN SAMPLE PROGRAM NO.1
        '
        ' This program connects to LR8400 and do the sending and receiving of a command.  
        ' When push a command execute button and input the command 
        ' that wants to transmit it to the column of a command it is transmitted.  
        ' The answer is displayed to a text box, in the case of the command that is included (? ).  
        '*******************************************************************************

        If InStr(TextBox3.Text, "?") = 0 Then                   '

            SendMsgCrLf(TextBox3.Text)                          'Send command
            TextBox1.Text = ""

        Else                                                    '

            GetMsgCrLf(TextBox3.Text)                           'Send command and receive query
            TextBox1.Text = MsgBuf

        End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim client As New TcpClient(TextIp.Text, Val(TextPort.Text))    'Set Tcp Client

        netStream = client.GetStream()                     'Open Tcp Client
        netStream.WriteTimeout = 100000
        netStream.ReadTimeout = 100000

        'Do While (Winsock1.State <> sckConnected)
        '    If Winsock1.State = sckConnecting Then Text2.Text = "connecting"
        '    DoEvents()
        'Loop
        'If Winsock1.State = sckConnected Then Text2.Text = "LAN connect"

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        netStream.Close()                                  'Close Tcp Client

        'Do While (Winsock1.State <> sckClosed)
        '    If Winsock1.State = sckConnected Then Text2.Text = "disconecting"
        '    DoEvents()
        'Loop
        'If Winsock1.State = sckClosed Then Text2.Text = "LAN disconnect"

    End Sub

    Private Sub SendMsgCrLf(ByVal strMsg As String)

        strMsg = strMsg & vbCrLf
        netStream.Write(Encoding.ASCII.GetBytes(strMsg), 0, strMsg.Length)            'send message

    End Sub

    Private Sub GetMsgCrLf(ByVal strMsg As String)
        Dim Check As Integer

        strMsg = strMsg & vbCrLf
        netStream.Write(Encoding.ASCII.GetBytes(strMsg), 0, strMsg.Length)            'send message

        MsgBuf = Nothing
        Do                                                    'wait receive command
            Check = netStream.ReadByte()

            If Chr(Check) = vbLf Then
                '                MsgBuf = MsgBuf & Chr(Check)
                Exit Do
            ElseIf Chr(Check) = vbCr Then
                '                MsgBuf = MsgBuf & Chr(Check)
            Else
                MsgBuf = MsgBuf & Chr(Check)
            End If
        Loop
        MsgBuf = MsgBuf & vbCrLf

    End Sub

End Class
