[Socket+thread] Send message to all client

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mariano

    [Socket+thread] Send message to all client

    Then, I have one client and one server connected trough TCP socket.
    When server is listening, client can establish new connection to it.
    Every time that new client try to start the connection, the server
    use pthread_create( ) to start a new detached thread for every client.
    In this way I can have one server listening and one or more clients
    connected. If a client send a particular input, server must disconnect
    all clients, close itself and return to prompt.

    How can I do that?

    With actual scenario only client that have sended that particular
    input is disconnected and closed correctly, All other clients that was
    connected first are "pending".

  • Antoninus Twink

    #2
    Re: [Socket+thread] Send message to all client

    On 24 Jun 2008 at 17:20, Mariano wrote:
    Then, I have one client and one server connected trough TCP socket.
    When server is listening, client can establish new connection to it.
    Every time that new client try to start the connection, the server
    use pthread_create( ) to start a new detached thread for every client.
    In this way I can have one server listening and one or more clients
    connected. If a client send a particular input, server must disconnect
    all clients, close itself and return to prompt.
    >
    How can I do that?
    Using exit()?

    Comment

    • Mariano

      #3
      Re: Send message to all client

      On 24 Giu, 19:48, Antoninus Twink <nos...@nospam. invalidwrote:
      On 24 Jun 2008 at 17:20, Mariano wrote:
      >
      Then, I have one client and one server connected trough TCP socket.
      When server is listening, client can establish new connection to it.
      Every time that new client try to start the connection,  the server
      use pthread_create( ) to start a new detached thread for every client.
      In this way I can have one server listening and one or more clients
      connected. If a client send a particular input, server must disconnect
      all clients, close itself and return to prompt.
      >
      How can I do that?
      >
      Using exit()?
      Sure.

      Comment

      • Mariano

        #4
        Re: Send message to all client

        On 24 Giu, 19:56, Mariano <mariano.calan. ..@gmail.comwro te:
        On 24 Giu, 19:48, Antoninus Twink <nos...@nospam. invalidwrote:
        >
        On 24 Jun 2008 at 17:20, Mariano wrote:
        >
        Then, I have one client and one server connected trough TCP socket.
        When server is listening, client can establish new connection to it.
        Every time that new client try to start the connection,  the server
        use pthread_create( ) to start a new detached thread for every client.
        In this way I can have one server listening and one or more clients
        connected. If a client send a particular input, server must disconnect
        all clients, close itself and return to prompt.
        >
        How can I do that?
        >
        Using exit()?
        >
        Sure.
        but using exit() on server only close the server process and all his
        threads, this threads remain pending, I need that after exit all the
        clients retuns control to prompt.

        Comment

        • Keith Thompson

          #5
          Re: [Socket+thread] Send message to all client

          Mariano <mariano.caland ra@gmail.comwri tes:
          Then, I have one client and one server connected trough TCP socket.
          When server is listening, client can establish new connection to it.
          Every time that new client try to start the connection, the server
          use pthread_create( ) to start a new detached thread for every client.
          In this way I can have one server listening and one or more clients
          connected. If a client send a particular input, server must disconnect
          all clients, close itself and return to prompt.
          >
          How can I do that?
          >
          With actual scenario only client that have sended that particular
          input is disconnected and closed correctly, All other clients that was
          connected first are "pending".
          None of this stuff is supported in standard C. Try asking in a
          system-specific newsgroup, perhaps comp.unix.progr ammer or
          comp.programmin g.threads.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          Nokia
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          • pete

            #6
            Re: Send message to all client

            Mariano wrote:
            On 24 Giu, 19:56, Mariano <mariano.calan. ..@gmail.comwro te:
            >On 24 Giu, 19:48, Antoninus Twink <nos...@nospam. invalidwrote:
            >>
            >>On 24 Jun 2008 at 17:20, Mariano wrote:
            >>>Then, I have one client and one server connected trough TCP socket.
            >>>When server is listening, client can establish new connection to it.
            >>>Every time that new client try to start the connection, the server
            >>>use pthread_create( ) to start a new detached thread for every client.
            >>>In this way I can have one server listening and one or more clients
            >>>connected. If a client send a particular input, server must disconnect
            >>>all clients, close itself and return to prompt.
            >>>How can I do that?
            >>Using exit()?
            >Sure.
            >
            but using exit() on server only close the server process and all his
            threads, this threads remain pending, I need that after exit all the
            clients retuns control to prompt.
            You can get some functions to execute at normal termination
            using atexit.

            #include <stdlib.h>

            int atexit(void (*func)(void));

            I don't know if that is sufficient for your needs.

            --
            pete

            Comment

            • Nicolas Florian

              #7
              Re: Send message to all client

              On 24 Jun., 21:20, Mariano <mariano.calan. ..@gmail.comwro te:
              Then, I have one client and one server connected trough TCP socket.
              When server is listening, client can establish new connection to it.
              Every time that new client try to start the connection,  the server
              use pthread_create( ) to start a new detached thread for every client.
              In this way I can have one server listening and one or more clients
              connected. If a client send a particular input, server must disconnect
              all clients, close itself and return to prompt.
              >
              How can I do that?
              >
              With actual scenario only client that have sended that particular
              input is disconnected and closed correctly, All other clients that was
              connected first are "pending".
              Make a global var which gets checked by the client thread
              if it should close.

              but the better way to do things like you want to, is to use
              select().

              Comment

              • Kaz Kylheku

                #8
                Re: Send message to all client

                On Jun 24, 10:20 am, Mariano <mariano.calan. ..@gmail.comwro te:
                If a client send a particular input, server must disconnect
                all clients, close itself and return to prompt.
                >
                How can I do that?
                You write the code such that the server detects a particular input and
                disconnects all the clients. Doh?
                With actual scenario only client that have sended that particular
                input is disconnected and closed correctly, All other clients that was
                connected first are "pending".
                That means you have a bug in your program, doesn't it!

                Only a crystal ball could tell me where that bug is, since you haven't
                posted even the slightest piece of that program. And that is a good
                thing, because that program is severely off-topic here; it belongs in
                comp.unix.progr ammer, or possibly comp.programmin g.threads.

                Comment

                • Mariano

                  #9
                  Re: Send message to all client

                  On 25 Giu, 02:03, pete <pfil...@mindsp ring.comwrote:
                  Mariano wrote:
                  On 24 Giu, 19:56, Mariano <mariano.calan. ..@gmail.comwro te:
                  On 24 Giu, 19:48, Antoninus Twink <nos...@nospam. invalidwrote:
                  >
                  >On 24 Jun 2008 at 17:20, Mariano wrote:
                  >>Then, I have one client and one server connected trough TCP socket.
                  >>When server is listening, client can establish new connection to it.
                  >>Every time that new client try to start the connection, the server
                  >>use pthread_create( ) to start a new detached thread for every client.
                  >>In this way I can have one server listening and one or more clients
                  >>connected. If a client send a particular input, server must disconnect
                  >>all clients, close itself and return to prompt.
                  >>How can I do that?
                  >Using exit()?
                  Sure.
                  >
                  but using exit() on server only close the server process and all his
                  threads, this threads remain pending, I need that after exit all the
                  clients retuns control to prompt.
                  >
                  You can get some functions to execute at normal termination
                  using atexit.
                  >
                  #include <stdlib.h>
                  >
                  int atexit(void (*func)(void));
                  >
                  I don't know if that is sufficient for your needs.
                  >
                  --
                  pete
                  This function should be used in client. right?

                  Comment

                  • Nick Keighley

                    #10
                    Re: Send message to all client

                    On 25 Jun, 01:44, Nicolas Florian <nicolas...@gmx .dewrote:
                    On 24 Jun., 21:20, Mariano <mariano.calan. ..@gmail.comwro te:
                    >
                    Then, I have one client and one server connected trough TCP socket.
                    When server is listening, client can establish new connection to it.
                    Every time that new client try to start the connection,  the server
                    use pthread_create( ) to start a new detached thread for every client.
                    In this way I can have one server listening and one or more clients
                    connected. If a client send a particular input, server must disconnect
                    all clients, close itself and return to prompt.
                    >
                    How can I do that?
                    >
                    With actual scenario only client that have sended that particular
                    input is disconnected and closed correctly, All other clients that was
                    connected first are "pending".
                    >
                    Make a global var which gets checked by the client thread
                    if it should close.
                    and if the clients are on a different machine?

                    but the better way to do things like you want to, is to use
                    select().
                    that's what I'd do, but you'd be *much* better off
                    checking with the experts on Unix and socket programming
                    on a unix ng.


                    --
                    Nick Keighley



                    Comment

                    • pete

                      #11
                      Re: Send message to all client

                      Mariano wrote:
                      On 25 Giu, 02:03, pete <pfil...@mindsp ring.comwrote:
                      >Mariano wrote:
                      >>On 24 Giu, 19:56, Mariano <mariano.calan. ..@gmail.comwro te:
                      >>>On 24 Giu, 19:48, Antoninus Twink <nos...@nospam. invalidwrote:
                      >>>>On 24 Jun 2008 at 17:20, Mariano wrote:
                      >>>>>Then, I have one client and one server connected trough TCP socket.
                      >>>>>When server is listening, client can establish new connection to it.
                      >>>>>Every time that new client try to start the connection, the server
                      >>>>>use pthread_create( ) to start a new detached thread for every client.
                      >>>>>In this way I can have one server listening and one or more clients
                      >>>>>connecte d. If a client send a particular input, server must disconnect
                      >>>>>all clients, close itself and return to prompt.
                      >>>>>How can I do that?
                      >>>>Using exit()?
                      >>>Sure.
                      >>but using exit() on server only close the server process and all his
                      >>threads, this threads remain pending, I need that after exit all the
                      >>clients retuns control to prompt.
                      >You can get some functions to execute at normal termination
                      >using atexit.
                      >>
                      > #include <stdlib.h>
                      >>
                      > int atexit(void (*func)(void));
                      >>
                      >I don't know if that is sufficient for your needs.
                      This function should be used in client. right?
                      This is not the right newsgroup to find people
                      who have any idea of which programming issues
                      are involved with clients and servers.
                      I don't know anything about clients and servers.

                      --
                      pete

                      Comment

                      • Nicolas Florian

                        #12
                        Re: Send message to all client

                        On Jun 25, 1:03 pm, Nick Keighley <nick_keighley_ nos...@hotmail. com>
                        wrote:
                        On 25 Jun, 01:44, Nicolas Florian <nicolas...@gmx .dewrote:
                        >
                        >
                        >
                        On 24 Jun., 21:20, Mariano <mariano.calan. ..@gmail.comwro te:
                        >
                        Then, I have one client and one server connected trough TCP socket.
                        When server is listening, client can establish new connection to it.
                        Every time that new client try to start the connection,  the server
                        use pthread_create( ) to start a new detached thread for every client.
                        In this way I can have one server listening and one or more clients
                        connected. If a client send a particular input, server must disconnect
                        all clients, close itself and return to prompt.
                        >
                        How can I do that?
                        >
                        With actual scenario only client that have sended that particular
                        input is disconnected and closed correctly, All other clients that was
                        connected first are "pending".
                        >
                        Make a global var which gets checked by the client thread
                        if it should close.
                        >
                        and if the clients are on a different machine?
                        How do you mean?

                        The autor said, that he wrote a program with more
                        threads ( one thread per client and one thread which listens for
                        connections ),
                        so wheres the problem?

                        Comment

                        • vippstar@gmail.com

                          #13
                          Re: Send message to all client

                          On Jun 25, 1:39 pm, Nicolas Florian <nicolas...@gmx .dewrote:
                          <snip>
                          so wheres the problem?
                          The problem is that this newsgroup is the wrong newsgroup to post
                          about these things (threads, servers/clients).
                          Try comp.unix.progr ammer or comp.programmin g.threads

                          Comment

                          • Antoninus Twink

                            #14
                            Re: Send message to all client

                            On 25 Jun 2008 at 10:39, Nicolas Florian wrote:
                            On Jun 25, 1:03 pm, Nick Keighley <nick_keighley_ nos...@hotmail. com>
                            >and if the clients are on a different machine?
                            >
                            How do you mean?
                            >
                            The autor said, that he wrote a program with more threads ( one thread
                            per client and one thread which listens for connections ), so wheres
                            the problem?
                            It's certainly true that the OP hasn't described his problem very
                            coherently. However, reading between the lines I think what he may be
                            asking is how to terminate the processes that are connecting to his
                            server as clients, not just the threads in the server process dealing
                            with each client.

                            That's a slightly odd situation - when the server exits, it will close
                            all its sockets, and if a client tries to read() from it, the return
                            value will be less than zero. In that case, the client process can just
                            exit itself.

                            We really need more information from the OP - exactly what problem is he
                            trying to solve, what type of socket is involved, etc.

                            Comment

                            • Chad

                              #15
                              Re: Send message to all client

                              On Jun 25, 2:57 pm, Antoninus Twink <nos...@nospam. invalidwrote:
                              On 25 Jun 2008 at 10:39, Nicolas Florian wrote:
                              >
                              On Jun 25, 1:03 pm, Nick Keighley <nick_keighley_ nos...@hotmail. com>
                              and if the clients are on a different machine?
                              >
                              How do you mean?
                              >
                              The autor said, that he wrote a program with more threads ( one thread
                              per client and one thread which listens for connections ), so wheres
                              the problem?
                              >
                              It's certainly true that the OP hasn't described his problem very
                              coherently. However, reading between the lines I think what he may be
                              asking is how to terminate the processes that are connecting to his
                              server as clients, not just the threads in the server process dealing
                              with each client.
                              >
                              That's a slightly odd situation - when the server exits, it will close
                              all its sockets, and if a client tries to read() from it, the return
                              value will be less than zero. In that case, the client process can just
                              exit itself.
                              >
                              We really need more information from the OP - exactly what problem is he
                              trying to solve, what type of socket is involved, etc.
                              <OT>
                              I've been reading comp.lang.c for the past few years. Over time I've
                              come to recognize the regulars. Some of them are totally brilliant
                              computer scientists. Some of them are brilliant in all multiple areas
                              of science. There are two that I think just have a direct line to god.
                              However ALL them either refer an off topic post to another forum or
                              they just ignore the poster.

                              For some strange reason you feel the need to try to be an expert in
                              off topic posts. You don't seem to understanding that this is a C
                              forum. Ie, not a place to discuss networking or C++. That is why there
                              are forums related to those topics. Maybe you are retarded. Who knows.
                              Either way, I consider you a blemish on this forum.

                              Now back to our discussion on C.
                              </OT>

                              Comment

                              Working...