troubleshooting Question

OnDataArrival in VB 2008 / 2005 / dot net

Avatar of DrDamnit
DrDamnitFlag for United States of America asked on
Visual Basic.NET
1 Comment1 Solution845 ViewsLast Modified:
In VB6, you could use CSocket, Winsock and a few other ways to make TCP connections to remote servers. When the remote server sent information to you, an event, "OnDataArrival" would fire at which point you could read the data in from the buffer, process it, and execute other code.

How do you do this in VB 2008 using stream objects? I built a simple client that connects to a remote server, and can successfully send my login information (and the server accepts it), but I don't know how to recieve data.

thanks in advance!

oh... and my code for the remote connection is based on the code snippet below from MSDN.
Shared Sub Connect(server As [String], message As [String])
      ' Create a TcpClient.
      ' Note, for this client to work you need to have a TcpServer 
      ' connected to the same address as specified by the server, port
      ' combination.
      Dim port As Int32 = 13000
      Dim client As New TcpClient(server, port)
      ' Translate the passed message into ASCII and store it as a Byte array.
      Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
      ' Get a client stream for reading and writing.
      '  Stream stream = client.GetStream();
      Dim stream As NetworkStream = client.GetStream()
      ' Send the message to the connected TcpServer. 
      stream.Write(data, 0, data.Length)
      Console.WriteLine("Sent: {0}", message)
      ' Receive the TcpServer.response.
      ' Buffer to store the response bytes.
      data = New [Byte](256) {}
      ' String to store the response ASCII representation.
      Dim responseData As [String] = [String].Empty
      ' Read the first batch of the TcpServer response bytes.
      Dim bytes As Int32 = stream.Read(data, 0, data.Length)
      responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
      Console.WriteLine("Received: {0}", responseData)
      ' Close everything.
   Catch e As ArgumentNullException
      Console.WriteLine("ArgumentNullException: {0}", e)
   Catch e As SocketException
      Console.WriteLine("SocketException: {0}", e)
   End Try
   Console.WriteLine(ControlChars.Cr + " Press Enter to continue...")
End Sub 'Connect
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros