Results 1 to 22 of 22

Thread: winsock and multithreading

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Location
    Birmingham - UK
    Posts
    27

    Post winsock and multithreading

    Hi,

    Just to confirm.

    I have written a server application which creates 10 winsock controls Dynamically. Therefore allowing 10 clients to connect to it.
    I have only coded for the DataArrival event once (as I only have one winsock object on my form) and within this DataArrival event I call a function which Processes the Data recieved from the client. I do use the index of the winsock and pass it to the 'ProcessData' routine to know which one of the 10 winsock called the 'ProcessData' function so I can send back a message from the 'ProcessData' function telling the client the data recived was Ok or not.

    In process Data I connect to a DB and do stuff aswell as reply back to the calling winsock.

    What I would like to confirm is every time a client connects does VB6 create a new thread for that process. Meaning even though my Process Data function has only been written once will there be multiple instances of it running and running at the same time depending on how many winsocks data arrival events called it.

    I have restriced the usage of Global Vars, as this may be effected if the above is true.

    Can some one confirm for me that the above will work and that clients will not have to wait for one process to finish before there request is carried out.

    Thanks in advance..

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    VB does NOT create multiple threads - until recently I hadn't even seen multi-threading working properly in VB6 (I searched hard for it a couple of years ago, see the link below for how it was finally done).

    I don't use Winsock, but from what other people have said on this forum each control will store the data until it is read by the program, so I'd guess that users will have to wait until your program has done what it does.

  3. #3
    Addicted Member
    Join Date
    Feb 2002
    Location
    closed
    Posts
    196
    yup - your app won't create individual processes to handle your winsock events so you will get blocking. You would need to farm out your working to seperate active exe's. Like Si suggests there is a nice theading-esque example knocking around so take a look and have a kick around with that

  4. #4

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Location
    Birmingham - UK
    Posts
    27
    "need to farm out your working to seperate active exe's. Like Si suggests there is a nice theading-esque example "


    ????
    Thanks for the help guys but how am I suppose to create seperate active exe's and what does that mean?.

    I have read around abit and none of it makes sence or is applicable to my situation.

    Some say certain methods are unstable etc etc.

    Can you guys give some sort of tip what I am suppose to do or where to look which will help my problem.


    Do I have to do somesort of createprocess every time I recieve Data on the winsock or Do I do something else.

    Thanks for the help so far.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    When you create a new VB project you get the option of the project type - "ActiveX Exe" is the one you want. This program will be used multiple times (each in a separate thread), but only needs to be written once.

    Put in it just ONE of the controls (and all the code that it runs). Then you need to link to it from your main Exe to get whatever information you need from it, and to set any parameters for it.

    The threading link below shows how to communicate between you main Exe and the ActiveX Exe - and is perfectly stable. The example code given does a file search, but you can ignore that part - what you need to look at is the communication between them.

  6. #6
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    OK...this question is right up my street.

    Right...I am assuming you want a user to say click the "Save" button, for example, and for the data to be passed to the server using TCP/IP and saved to the DB?
    Also, if another user, on another PC, does the same thing you want their data to be "saved" also, but without waiting for the 1st users DB stuff to finish...? ie, if all users click save at the same time you don't want the apps to hang for ages waiting for other users DB transactions to finish?

    Is that a good guess?

    To do this correctly you will need 7 projects.

    Client UI (EXE)
    ClientTCPProxy (DLL)
    ClientTCPConnection (ActiveX EXE)

    Server UI (EXE)
    ServerTCPConnection (ActiveX EXE)
    ServerDataProxy (DLL)
    ServerDataObjects (DLL)

    The problem with using TCP/IP to communicate with a DB is that when you click "save" and send the data to the server you are still able to click "save" again. this will send the same data to the server for a 2nd time.

    The ClientTCPConnection project prevents this from happening by looping until either confirmation data is recieved from the server, or the timeout limit is reached.

    the multithreading bit is difficult. It really depends on what functionality you want to give to your server UI. Ie, do you want to see all connections in a grid? if so, you you want to be able to close them manually? etc

    There is a multithreading example in my link, which is teh same as Si_The_Geeks. This will explain how it can be achieved. However, that thread does not have the code for TCP/IP connections.
    I have this code at home, but not at work as they deemed it too complicated to add into our apps here because the SENIOR programmer wouldn't be able to understand it *sigh*

    A few questions before continue as this can and will get slightly complicated...

    Have you ever done n-tier application development b4?
    Have you ever used property bags to pass data between projects?
    Do you know any basic API?
    Do you know how Winsock works?
    Do you know how and why an ActiveX EXE would be used?
    How many users will be usign this application at the same time?
    How long have youi been programming in VB?

    These are all important questions.

    Just trying to guage how detailed I have to be with my answers

    I have written a large app at home, that I am selling commercially, using the above methods, and it's just as quick as using MTS, but with the advantage that it is way easier to maintain, scale and deploy, plus you get to be able to add extra functionality server side.

    Regards,

    Woka

  7. #7

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Location
    Birmingham - UK
    Posts
    27
    Ok let me get this straight

    I need to create two projects.

    The first is a normal project that has one winsock control on it and and when some one connects I'll kick off another exe which is an ActiveX exe. and in there i have one winsock control which then talks directly to the client by recieving any requests and sending it any data from the DB.

    And so every time a client trys to connect to m main program I kick off another ActiveX exe to talk to it.

    Have I understood the concept correctly?

    Thanks for the help so far

  8. #8
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    The way it should work is when you run the Server UI it creates an instance of the ServerTCPConnection. This contains a winsock control, or a cSocket class in my case, and all this does it listens for a client trying to connect.
    When connection is established, the ServerTCPConnection communicates with the sevrer UI, in the same way as the multithreading example does in my link, and tells the Server UI that a conenction has been made. The Server UI then creates ANOTHER instance of the ServerTCPConnection, which waits for the next client who wants to connect.
    When a client disconnects then the ServerTCPConnection instance is then terminated.
    This means that there will ALWAYS be one connection listening for incoming connection requests.

    The 2 Proxy projects (DLLs), client and server side, are there to convert/retrive your data into/from a byte array. This allows your project to be very flexable and, like I have had to do at work, makes it easy to switch how it works. With a few clicks of the mouse you can have exactly the same project running under MTS instead of using TCP/IP...u may not need that, but it also has other advantages too.

    What DB stuff are you going to be doing?
    Can we have an example of data that will be passed and a table that will be updated.
    I can write you a small example when I get home to show you how it's done if you want.

    Do you know much about classes?

    Woka

  9. #9
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Tell I lie, you need 8 projects to do this correctly

    Client Side
    • Client UI (EXE)
    • ClientObjects (DLL)
    • ClientTCPProxy (DLL)
    • ClientTCPConnection (ActiveX EXE)

    Server Side
    • Server UI (EXE)
    • ServerTCPConnection (ActiveX EXE)
    • ServerDataProxy (DLL)
    • ServerDataObjects (DLL)

    Although the Client UI and ClientObjects can be combined into the same project. I personally use an ActiveX EXE for this, buit it really depends of what you want your project to do.

    Woka

  10. #10
    Addicted Member
    Join Date
    Feb 2002
    Location
    closed
    Posts
    196
    i think you have the general idea.

    An ActiveExe runs in its own process space so you can start up multiple instances of it [the ActiveExe] independant of the creator routine. - in the same way you can launch multiple instances of, say, MS Word


    The problem arises when you try to communicate between processes [woka - haven't really looked but i believe your example posts to a memory address for pickup is that right?] because you run into all types of re-entrance problems i.e. methods being called multiple times before it has a chance to return.

    Cheers...

  11. #11
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    My multithreading app uses subclassing, postmessages and reads the memory from other processes. So, yea, you are right.

    The way to get around the winsock problem is by looping in the clientTCPConnection until data is retrieved or a timeout is reached. The timeout values can be set from the UI by using the registry...also, so can the IP and port number of your server.
    this will be explained in the project I will write for you.

    Woka

  12. #12

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Location
    Birmingham - UK
    Posts
    27
    Ok it's starting to make sence.

    But I take it in order for me to start another instance of an ActiveX exe I need to be able to pass it a minimum of 1 value which is the connection request ID that I got from my main exe when a connection attampt was made. Other wise the what would it know to connect to.

    Also is there some sort of handle to this ActiveX exe from my main program that I can use to keep any eye on it or even kill it.


    Thanks for the help so far.

  13. #13
    Addicted Member
    Join Date
    Feb 2002
    Location
    closed
    Posts
    196
    Originally posted by Wokawidget

    this will be explained in the project I will write for you.
    any change you could write mine when your done here - i would rather be at home watching telly

  14. #14
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    OK, to see how the projects will comunicate with each other click on the link in my sig and then download the 2nd project I ahve posted in that thread.

    When you recieve a connection request you just connect, no need to pass the RequestID anywhere. What you have to do though is to send a message to teh server to say that a connection has been amde so that the server can create a new instance of the connection that will listen for another request coming in.

    If I write an example that just loads and saves Users to a DB would that help?
    I will have UserID, Username, real name and password as varibles...is that OK?

    Woka

  15. #15

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Location
    Birmingham - UK
    Posts
    27

    Lightbulb

    OK I'm nearly there

    I am getting error 10038
    method '~' of object '~' failed.

    this error occurs when I do a winsock.acceptid on the ActiveX winsock that I created on the fly.

    The ID is passed to it from the Main application which creates an instance of this ActiveX passing it the RequestID.

    TIA

  16. #16
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    I take it you have a main app that listens for incoming requests, then when you get one you create a new ActiveX EXE and then pass the RequestID, via a property or function/sub, to the new instance of the activeX...?
    Is so, then this will not work...DOn't 100% know why this won't work
    This is why the ACtiveX EXE I used was the one listening for the request, and when it gets it it then accepts it, then passes a message back to the server to spawn a new activeX exe that is "listening"

    My app ALWAYS has at least ONE activeX exe...this will ALWAYS be "listening"

    Woka

  17. #17

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Location
    Birmingham - UK
    Posts
    27
    Thanks,

    But won't there be a port conflict if you got one ActiveX talking to a client on a port and at the same time you have another instance of the same activeX listening on them same port.

    TIA

  18. #18

  19. #19
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Well, if there is then I haven't come across it yet, and I have written some pretty hefty multi-user, multi-tiered, client-server apps using that method and I could have even of dreamed of how well it works

    Woka

  20. #20

  21. #21

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Location
    Birmingham - UK
    Posts
    27
    How would the ActiveX talk back to the main app to tell it to kick off another activeX listener

    I can talk from the main app to activeX through properties etc but how to go back?

    thanks

  22. #22
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Download the example, from the link in my sig, on how an application communicates with an ActiveX EXE.
    The zip you want to download is the 2nd ZIP I posted. Think it's between the 4th and 6th post in the thread.

    If you have any question then give me a shout.
    Using that method is how my winsock connections talk to my main app...

    Woka

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width