CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2006
    Posts
    28

    MultiThreaded Socket Fun

    http://csharp.pastebin.com/qnSZTdzr

    I'm trying to make a "SendRecieveCommand" method. The problem that I am having is this.

    When the class gets initialized it creates a background worker listening on that socket. The problem that I am having is that, the background worker is the one that is listening and will wait for a response. I need to Cancel/Pause that background worker, so that I can write, and read in one function, then re-initialize that background worker so it can look for stray connections.

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: MultiThreaded Socket Fun

    I'm not sure why you would need to pause the worker thread that is listening?

    I normally have a thread that listens and when a connection comes in I create a new thread to handle the connection. The listener keeps listening while the new thread does it's send and receive of data. If more than one connection request is received it simply creates new threads as needed to handle them while still listening for more.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Sep 2006
    Posts
    28

    Re: MultiThreaded Socket Fun

    On the server, that's pretty much how it's setup.

    This is the client side, which is 90% what I need. When I create the connection, a separate thread is made that does the listening, and shoots everything into an event.

    From what I can tell, the listening thread interfere's with the Read/Write thread, so I am not sure what the problem is.

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: MultiThreaded Socket Fun

    The server is where you would listen. The client sends a connection request to the listener on the server and if accepted is able to send and receive. No reason to have a listener on a client that I can think of.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Sep 2006
    Posts
    28

    Re: MultiThreaded Socket Fun

    Just because your application might not need it, doesn't mean my does too.

    I have an application, that talks to the server. It opens up a connection, and starts talking with the server. On the server, I don't mind if it just listens and fires everything to an event (non-blocking), and then the server does stuff, and responds back.

    On the client tho, I need it to do the same thing. If the server needs to send a command to the client (like when a new client connects), the server needs to send a command to all the clients. Therefore a non-blocking method needs to be implemented on the clients. The only problem is, on some occasions, I need to send a command to the server, and then the server needs to give a response back right away. I need a blocking method, to send a message to the server, then get a response back right away. The problem is that background thread, that listens all the time.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: MultiThreaded Socket Fun

    Rather than rolling your own multithreaded client/server mechanism, you might want to look into using WCF. It offers dual bindings (so the server can callback into the clients), and other security and transport options.


    __________________________________________________________
    Arjay

    See my latest series on using WCF to communicate between a Windows Service and WPF task bar application.
    Tray Notify - Part I Tray Notify - Part II

    Need a little help with Win32 thread synchronization? Check out the following CG articles and posts:
    Sharing a thread safe std::queue between threads w/progress bar updating
    Simple Thread: Part I Simple Thread: Part II
    Win32 Thread Synchronization, Part I: Overview Win32 Thread Synchronization, Part 2: Helper Classes

    www.iridyn.com


  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: MultiThreaded Socket Fun

    So your client is both a client and a server? I would think that if you created a new thread to accept the connection request which is received by your listener that it would work just fine. Basically that is the way a server should work and whenever you are listening for and accepting connections you are a server. Hence the confusion when you said client but were using a listener. Listener always impiles server.
    Always use [code][/code] tags when posting code.

  8. #8
    Join Date
    Jan 2010
    Posts
    1,133

    Re: MultiThreaded Socket Fun

    From what I can see, based on what danbopes said, there's no need for having a server application on both ends.
    Danbopes, your 'client' doesn't accept other clients, right?
    The thing is, the server listens for incoming connections, and once one is established, you should have no problems to initiate communication on either end at any given time (at least as it would appear to the human user), as long as the send/receive functionality is correctly implemented.

    Anyway, some code is probably waiting for something to happen, and thus preventing something else to happen - which means that some of your code is in the wrong place.
    I could be wrong, though. And, I admit what I said is rather vague, but, then again, so is the info on your problem.
    It would really help if you posted some code. Like, how you handle multiple clients, where and how you handle a send/receive operation, in what way are you using threads/async-methods.
    (And, to be clear - I'm not talking about how you handle the data you received, but about how you proceed with the receiving afterwards, and also what assumptions you have about what code will execute when something happens.)

    Also, I don't quite follow your logic on blocking vs non-blocking methods. I'm not saying you did anything wrong, and I understand why one would be preferable over the other, but this sentence is somewhat unclear:
    "[...] the server needs to send a command to all the clients. Therefore a non-blocking method needs to be implemented on the clients."

    On the server side, you mean?
    You can use either the blocking or the non-blocking approach. The important difference is how the server or client app will behave - block and wait (and annoy a human user) or not.
    Last edited by TheGreatCthulhu; June 6th, 2010 at 09:56 AM.

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