hey guyz,
i got this bunch of codes from msdn.. but its written for console applications...
how do you convert it so that it will run on a form?
its abit the long.. thx for your help

these are the codes

VB Code:
  1. Imports System
  2. Imports System.Net
  3. Imports System.Net.Sockets
  4. Imports System.Text
  5. Imports System.Threading
  6.  
  7.  
  8. ' State object for reading client data asynchronously
  9. Public Class StateObject
  10.     ' Client  socket.
  11.     Public workSocket As Socket = Nothing
  12.     ' Size of receive buffer.
  13.     Public Const BufferSize As Integer = 1024
  14.     ' Receive buffer.
  15.     Public buffer(BufferSize) As Byte
  16.     ' Received data string.
  17.     Public sb As New StringBuilder
  18. End Class 'StateObject
  19.  
  20. Public Class AsynchronousSocketListener
  21.  
  22.     ' Incoming data from the client.
  23.     Public Shared data As String = Nothing
  24.  
  25.     ' Thread signal.
  26.     Public Shared allDone As New ManualResetEvent(False)
  27.  
  28.  
  29.     Public Sub New()
  30.     End Sub 'New
  31.  
  32.  
  33.     Public Shared Sub StartListening()
  34.         ' Data buffer for incoming data.
  35.         Dim bytes() As Byte = New [Byte](1024) {}
  36.  
  37.         ' Establish the local endpoint for the socket.
  38.         ' The DNS name of the computer
  39.         ' running the listener is "host.contoso.com".
  40.         Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName())
  41.         Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
  42.         Dim localEndPoint As New IPEndPoint(ipAddress, 11000)
  43.  
  44.         ' Intializes a TCP/IP socket.
  45.         Dim listener As New Socket(AddressFamily.InterNetwork, _
  46.             SocketType.Stream, ProtocolType.Tcp)
  47.  
  48.         ' Bind the socket to the local endpoint and listen for incoming
  49.         ' connections.
  50.         Try
  51.             listener.Bind(localEndPoint)
  52.             listener.Listen(100)
  53.  
  54.             While True
  55.                 ' Set the event to nonsignaled state.
  56.                 allDone.Reset()
  57.  
  58.                 ' Start an asynchronous socket to listen for connections.
  59.                 Console.WriteLine("Waiting for a connection...")
  60.                 listener.BeginAccept(New AsyncCallback(AddressOf AcceptCallback), _
  61.                 listener)
  62.  
  63.                 ' Wait until a connection is made before continuing.
  64.                 allDone.WaitOne()
  65.             End While
  66.  
  67.         Catch e As Exception
  68.             Console.WriteLine(e.ToString())
  69.         End Try
  70.  
  71.         Console.WriteLine(ControlChars.Cr + "Press ENTER to continue...")
  72.         Console.Read()
  73.     End Sub 'StartListening
  74.  
  75.  
  76.     Public Shared Sub AcceptCallback(ByVal ar As IAsyncResult)
  77.         ' Signal the main thread to continue.
  78.         allDone.Set()
  79.  
  80.         ' Get the socket that handles the client request.
  81.         Dim listener As Socket = CType(ar.AsyncState, Socket)
  82.         Dim handler As Socket = listener.EndAccept(ar)
  83.  
  84.         ' Create the state object.
  85.         Dim state As New StateObject
  86.         state.workSocket = handler
  87.         handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, _
  88.             New AsyncCallback(AddressOf ReadCallback), state)
  89.     End Sub 'AcceptCallback
  90.  
  91.  
  92.     Public Shared Sub ReadCallback(ByVal ar As IAsyncResult)
  93.         Dim content As [String] = [String].Empty
  94.  
  95.         ' Retrieve the state object and the handler socket
  96.         ' from the asynchronous state object.
  97.         Dim state As StateObject = CType(ar.AsyncState, StateObject)
  98.         Dim handler As Socket = state.workSocket
  99.  
  100.         ' Read data from client socket.
  101.         Dim bytesRead As Integer = handler.EndReceive(ar)
  102.  
  103.         If bytesRead > 0 Then
  104.             ' There might be more data, so store the data received so far.
  105.             state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, _
  106.                 bytesRead))
  107.  
  108.             ' Check for end-of-file tag. If it is not there, read
  109.             ' more data.
  110.             content = state.sb.ToString()
  111.             If content.IndexOf("<EOF>") > -1 Then
  112.                 ' All the data has been read from the
  113.                 ' client. Display it on the console.
  114.                 Console.WriteLine("Read {0} bytes from socket. " + _
  115.                     ControlChars.Cr + " Data : {1}", content.Length, content)
  116.                 ' Echo the data back to the client.
  117.                 Send(handler, content)
  118.             Else
  119.                 ' Not all data received. Get more.
  120.                 handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, _
  121.                     0, New AsyncCallback(AddressOf ReadCallback), state)
  122.             End If
  123.         End If
  124.     End Sub 'ReadCallback
  125.  
  126.  
  127.     Private Shared Sub Send(ByVal handler As Socket, ByVal data As [String])
  128.         ' Convert the string data to byte data using ASCII encoding.
  129.         Dim byteData As Byte() = Encoding.ASCII.GetBytes(data)
  130.  
  131.         ' Begin sending the data to the remote device.
  132.         handler.BeginSend(byteData, 0, byteData.Length, 0, _
  133.             New AsyncCallback(AddressOf SendCallback), handler)
  134.     End Sub 'Send
  135.  
  136.  
  137.     Private Shared Sub SendCallback(ByVal ar As IAsyncResult)
  138.         Try
  139.             ' Retrieve the socket from the state object.
  140.             Dim handler As Socket = CType(ar.AsyncState, Socket)
  141.  
  142.             ' Complete sending the data to the remote device.
  143.             Dim bytesSent As Integer = handler.EndSend(ar)
  144.             Console.WriteLine("Sent {0} bytes to client.", bytesSent)
  145.  
  146.             handler.Shutdown(SocketShutdown.Both)
  147.             handler.Close()
  148.  
  149.         Catch e As Exception
  150.             Console.WriteLine(e.ToString())
  151.         End Try
  152.     End Sub 'SendCallback
  153.  
  154.     'Entry point that delegates to C-style main Private Function.
  155.     Public Overloads Shared Sub Main()
  156.         System.Environment.ExitCode = _
  157.         Main(System.Environment.GetCommandLineArgs())
  158.     End Sub
  159.  
  160.     Public Overloads Shared Function Main(ByVal args() As [String]) As Integer
  161.         StartListening()
  162.         Return 0
  163.     End Function 'Main
  164. End Class 'AsynchronousSocketListener