[套接字+线程]向所有客户端发送消息 [英] [Socket+thread] Send message to all client

查看:77
本文介绍了[套接字+线程]向所有客户端发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

然后,我有一个客户端和一个服务器通过TCP套接字连接。

当服务器正在侦听时,客户端可以建立新的连接。

每次新的客户端尝试启动连接,服务器

使用pthread_create()为每个客户端启动一个新的分离线程。

通过这种方式我可以有一个服务器监听和一个或更多客户

已连接。如果客户端发送特定输入,服务器必须断开所有客户端的连接,关闭自身并返回提示。


我该怎么做?


实际情况下,只有客户端发送了特定的

输入才能正确断开连接,所有其他客户端首先连接的是
等待。

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

推荐答案

2008年6月24日17:20,Mariano写道:
On 24 Jun 2008 at 17:20, Mariano wrote:

然后,我有一个客户端和一个服务器通过TCP套接字连接。

当服务器正在侦听时,客户端可以建立新的连接。

每次新客户端尝试启动连接时,服务器

使用pthread_create()为每个客户端启动一个新的分离线程。

这样我可以拥有一个服务器正在监听,一个或多个客户端连接了
。如果客户端发送特定输入,服务器必须断开所有客户端的连接,关闭自己并返回提示。


我该怎么做?
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?



使用exit()?

Using exit()?


On 24 Giu,19:48,Antoninus Twink < nos ... @nospam.invalidwrote:
On 24 Giu, 19:48, Antoninus Twink <nos...@nospam.invalidwrote:

2008年6月24日17:20,Mariano写道:
On 24 Jun 2008 at 17:20, Mariano wrote:

然后,我有一个客户端和一个服务器通过TCP套接字连接。

当服务器正在侦听时,客户端可以建立新的连接。

每次新客户端尝试启动连接,*服务器

使用pthread_create()为每个客户端启动一个新的分离线程。

这样我可以有一台服务器听取和一个或多个客户

连接。如果客户端发送特定输入,服务器必须断开所有客户端的连接,关闭自己并返回提示。
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?



使用exit()?


Using exit()?



当然。

Sure.


24 Giu,19:56,Mariano< mariano.calan ... @ gmail.comwrote:
On 24 Giu, 19:56, Mariano <mariano.calan...@gmail.comwrote:

On 24 Giu,19:48,Antoninus Twink< nos ... @nospam.invalidwrote:
On 24 Giu, 19:48, Antoninus Twink <nos...@nospam.invalidwrote:

2008年6月24日17:20,Mariano写道:
On 24 Jun 2008 at 17:20, Mariano wrote:


然后,我有一个客户端和一个服务器通过TCP套接字连接。

当服务器正在侦听时,客户端可以建立新的连接。

每次新客户端尝试启动连接,*服务器

使用pthread_create()为每个客户端启动一个新的分离线程。

通过这种方式我可以监听一个服务器和一个或多个客户端

已连接。如果客户端发送特定输入,服务器必须断开所有客户端的连接,关闭自己并返回提示。
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?


使用exit()?
Using exit()?



当然。


Sure.



但是在服务器上使用exit()只关闭服务器进程和他所有的b
个线程,这个线程仍处于待定状态,我需要之后退出所有

客户端返回控件以提示。

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.


这篇关于[套接字+线程]向所有客户端发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆