Advertisement
Guest User

Async Client

a guest
Oct 16th, 2011
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.13 KB | None | 0 0
  1. Module Client
  2.  
  3.     Dim host As String = "127.0.0.1"
  4.     Dim port As Integer = 7008
  5.     Dim client As TcpClient
  6.  
  7.     Sub Main()
  8.         Dim t As New Threading.Thread(AddressOf KeepOpen)
  9.         t.Name = "Keep Open"
  10.         t.Start()
  11.         Connect()
  12.     End Sub
  13.  
  14.     Sub KeepOpen()
  15.         Console.ReadLine()
  16.     End Sub
  17.  
  18.     Sub Send(ByVal message As String)
  19.          Dim writer As New StreamWriter(client.GetStream())
  20.          writer.WriteLine(message)
  21.          writer.Flush()
  22.     End Sub
  23.  
  24.     Sub Read(ByVal ar As IAsyncResult)
  25.         Dim reader As New StreamReader(client.GetStream())
  26.         Dim unparsed = reader.ReadLine()
  27.         client.GetStream().BeginRead(New Byte() {0}, 0, 0, AddressOf Read, Nothing)
  28.     End Sub
  29.  
  30.     Sub Connect()
  31.         Console.Write("Connecting to " & host & ":" & port & Environment.NewLine)
  32.         client = New TcpClient(host, port)
  33.         Console.Write("Connected" & Environment.NewLine)
  34.         For i = 0 To 100
  35.             Send(i & ",")
  36.         Next i
  37.         Send("g")
  38.         client.GetStream().BeginRead(New Byte() {0}, 0, 0, AddressOf Read, Nothing)
  39.     End Sub
  40.  
  41. End Module
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement