0

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 :

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.

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 :

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?

  • 1
    Using a `StreamReader` with an asynchronous connection like this isn't good as it's easy to desynchronize the receiving of the data, and it might also cause your UI to freeze while waiting for _more_ data (in cases where you don't receive a whole line immediately). TCP is a stream-based protocol, meaning the application layer has no notion of where data begins/ends unless you implement that yourself. – Visual Vincent Apr 24 '19 at 16:33
  • 1
    Likely, the reason it doesn't print all numbers is that it has already received all lines, but you're only reading one of them in the callback before telling it to wait for more data. In order to implement a reliable message/packet system you need to treat the data as what it is - raw bytes - and only convert it to strings when you know you've received everything you need. – Visual Vincent Apr 24 '19 at 16:39
  • 1
    I have briefly described a simple way of doing so in [this answer of mine](https://stackoverflow.com/a/37352525). The process works by prefixing each "packet" with the exact number of bytes that you've sent, as well as including a single-byte header which helps you differentiate between your different types of data. – Visual Vincent Apr 24 '19 at 16:41

0 Answers0