Advice needed for sockets/threads

  • 12 years ago
    HI All, I need some advice for a service that i am writing. I need to be able to connect and send data to around 800 devices (with ip addresses). This needs to be done asynchronously. The problem that i have is that if i create 800 threads the performance of the machine will be non existent and i understand that 800 sockets would have the same affect. I just need some advice on the best way to go about this. Thanks in advance
  • 12 years ago
    It's a big BIG code subject, I think you'll have to be more specific about whats going on, BUT i'll offer my first impressions. I've done something similar with http gets. Spawning off threads to grab a web pages html from 2000 different locations. Not exactly the same but I can offer some info. One, I have found that when I get to 1200 or so threads windows will just least on my PC and image. With the standard xp kernal running 650 or so threads baseline, adding another 800 is iffy. You can get the number of threads running from the task manager FYI. I will go on the assumption that what you are trying to do is spin the thread, open the connection, copy the file, close connection, terminate thread. Based on the above assumption, I will assume you are wanting to only have x number of threads open at any one time. My quite inelegant solution was this. Create a loop to spin off the threads and add a sleep command to control the number of threads. Not elegant, code lawyers would hate it, but it works well. My https gets are high latency and I have found spinning off 10 per second to work well in my case thread.sleep(100) For Each objSite_ As SiteClass In SiteClassArray_ Dim objSS_ As New GetHttpData(objSite_, timeout_) Dim t As Thread = New Thread(AddressOf objSS_.Go) t.IsBackground = True t.Name = objBranch_.BrDbSiteID & "1" t.Start() Thread.Sleep(100) Next What size is the file? If it's huge, you may need less or more threads/sec. I would think you could, through trial and error, find a relationship between the size of the file and the number of threads per second your pc can handle ex. 10 meg file = 10 threads/sec 1 meg file = 25 threads/sec 100 meg file = 2 threads/sec Once you develop your relationship, your program could calculate the thread sleep time based on file size to be transmitted. I like to make sure my threads quit, so I do use the thread.currentthread.abort to close down each thread. Not sure if you want to do that or not. I've tried to figure out thread pools, never got anywhere with them, so I've just been using the above method. Proxy servers caused me much headache. It seems when you test your code with VB's Debug the Debug "host" uses IE's proxy settings while a compiled program does not. Lost a couple of weeks to that one. So I found some code to create proxy bypasses before doing my http gets...again, not sure if that applies to sockets. Finally, I would REALLY like to know how to copy a file via sockets. Could you help me with that?
  • 12 years ago
    About my above post...I didn't edit everything correctly but the important part was showing you the delay in spinning off threads. My sweetspot for threads has been 850-1050 or so.
  • 12 years ago
    Your best bet is definitely to use a ThreadPool or similar - spool up a bunch of tasks and pick them off a group at a time. I've found this [extended thread pool]( code very useful.

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“In theory, theory and practice are the same. In practice, they're not.”