[Socket+thread] Send message to all client

M

Mariano

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".
 
A

Antoninus Twink

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()?
 
M

Mariano


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.
 
K

Keith Thompson

Mariano said:
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.programmer or
comp.programming.threads.
 
N

Nicolas Florian

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().
 
K

Kaz Kylheku

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.programmer, or possibly comp.programming.threads.
 
M

Mariano

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?
 
N

Nick Keighley

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.
 
N

Nicolas Florian

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?
 
A

Antoninus Twink

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.
 
C

Chad

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>
 
C

Chad

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 understand that this is a C
forum. That is, 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.

A while I'm off topic, Google needs to get a few their MIT scientists
to fix the stupid 'remove feature' on this forum.

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

Keith Thompson

Chad said:
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. [...]
</OT>

"Antoninus Twink" is simply a troll, and is best ignored.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top