Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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

  • Administrators
Posted

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.

 

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)

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Application.DoEvents()

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

.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...