vb.net console

Status
Niet open voor verdere reacties.

sunion

Gebruiker
Lid geworden
24 aug 2008
Berichten
278
Ik heb een simpele vraag maar vind nergens een antwoord :p

ik heb een server/client gemaakt en als de client iets naar de server stuurt gaat de console van de client en de console van de server helemaal in het midden staan.
weet iemand hoe dit komt?
of hoe je kunt programeren dat ie vanzelf trug naar bove gaat?

fout.jpg
 
server
Code:
Imports System.Net.Sockets
Imports System.Text

Class TCPSrv
    Shared Sub Main()
        '----------------START LISTENING----------------------------------
        Const portNumber As Integer = 8000
        Dim tcpListener As New TcpListener(portNumber)
        tcpListener.Start()
        Console.WriteLine("<NO CONNECTION>")
        '---------------ACCEPT CONNECTION-------------------------------
1:
        Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
        Console.Clear()
        Console.WriteLine("<CONNECTION ESTABLISHED>")
        Dim networkStream As NetworkStream = tcpClient.GetStream()
        Dim bytes(tcpClient.ReceiveBufferSize) As Byte
        networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
        Dim clientdata As String = Encoding.ASCII.GetString(bytes)
        Console.WriteLine("Incomming command: " & clientdata)
        If clientdata.Contains("hello") Then
            Console.WriteLine(clientdata & "recognized, sending LOGIN page!")
            Dim responseString As String = "             ___       ______ " & vbNewLine & "            / _ \      | ___ \" & vbNewLine & "           / /_\ \ ___ | |_/ /" & vbNewLine & "           |  _  |/ _ \| ___ \" & vbNewLine & "           | | | | (_) | |_/ /" & vbNewLine & "      *****\_| |_/\___/\____/*****  " & vbNewLine & "     *____________________________*" & vbNewLine & "     |~~~~~~~ WELCOME TO ~~~~~~~~~|" & vbNewLine & "     |~~~~~~ DARK ARRAYS ~~~~~~~~~|" & vbNewLine & "     |~~~~~~ COMMAND-LINE ~~~~~~~~|" & vbNewLine & "     |~~~~~~~~~ CENTER ~~~~~~~~~~~|" & vbNewLine & "     |____________________________|" & vbNewLine & "_____________________________________________" & vbNewLine & vbNewLine & vbNewLine
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
            networkStream.Write(sendBytes, 0, sendBytes.Length)

            Console.ReadLine()
            GoTo 1

        Else
            Dim responseString As String = "Who are you?"
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            GoTo 1
        End If
    End Sub
    '----------------------AUTHENTICATION------------------------
    Shared Sub pass()
        Try
            Dim tcpClient As New System.Net.Sockets.TcpClient()

            Dim networkStream As NetworkStream = tcpClient.GetStream()
            If networkStream.CanWrite And networkStream.CanRead Then

                Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Please enter a password: ")
                networkStream.Write(sendBytes, 0, sendBytes.Length)
                Dim bytes(tcpClient.ReceiveBufferSize) As Byte
                networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
                ' Output the data received from the client to the console.
                Dim returndata As String = Encoding.ASCII.GetString(bytes)
                Console.WriteLine(("Host returned: " + returndata))
            End If
        Catch ex As Exception
            MsgBox("FAILED TO SEND REQUEST")
        End Try

        Console.ReadLine()

    End Sub
End Class


client
Code:
Imports System.Net.Sockets
Imports System.Text
Class TCPCli
    Shared Sub Main()
        Dim tcpClient As New System.Net.Sockets.TcpClient()
        tcpClient.Connect("127.0.0.1", 8000)
        Dim networkStream As NetworkStream = tcpClient.GetStream()
1:
        Console.Clear()
        If networkStream.CanWrite And networkStream.CanRead Then
            ' Do a simple write.
            Dim hello As String
            hello = Console.ReadLine
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(hello)
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            ' Read the NetworkStream into a byte buffer.
            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
            ' Output the data received from the host to the console.
            Dim returndata As String = Encoding.ASCII.GetString(bytes)
            Console.WriteLine(returndata)
            Console.ReadLine()
            GoTo 1
        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 Class
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan