TcpListener

  • Thread starter Thread starter Jerry Spence1
  • Start date Start date
J

Jerry Spence1

I'm just getting started with a small TCPIP project in VB.Net.

I've written a successful bit of code but I'm trying to make it work like
VB6 did. In VB6 you have a winsock and set it to connect to an IpAddress on
a certain port. The program then sits there waiting for some data to come
in, and an event is triggered, the data is processed and the routine exits
again.

With VB.Net, there doesn't seem to be an event as such. You set the
tcplister to read the data, and the program hangs until something is read.
How can I get it to trigger an event when it receives something?

- Jerry
 
Jerry Spence1 said:
I'm just getting started with a small TCPIP project in VB.Net.

I've written a successful bit of code but I'm trying to make it work like
VB6 did. In VB6 you have a winsock and set it to connect to an IpAddress
on
a certain port. The program then sits there waiting for some data to come
in, and an event is triggered, the data is processed and the routine exits
again.

With VB.Net, there doesn't seem to be an event as such. You set the
tcplister to read the data, and the program hangs until something is read.
How can I get it to trigger an event when it receives something?

The TcpListener is a simplified helper class. Using the underlying Socket
class you can do this with

s.BeginAccept( addressOf MyAcceptMethod, nothing)

But the TcpListener doesn't have this for a good reason. There's no point
in having your listener run asynchronously. Just start a worker thread to
run TcpListener.AcceptTcpClient in a loop. Each time it gets a connection
it should pass it off to a background thread and go back to listening.

David
 
Thanks David.

Ah - I think the next topic I'd better learn is threading then! If it's
running in a loop, doesn't this eat up processor resources? With an event
this isn't so.


-Jerry
 
Jerry Spence1 said:
Thanks David.

Ah - I think the next topic I'd better learn is threading then! If it's
running in a loop, doesn't this eat up processor resources? With an event
this isn't so.

The AcceptTcpClient method will block the thread until an incoming
connection is made, so no CPU is used.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top