Question Async example

tambrosi

New member
Joined
Oct 31, 2011
Messages
1
Programming Experience
Beginner
Hi All, I am new to this vb .net stuff and working on getting an async socket program to work. Could somebody tell me if this is the correct approach, or if somebody has an example. Could I see it.

All I want to do is make a connection and display the data that is coming thru the port.


Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic




Module MyTcpListener
Sub Main()
Dim server As TcpListener
server = Nothing
Try
' Set the TcpListener on port 13000.
Dim port As Int32 = 10001
Dim localAddr As IPAddress = IPAddress.Parse("10.13.99.90")


server = New TcpListener(localAddr, port)


' Start listening for client requests.
server.Start()


' Buffer for reading data
Dim bytes(1024) As Byte
Dim data As String = Nothing


' Enter the listening loop.
While True
Console.Write("Waiting for a connection... ")


' Perform a blocking call to accept requests.
' You could also user server.AcceptSocket() here.
Dim client As TcpClient = server.AcceptTcpClient()
Console.WriteLine("Connected!")


data = Nothing


' Get a stream object for reading and writing
Dim stream As NetworkStream = client.GetStream()


Dim i As Int32


' Loop to receive all the data sent by the client.
i = stream.Read(bytes, 0, bytes.Length)
While (i <> 0)
' Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
Console.WriteLine("Received: {0}", data)


' Process the data sent by the client.
data = data.ToUpper()
Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)


' Send back a response.
stream.Write(msg, 0, msg.Length)
Console.WriteLine("Sent: {0}", data)


i = stream.Read(bytes, 0, bytes.Length)


End While


' Shutdown and end connection
client.Close()
End While
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
Finally
server.Stop()
End Try


Console.WriteLine(ControlChars.Cr + "Hit enter to continue....")
Console.Read()
End Sub 'Main
 
Async means asynchronous, which involves working with multiple threads. VB.Net has various multi-threading functionality that you can read up on and try out without involving sockets in the mix. Understanding what it is and how to operate it will help you manage all situations that require asynchronous functionality.

As for the socket classes they also have built in asynchronous functionality, for example TcpListener has the synchronous AcceptTcpClient and the asynchronous BeginAcceptSocket/EndAcceptSocket method pair. The Begin-/End- method pairs is something many classes have, and you operate them by calling the Begin- method to start the operation and when your callback method is invoked you use the End- method to complete the operation. This is comparable to the general Delegate.BeginInvoke/EndInvoke method pairs. A variation of async functionality can be found for example in the WebClient socket class, where you have the synchronous DownloadFile method, while the asynchronous DownloadFileAsync method is paired with an DownloadFileCompleted event. The event handler is a callback delegate just like the callback you assign to the Begin- operation, but events is the common approach in UI programming and since WebClient is a component it uses events.

So my recommandation is, (1) get to know multi-threading in general, (2) look up the documentation for TcpListener/TcpClient classes Begin-/End- methods, apart from explanations you will also find code examples there.
 
Back
Top