By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,296 Members | 1,457 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,296 IT Pros & Developers. It's quick & easy.

[Socket+thread] Send message to all client

P: n/a
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".

Jun 27 '08 #1
Share this Question
Share on Google+
16 Replies


P: n/a
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()?

Jun 27 '08 #2

P: n/a
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.
Jun 27 '08 #3

P: n/a
On 24 Giu, 19:56, Mariano <mariano.calan...@gmail.comwrote:
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.
Jun 27 '08 #4

P: n/a
Mariano <ma**************@gmail.comwrites:
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.

--
Keith Thompson (The_Other_Keith) ks***@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"
Jun 27 '08 #5

P: n/a
Mariano wrote:
On 24 Giu, 19:56, Mariano <mariano.calan...@gmail.comwrote:
>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
Jun 27 '08 #6

P: n/a
On 24 Jun., 21:20, Mariano <mariano.calan...@gmail.comwrote:
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().
Jun 27 '08 #7

P: n/a
On Jun 24, 10:20*am, Mariano <mariano.calan...@gmail.comwrote:
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.

Jun 27 '08 #8

P: n/a
On 25 Giu, 02:03, pete <pfil...@mindspring.comwrote:
Mariano wrote:
On 24 Giu, 19:56, Mariano <mariano.calan...@gmail.comwrote:
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?
Jun 27 '08 #9

P: n/a
On 25 Jun, 01:44, Nicolas Florian <nicolas...@gmx.dewrote:
On 24 Jun., 21:20, Mariano <mariano.calan...@gmail.comwrote:
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

Jun 27 '08 #10

P: n/a
Mariano wrote:
On 25 Giu, 02:03, pete <pfil...@mindspring.comwrote:
>Mariano wrote:
>>On 24 Giu, 19:56, Mariano <mariano.calan...@gmail.comwrote:
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.
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
Jun 27 '08 #11

P: n/a
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.comwrote:
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?
Jun 27 '08 #12

P: n/a
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.programmer or comp.programming.threads
Jun 27 '08 #13

P: n/a
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.

Jun 27 '08 #14

P: n/a
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>
Jun 27 '08 #15

P: n/a
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 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>

Jun 27 '08 #16

P: n/a
Chad <cd*****@gmail.comwrites:
On Jun 25, 2:57*pm, Antoninus Twink <nos...@nospam.invalidwrote:
[snip]
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.

--
Keith Thompson (The_Other_Keith) ks***@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"
Jun 27 '08 #17

This discussion thread is closed

Replies have been disabled for this discussion.