Advertisement
Guest User

Async Server

a guest
Oct 16th, 2011
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.26 KB | None | 0 0
  1. Public Class Client
  2.  
  3.     Private client As TcpClient
  4.     Private clientPacket As String
  5.  
  6.     Public Sub New(ByVal client As TcpClient)
  7.         'New client connects
  8.         Me.client = client
  9.         client.GetStream().BeginRead(New Byte() {0}, 0, 0, AddressOf Read, Nothing)
  10.     End Sub
  11.  
  12.     Public Sub Read(ByVal ar As IAsyncResult)
  13.         Dim reader As New StreamReader(client.GetStream())
  14.         clientPacket &= reader.ReadLine()
  15.         client.GetStream().BeginRead(New Byte() {0}, 0, 0, AddressOf Read, Nothing)
  16.     End Sub
  17.  
  18.     Public 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. End Class
  25.  
  26.  
  27. Public Class frmMain
  28.  
  29.     Dim Listener As TcpListener
  30.     Dim ListenerThread As Thread
  31.  
  32.     Private Sub StartListening()
  33.         ListenerThread = New Thread(AddressOf Listen)
  34.         ListenerThread.IsBackground = True
  35.         ListenerThread.Name = "listener"
  36.         ListenerThread.Start()
  37.     End Sub
  38.  
  39.     Public Sub Listen()
  40.         Listener = New TcpListener(IPAddress.Any, 7008)
  41.         Listener.Start()
  42.         While (True)
  43.             Dim c As New Client(Listener.AcceptTcpClient())
  44.         End While
  45.     End Sub
  46.  
  47. End Class
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement