Visual Basic .NET Forums  
Click here to advertise with us

Go Back   Visual Basic .NET Forums > Components & Controls > Net / Sockets

Net / Sockets Components for network and related use

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-25-2009, 11:23 PM
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Jun 2009
Location: New Zealand
Posts: 5
Reputation: 0
hopeful is on a distinguished programming path ahead
Default Converting VB6 winsock app to .net

Hi,

I am converting an existing TCP server application that uses the winsock control from VB6 to vb.net. The application runs as a service and communicates with approximately 500 clients. Each client is connected for long periods and has occasional conversations with the server.

I have been reading about the socket, tcplistener and tcpclient classes in .net. I was dissappointed to see there do not appear to be any events in these classes and that it seems the closest equivalent to the DataArrival event is to do an asynchronous read, processing the received data in a separate thread - although I would prefer to be asynchronously notified of data arrival as I don't want to poll 500 clients repeatedly.

Having a separate thread for each client connection seems excessive and would add unnecessary complexity to the application, which currently runs quite happily as a single thread. The server maintains complex data structures for each client and does other work while it is operating and I don't want to have to make all of this code thread-safe.

One idea I had was to read the data in a separate thread and somehow fire an event in the main thread to let it know that data is available, but I don't know whether this is considered an acceptable thing to do in a multi-threaded application, or whether there is a another way to do this. I should add that I'd like to avoid excessive change to the application - I can't afford the time to completely re-engineer it unless absolutely necessary.

I also considered trying to use the existing winsock dll/control via interop but I would like to go fully to .net if possible.

I'd appreciate any suggestions about the best way to approach this in .net.

Thanks

Last edited by hopeful; 06-25-2009 at 11:32 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-03-2009, 6:15 PM
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Jun 2009
Location: New Zealand
Posts: 5
Reputation: 0
hopeful is on a distinguished programming path ahead
Default

In case anyone has interested I have confirmed that my intended approach was reasonable. Here are some brief comments that might help others:

Reading from sockets:- It appears an async read (BeginRead) is the way to do this, although the Microsoft documentation for EndRead has a horribly misleading example (discussed elsewhere). The call back allows you to do the same thing as a DataArrival event.

Events:- Event handlers run in the same thread as they are raised (unless your main thread is a windows form in which case you can use Invoke but this is no good if you are writing a service application).

Timers:- For windows NT service applications you can't use the windows timer component. It will let you create one and everything appears ok but it won't fire. Use System.Timers.Timer instead.

Threads:- It also appears that I need to deal with multiple threads whether I want to or not, because both the asynchronous reads and the VB.NET timer class run their call back functions on separate threads. I use the Monitor class to synchronise DataArrival activities with the main thread activities and other timer activities.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks

Tags
multithreading, socket, tcp


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -4. The time now is 12:17 PM.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2


For advertising opportunities click here.