Question how to ensure about sending multiple serial data from server to client

pooya1072

Well-known member
Joined
Jul 5, 2012
Messages
84
Programming Experience
Beginner
My program is divided into two parts , a server and a client. when the client connects to the server , At this time, the server should send numbers 1 to 10 to the client. but only one or two first number is send.

this is the variables :
Variables:
Dim pClient As TcpClient
Dim Listener As TcpListener
Dim mre As New Threading.ManualResetEvent(False)

this is server side : in the AcceptClient sub i create a for loop to send numbers 1 to 10 to client. (at line number 12 - 14)
Server side code:
Sub Main()
    mre.Reset()
    Listener = New TcpListener(IPAddress.Any, 6000)
    Listener.Start()
    Listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf AcceptClient), Listener)
    mre.WaitOne()
End Sub

Sub AcceptClient(ByVal ar As IAsyncResult)
    pClient = Listener.EndAcceptTcpClient(ar)
    Listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf AcceptClient), Listener)
    For i = 1 To 10
        Send(i)
    Next
End Sub

Public Sub Send(ByVal Messsage As String)
    Dim sendMessage As StreamWriter = New StreamWriter(pClient.GetStream)
    sendMessage.WriteLine(Messsage)
    sendMessage.Flush()
End Sub

and this is client side :
Client side code:
Sub Main()
    mre.Reset()
    Try
        client = New TcpClient("localhost", 6000)
        client.GetStream.BeginRead(New Byte() {0}, 0, 0, New AsyncCallback(AddressOf read), client.GetStream)
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    mre.WaitOne()
End Sub

Sub read(ByVal ar As IAsyncResult)
    Try
        Dim ns As NetworkStream = ar.AsyncState
        Dim l As Int16 = ns.EndRead(ar)
        Dim msg As String = New StreamReader(ns).ReadLine
        Console.WriteLine(msg)
        ns.BeginRead(New Byte() {0}, 0, 0, New AsyncCallback(AddressOf read), ns)
    Catch ex As Exception
        MsgBox(ex.Message)
        Exit Sub
    End Try
End Sub

i want to know how can i correct this code to send all of these numbers?
 
For BeginRead you need a buffer with some size and request to read a number of bytes greater than 0 (and limited by buffer size).
For read callback you need two items, the NetworkStream to call EndRead and the byte buffer that was filled with data.

For example you could have this variable available to both methods:
VB.NET:
Dim buffer(1023) As Byte
BeginRead uses it:
VB.NET:
Client.GetStream.BeginRead(buffer, 0, buffer.Length, New AsyncCallback(AddressOf read), Client.GetStream)
After EndRead data is available (l number of bytes as returned)
VB.NET:
Dim msg As String = Text.Encoding.ASCII.GetString(buffer, 0, l)
You can also replace BeginRead/EndRead with an Async method like this:
VB.NET:
Async Sub Read2()
    Using reader As New StreamReader(Client.GetStream, Text.Encoding.ASCII, False, 1024, True)
        Do
            Dim msg = Await reader.ReadLineAsync()
            If msg Is Nothing Then Exit Do
            Console.WriteLine(msg)
        Loop
    End Using
End Sub
 
Back
Top