VB.Net/Network Remote/TCP Client

Материал из VB Эксперт
Перейти к: навигация, поиск

TCP Client Connection

Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports System.Threading

Public Class MainClass
   Shared Stream As NetworkStream

   Public Shared Sub Main()
        System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1))
        Dim Client As New TcpClient()
        Try
            Client.Connect(IPAddress.Parse("127.0.0.1"), 11000)
            Console.WriteLine("TCP Client Connection established.")
            Console.WriteLine(New String("-", 40))
            Console.WriteLine()
            Stream = Client.GetStream()
            Dim ReceiveThread As New Thread(AddressOf ReceiveData)
            ReceiveThread.IsBackground = True
            ReceiveThread.Start()
            Dim w As New BinaryWriter(Stream)
            Dim Text As String
            Do
                Text = Console.ReadLine()
                If Text <> "QUIT" Then w.Write(Text)
            Loop Until Text.ToUpper() = "QUIT"
            Client.Close()
        Catch Err As Exception
            Console.WriteLine(Err.ToString())
        End Try
   End Sub
   
   Shared  Private Sub ReceiveData()
        Dim r As New BinaryReader(Stream)
        Do
            If Stream.DataAvailable Then
                Console.WriteLine(("RECEIVED: " + r.ReadString()))
            End If
        Loop
    End Sub
   
End Class