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

  • Thread starter Thread starter mahmoodi1072
  • Start date Start date
M

mahmoodi1072

Guest
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?

Continue reading...
 
Back
Top