Imports System.Net, System.Net.Sockets
Imports System.Text

Public Class cClient
    Dim clientSocket As Socket
    Dim byteData(1023) As Byte

    ' Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    '     begin()
    ' End Sub


    Public Sub begin()
        clientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        Dim ipAddress As IPAddress = ipAddress.Parse("192.168.0.90")
        Dim ipEndPoint As IPEndPoint = New IPEndPoint(ipAddress, 8800)
        clientSocket.BeginConnect(ipEndPoint, New AsyncCallback(AddressOf OnConnect), Nothing)

        '   endconnect(clientSocket)
    End Sub
    Private Sub OnConnect(ByVal ar As IAsyncResult)
        Try
            clientSocket.EndConnect(ar)
            clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, _
                                      New AsyncCallback(AddressOf OnRecieve), clientSocket)
        Catch ex As Exception
            '  MsgBox("Verbindung zum Server konnte nicht aufgebaut werden!")
        End Try
    End Sub

    Private Sub OnSend(ByVal ar As IAsyncResult)
        Dim client As Socket = ar.AsyncState
        client.EndSend(ar)
    End Sub
    Private Sub Send(ByVal msg As String, ByVal client As Socket)
        Dim sendBytes As Byte() = Encoding.BigEndianUnicode.GetBytes(msg)
        client.BeginSend(sendBytes, 0, sendBytes.Length, SocketFlags.None, New AsyncCallback(AddressOf OnSend), client)
    End Sub
    Public Function ByteArrayToTextString(ByRef Barr() As Byte) As String
        Dim enc As System.Text.Encoding = Encoding.BigEndianUnicode
        Return enc.GetString(Barr)
    End Function

    Private Sub OnRecieve(ByVal ar As IAsyncResult)
        Try
            Dim client As Socket = ar.AsyncState
            client.EndReceive(ar)
            Dim bytesRec As Byte() = byteData
            Dim message As String = Encoding.BigEndianUnicode.GetString(bytesRec)
            '  MsgBox("FROM SERVER: " & message)
            Dim b(1023) As Byte

            ' MsgBox("SERVER RECIEVE: " & Encoding.BigEndianUnicode.GetString(b))

            byteData = b
            Read(message)
            clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, _
                                      New AsyncCallback(AddressOf OnRecieve), clientSocket)

        Catch ex As Exception
            ' MsgBox("Verbindung zum Server wurde unterbrochen!")
        End Try
    End Sub

    Delegate Sub _Read(ByVal msg As String)
    Private Sub Read(ByVal msg As String)
        If frmMain.InvokeRequired Then
            frmMain.Invoke(New _Read(AddressOf Read), msg)
            Exit Sub
        End If
        frmMain.icoVERAGMonitoring.Visible = False
        'if richtige nsachricht
        '  Dim asciis As Byte() = Encoding.BigEndianUnicode.GetBytes(msg)
        '  Dim a As Char = msg(msg.Length - 1)
        ' msg = msg.Replace(a, "")
        ' MsgBox(msg.Length)

        ' MsgBox(msg(msg.Length - 1))
        ' MsgBox(msg(msg.Length - 1))
        '      MsgBox(Asc(msg(msg.Length - 1)))
        ' MsgBox(Asc(msg(msg.Length - 2)))
        msg = msg.Replace(Convert.ToChar(0), "")
        Try
            ' MsgBox(msg)
            '  MsgBox("-" & msg & "-" & vbNewLine & "ConSuccess" & "-")
            If msg = "ConSuccess" Then
                '  MsgBox("!!!!!ConSuccess --> initAufschubkonten")
                Send("initAufschubkonten", clientSocket)
            ElseIf msg = "Task successful" Then
                clientSocket.Disconnect(False)
                clientSocket.Shutdown(SocketShutdown.Both)
                clientSocket.Close()
                'endconnect(clientSocket)'geht ned
                MsgBox("Einlesen erfolgreich!", vbSystemModal, "Aufschubkonten")
            ElseIf msg = "Task not found" Then
                MsgBox("SERVERERROR: Der Dienst wurde nicht gefunden!", vbSystemModal, "Aufschubkonten")
            ElseIf msg = "Task inactive" Then
                MsgBox("SERVERERROR: Der Dienste ist nicht aktiv!", vbSystemModal, "Aufschubkonten")
            ElseIf msg = "Task timeout" Then
                MsgBox("SERVERERROR: Der Dienst wurde wegen einer Zeitüberschreitung abgebrochen!", vbSystemModal, "Aufschubkonten")
            Else
                MsgBox("SERVER_NACHRICHT NICHT VERSTANDEN: " & msg, vbSystemModal)

                clientSocket.Shutdown(SocketShutdown.Both)
                clientSocket.Close()
            End If

        Catch ex As Exception
            MsgBox("Verbindungs-Fehler beim Senden", vbSystemModal)
            clientSocket.Close()
        End Try
        ' byteData = Encoding.BigEndianUnicode.GetBytes("")

    End Sub

   
End Class