Click to See Complete Forum and Search --> : Winsock Multi-Thread?
VBAhack
Mar 4th, 2005, 02:44 PM
Hi,
Is multi-threading the same as multiple connections? If not, can someone explain the difference/advantages/disadvantages?
I created a client app to download HTML using Winsock API and implemented subclassing. The app creates 1 window with up to 52 sockets, who's messages all go through a single WindowProc. The data received gets split up according to socket number to keep things straight.
Is this a mult-threaded app or a single thread with multiple connections? I did it this way to hopefully get faster data rates but the number of simultaneous sockets doesn't seem to matter when it comes to transfer speed. Should I have done things different? Thanks for comments/suggestions.
VBAhack
Pino
Mar 8th, 2005, 04:47 AM
Hi,
Is multi-threading the same as multiple connections? If not, can someone explain the difference/advantages/disadvantages?
I created a client app to download HTML using Winsock API and implemented subclassing. The app creates 1 window with up to 52 sockets, who's messages all go through a single WindowProc. The data received gets split up according to socket number to keep things straight.
Is this a mult-threaded app or a single thread with multiple connections? I did it this way to hopefully get faster data rates but the number of simultaneous sockets doesn't seem to matter when it comes to transfer speed. Should I have done things different? Thanks for comments/suggestions.
VBAhack
Since no-ones replied to this, I did a quick search,
http://www.vbforums.com/showthread.php?t=284670&highlight=mulithreading
does this help?
Evgeni
Mar 14th, 2005, 10:38 PM
I have created multi-thread winsock app which was a p2p application. It can get the job done(C++ app tho vb will be slower), but if you have way to many threads loaded it will be absorbing all the cpu power. If you ever had your comp shutdown unexpectedly when using bittorrent thats one of the reasons. But if you code your threads right it shouldn't be that bad.
VBAhack
Mar 18th, 2005, 03:12 PM
Thanks Pino and Evgeni for responding. What I'm really trying to do is get a clear understanding of what multi-threading really is, why it is useful, and, ultimately, how to do it. I have a winsock api example that uses multiple simultaneous connections and I get the feeling that's not multiple threads, whether I use one window and one WindowProc or mutliple windows and a different WindowProcs for each window. I use subclassing and therefore have asychronous communications, but I don't think that constitutes multiple threads.
If it is as simple as each thread has its own process that can be seen in the task manager process window, then I think I understand. I've seen posts that say multi-threading isn't possible or extremely difficult in VB. I've also seen references to Overlapped I/O and I/O completion ports and have been trying to understand what those concepts are and how they relate, if any, to multi-threading. Just trying to comprehend things and am struggling a bit - I guess I need simple answers at first before the complicated ones. Silly thought - I'm using VBA really (Excel), so if I create multiple instances of Excel (I think that's possible), with each running a winsock app, is that multi-threading?
Too many questions and not enough answers.........
VBAhack
Halsafar
Mar 18th, 2005, 04:11 PM
Once again I will start by saying "get ready to give me some reputation"
Now these definitions are by me, they are correct, but probably not in the best notation.
A thread is somewhat hard to exactly define. Each running process has a thread, threads run simultaneously, so each application on your desktop has a thread and they all go as fast as they can. Each thread has an entry point, or a main function; In C++ your int main() {} is your main function, when it returns the thread terminates. In VB I believe it is quite similar but you dont get to see it, it is probably the message pump or the event handler, when it quits the thread is gone.
A single threaded application: An application which has one thread and thus runs from the first line in the thread entry point to the last, top to bottom. Pretty well the basis of all programming languages, as you can see why now that thread has been defined.
A multi threaded application: An application which creates more than one thread to process. Rememebering threads run simultaneous, a multi threaded application can also be called a "Parrell Threaded Application". All the threads entry points just run, as fast as they can and they terminate once they return. This can be very useful, one thread can listen, one thread can deal with connection activity, one thread can transfer a part of a file while another thread transfer the other part (the love behind p2p). This of course is a problem though, since threads run as fast as they can data acsess is unpredictable; it requires a unique way of looking at code.
Your application is a single threaded application running winsock, nothing more.
The reason Multithread comes up a lot when looking for winsock is because it makes dealing with Winsock very easy and a lot faster.
Their are blocking and non-blocking. Blocking sockets will cause the problem to wait for information when winsock requests. Non-blocking sockets will just say I got no info and return. Obviously Non-blocking is better, you'd think, I am not even going to explain why they are dumb. So windows came out with Async Sockets, which I believe is what you may be using. They allow you to tell Winsock to send messages to your application so you can use non-blocking sockets in a smart way. Then they came out with EventSockets, which are almost the best I'd say, you can use them in console apps and are not dependant on any sort of message pump. This is where multithreading comes in.
You create one thread to listen, it is just an infinite while loop for example...Always listening.
You create one thread to accept connections, a max of 64 per thread (num sound familiar).
The client when connecting creates a thread with an infinite while loop...Continue to get Data.
Now, none of the application will be stuck doing any work since they are using different threads to do the work.
If you still need more I am writing a tutorial on the topic, right from ground up the whole project.
Remember to give me a reputation point.
Thanks
Halsafar
Nove
Apr 8th, 2005, 04:28 PM
Did you just TELL him to give you a reputation point? How incredibly shallow. Rating a post is a token of gratitude, not payment for service.
visualAd
Apr 10th, 2005, 04:09 AM
Seen as a single precess or thread can only deal with one connection at a time, it it useful to use multi threaded applications. A thread simply allows an application do more than one thing at a time.
For example, when an event handler is invoked in VB, a new thread is created. This in theory allows several event handlers to run simultaneously.
If you apply the same notation to Winsock, you can have your application create separate threads, each listening on different ports. One common method is have a master thread listening on your main port and once a connection has been accepted, pass it to another thread and keep the master thread listening on the main port.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.