TCPListener does not automatically read

Winanjaya

Newcomer
Joined
Sep 16, 2003
Messages
10
Dear the Expert,
I am very new in VB.NET, I created winsock app by following the .NET sample program .. what I do not understand is why the TCPListener seems like does not automatically read when the new string sent by TCPClient..? .. I mean for the 1st time connected TCPListener works fine ..but after that I tried to send some strings to it but it seems like unable to read.. does it have read event to handle it ..(like winsock component in VB6?)

please help me ..I have stuck on this for almost 2 weeks ..and I am getting frustated with this..;>((


many thanks in advance

Regards
Winanjaya
 
Thanks for your quick reply, I followed the codes at microsoft.com

private sub form_load()
tcpListener.Start()
ListenerGO
end sub


private sub ListenerGO()
Console.WriteLine("TCP Server is up and waiting for Client connection...")
Try
''Accept the pending client connection and return a TcpClient for communication.
Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
Console.WriteLine("Connection accepted.")
' Get the data stream.
Dim networkStream As NetworkStream = tcpClient.GetStream()
' Read the data stream into a byte array.
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
' Return the data received from the client to the console.
Dim clientdata As String = Encoding.ASCII.GetString(bytes)
Console.WriteLine(("Client sent: " + clientdata))
Dim responseString As String = "Successfully connected to TCP server."
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
networkStream.Write(sendBytes, 0, sendBytes.Length)
Console.WriteLine(("Message Sent by TCP Server /> : " + responseString))

'Close TcpListener and TcpClient.
tcpClient.Close()
tcpListener.Stop()

Console.WriteLine("Exit")
Console.ReadLine()
Catch e As Exception
Console.WriteLine(e.ToString())
Console.ReadLine()
End Try
End sub

am I missing something??

TIA

Winan
 
Not worked with the TCPListner overly much so I may be wrong - but won't the tcpListner.Stop prevent it having new connections? Try removing that line and just keep looping in the main form just to see if it will keep accepting new connections.

Visual Basic:
private sub form_load()
tcpListener.Start()
do
ListenerGO
loop while true
end sub
if it now starts to accept more than one connection then at least the problem is spotted ;) (even if the code I suggested isn't that good)
 
I am afraid that it will do looping forever that mean my PC will hang .. does VB.NET also provide doevent ..for exit ... I need your idea .. thanks again

Winan
 
Application.DoEvents()
Visual Basic:
Private IsClosing As Boolean

Private Sub form_load(sender as object, e as eventargs) Handles MyBase.Load
tcpListener.Start()
Do
    ListenerGO
    Application.DoEvents()
Loop While IsClosing = False
End Sub

Private Sub Form_Closing(sender as object, e as EventArgs) Handles MyBase.Closing
IsClosing = True
End Sub
 
Back
Top