CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    May 2007
    Posts
    127

    Question Single socket, more than one socket, multithreading?

    Hello! I want to make an app (for personal use) with this functionality (I'm using MFC):
    1. Send the screen of one computer to another;
    2. Send only a window from one computer to another (using PrintWindow function);
    3. Capture the webcam and send it to, again, another computer.
    (all 3 in the same time)

    I think there are 3 solution to do this:
    1. Use just one socket: record the screen, window and webcam in one thread and send it through one socket (problem: must wait to receive the screen in order to get the webcam: "received data: ahh... the screen, ok... another data... the screen again?" what I want to say is that if the screen manage to send itself more than 2 frames before the webcam I must wait to the screen).
    2. Use multiple socket?! I declare 3 socket variables: one for each connection (screen, window, webcam)? This means I must listen on 3 different ports on the server? (I don't like more than one port).
    3. Multithreading? Ok... use 3 different threads: one (the main) to capture the desktop and send it over the net, one for window and one for webcam. But, using the same socket to send data or diferent socket?

    One question: is multithreading usefull on the server? The user will not interact with the server more than make him listen. I've read that multithreading is helpfull when you want to make your UI more responsive. (should I capture all 3 things in one thread?).
    What about the client side? Should I have 3 separate thread?

    One way:
    On the client side I declare 3 socket variables. The main socket will connect the app and will handle the receiving of data from the screen; the other 2: the window and the webcam. No need for multithreading, right? The client will not make any intense calculation: just receive and display and I think there is no need for multithreading.
    But, how do I connect with 3 socket on the server with only one port? I think I must create a main socket on the server that will listen for connections and will handle screen capture. When the client want to get data from the webcam (or a window) the app will connect to the server with another socket and the server will create a separate thread for this request and another socket for connection.
    But how I do this? How I know on the server which kind of thread should I create: is for a webcam or window capture? How I know that the request is not from another client. All I have is the OnAccept event.

    I hope you understand what I've said. Please help me! My time to do this project is running out!
    Bye!
    Last edited by Chirieac; May 13th, 2008 at 03:46 AM.
    I love this forum. Thanks all for your help!

  2. #2
    Join Date
    May 2006
    Location
    Indonesia & Japan
    Posts
    399

    Re: Single socket, more than one socket, multithreading?

    What you need to concern are:

    1. Captured image size. In some cases you may need data compression
    technology.

    2. To transfer data as fast as possible to destination, you may concern to
    use multiple sockets using multithreading by splitting image data into
    several split data. With this technique you can send data as parallel.

    Henky
    henky
    ----------------------------------
    [email protected] is not my email address anymore...
    Jangan Pernah Menyerah

  3. #3
    Join Date
    May 2007
    Posts
    127

    Re: Single socket, more than one socket, multithreading?

    Thanks henky for your reply!

    If I'll use multiple sockets with multithreading I will receive data faster?
    Here's a test of mine (using localhost):

    At first I've send it 20mb through one socket and then 5mb through the same socket: time 12sec.
    Then I've used 4 apps (to simulate multithreading): app A listening on port 4000 and app B on port 4001; with app X I've connected to app A and app Y to app B.

    After that I've send it from app A 20mb and then from app B 5mb after 1 sec.

    Result: of course I've received the 5mb first and then those 20mb, but still in 12 sec.
    So, is my test right?
    I love this forum. Thanks all for your help!

  4. #4
    Join Date
    May 2006
    Location
    Indonesia & Japan
    Posts
    399

    Re: Single socket, more than one socket, multithreading?

    Not exactly right.

    1. localhost can't simulate real networking world since it works on memory.
    2. 4 apps will not have same performance as 4 threads.

    What i meant, you need to split your data first.
    ie: 200 MB split into 50 MB, then trasfer them using 4 threads.

    Some of download accelerator softwares use this technique.

    Henky
    henky
    ----------------------------------
    [email protected] is not my email address anymore...
    Jangan Pernah Menyerah

  5. #5
    Join Date
    May 2007
    Posts
    127

    Re: Single socket, more than one socket, multithreading?

    Thanks for your quick reply!

    What I want is this:
    on some intervals of time I want to send different data like images, sound or file transfer. So, is better to send this data through different sockets on different thread?
    I love this forum. Thanks all for your help!

  6. #6
    Join Date
    May 2007
    Posts
    127

    Re: Single socket, more than one socket, multithreading?

    I ask because I don't know multithreading and before I start learning I want to know if I need it first.
    I love this forum. Thanks all for your help!

  7. #7
    Join Date
    May 2007
    Posts
    127

    Re: Single socket, more than one socket, multithreading?

    The bottom line is: I will receive data faster if I'll send it through multiple sockets with multithreading?
    If yes, what about multiple sockets without multithreading?
    I love this forum. Thanks all for your help!

  8. #8
    Join Date
    May 2007
    Posts
    127

    Re: Single socket, more than one socket, multithreading?

    I've search it the answer in google and no luck. I read right now the book Network Programming for Windows, second edition. Maybe I'll find in there any answer for advantages in using multiple socket, when and how to use it.
    I love this forum. Thanks all for your help!

  9. #9
    Join Date
    May 2006
    Location
    Indonesia & Japan
    Posts
    399

    Re: Single socket, more than one socket, multithreading?

    Quote Originally Posted by Chirieac
    I've search it the answer in google and no luck. I read right now the book Network Programming for Windows, second edition. Maybe I'll find in there any answer for advantages in using multiple socket, when and how to use it.
    Sorry for my late reply. I am living in Japan timezone.

    In order to realize faster transmission, you must use multithread with
    multiple socket. It won't be useful if using multiple socket but in single
    thread, cause it will work in sequential order.
    henky
    ----------------------------------
    [email protected] is not my email address anymore...
    Jangan Pernah Menyerah

  10. #10
    Join Date
    May 2007
    Posts
    127

    Re: Single socket, more than one socket, multithreading?

    Thanks again henky!

    One final question: Do I need to listen on multiple ports?

    It will work like this: server listening on one port; client is connecting with 2 sockets to the server; the server on accept creates 2 threads with 2 sockets, one for each connection?

    Thanks again, Bye!
    I love this forum. Thanks all for your help!

  11. #11
    Join Date
    May 2006
    Location
    Indonesia & Japan
    Posts
    399

    Re: Single socket, more than one socket, multithreading?

    Quote Originally Posted by Chirieac
    Thanks again henky!

    One final question: Do I need to listen on multiple ports?

    It will work like this: server listening on one port; client is connecting with 2 sockets to the server; the server on accept creates 2 threads with 2 sockets, one for each connection?

    Thanks again, Bye!
    You're welcome.

    No. You don't need to listen on multiple ports.

    Regards,
    Henky
    henky
    ----------------------------------
    [email protected] is not my email address anymore...
    Jangan Pernah Menyerah

  12. #12
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Single socket, more than one socket, multithreading?

    Quote Originally Posted by [email protected]
    In order to realize faster transmission, you must use multithread with multiple socket. It won't be useful if using multiple socket but in single thread, cause it will work in sequential order.
    This is COMPLETELY UNTRUE.

    A single threaded application using IO Completion ports will have equivilant performance to a multi-threaded application.

    Also for transmission, everything will STILL be serialized through the network adapter (you can only be physically transmitting one bit at a time), the improvement with multiple sockets will have a fairly low upper bound. The primary benefit with multiple streams is the interleaving of the ACK/NAK process that happens below the covers.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  13. #13
    Join Date
    May 2007
    Posts
    127

    Re: Single socket, more than one socket, multithreading?

    Thanks TheCPUWizard for your reply! So, even I send different data in the same time through multiple sockets I will still receive the data in the same amount of time if I will send it through one socket?
    I love this forum. Thanks all for your help!

  14. #14
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Single socket, more than one socket, multithreading?

    Quote Originally Posted by Chirieac
    Thanks TheCPUWizard for your reply! So, even I send different data in the same time through multiple sockets I will still receive the data in the same amount of time if I will send it through one socket?
    Look at the actual sequence...

    1) You get data from "somewhere"
    2) You submit the data to the operating system for Transmission
    3) The NIC (hardware) get data from the OS and transits when the Wire is Free.
    4) The NIC tells the OS that the Transmission is Complete
    5) The OS tells your application that transmission is complete.

    Step #3 is the place where things become sequential (for the real tech heads it is a sub-step of #3).

    The goal is to keep step #3 busy as close to 100% as possible.

    A multithreaded approach has (somewhat) independant threads all pushing data at the NIC. These threads will each block while the NIC is busy, but will wake quickly when it becomes available. However you have no control over which one will wake up, and the data it will transmit has already been pre-determined.

    An IO completion port allows you do directly control what wil get transmitted next.

    Consider Video being used like a survalaince camera. If everything is going "Smooth" it would be good to get a full 60 frames of video per second. However if things temporarily fall behind, then it may be acceptible to drop frames rather than queue them up in the attempt to "Catch up".
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  15. #15
    Join Date
    May 2007
    Posts
    127

    Re: Single socket, more than one socket, multithreading?

    Thanks again! I will read about IO completion ports...
    The idea is this: if i send data through one socket the data will be put sequential to the operating system, even if I call send one after another,
    since if i send it through multiple sockets (with IO completion ports or multithreading) the data will be put in to the operating system in parallel?
    Bye!
    I love this forum. Thanks all for your help!

Page 1 of 2 12 LastLast

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

Featured