200分求助socket聊天室的问题,服务器端怎么进行广播所有连接的客户端传来的信息?急 急 急!!

deerfly 2002-01-04 03:31:05
我用socket编了一个网络聊天室,服务器端以多线程的方式进行处理和它连接的客户端,
每个客户和服务器以一个socket建立连接,服务器端怎么进行广播所有连接的客户端传来的信息给每一个客户端,怎么取得这一时刻和服务器端连接的线程数并广播?使每一个和服务器端连接的客户端的页面都能时时的更新??可以在加分!!!!
...全文
188 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyyoung 2002-01-04
  • 打赏
  • 举报
回复
java.net
Class MulticastSocket
java.lang.Object
|
+--java.net.DatagramSocket
|
+--java.net.MulticastSocket


--------------------------------------------------------------------------------

public class MulticastSocket
extends DatagramSocket
The multicast datagram socket class is useful for sending and receiving IP multicast packets. A MulticastSocket is a (UDP) DatagramSocket, with additional capabilities for joining "groups" of other multicast hosts on the internet.

A multicast group is specified by a class D IP address and by a standard UDP port number. Class D IP addresses are in the range 224.0.0.0 to 239.255.255.255, inclusive. The address 224.0.0.0 is reserved and should not be used.

One would join a multicast group by first creating a MulticastSocket with the desired port, then invoking the joinGroup(InetAddress groupAddr) method:

// join a Multicast group and send the group salutations
...
String msg = "Hello";
InetAddress group = InetAddress.getByName("228.5.6.7");
MulticastSocket s = new MulticastSocket(6789);
s.joinGroup(group);
DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(),
group, 6789);
s.send(hi);
// get their responses!
byte[] buf = new byte[1000];
DatagramPacket recv = new DatagramPacket(buf, buf.length);
s.receive(recv);
...
// OK, I'm done talking - leave the group...
s.leaveGroup(group);

When one sends a message to a multicast group, all subscribing recipients to that host and port receive the message (within the time-to-live range of the packet, see below). The socket needn't be a member of the multicast group to send messages to it.
When a socket subscribes to a multicast group/port, it receives datagrams sent by other hosts to the group/port, as do all other members of the group and port. A socket relinquishes membership in a group by the leaveGroup(InetAddress addr) method. Multiple MulticastSocket's may subscribe to a multicast group and port concurrently, and they will all receive group datagrams.

Currently applets are not allowed to use multicast sockets.



Since:
JDK1.1
flytsu 2002-01-04
  • 打赏
  • 举报
回复
可以使用类MulticastSocket,每个客户端在一个广播地址的特定端口上监听UDP报文,而且在客户端启动的时候也向该地址进行广播,这样别人能知道你的存在,而你也可以知道别人的存在,还不需要服务器,而且信息总是最新的,再设置每隔一段时间重新广播。。。

基本原理如下:
多点广播:
int broadcastPort=5678;
int ttl=10;
String broadcastAddress="239.80.80.80";
InetAddress multiAddr=InetAddress.getByName(broadcastAddress);
byte[] bbuf=new byte[1024];
DatagramPacket multiDatagram=new DatagramPacket(bbuf,bbuf.length,multiAddr,broadcastPort);
MulticastSocket broadcastSocket=new MulticastSocket();
multiSocket.send(multiDatagram,ttl);

广播监听:
MulticastSocket receiveSocket=new MulticastSocket(broadcastPort);
receiveSocket.joinGroup(multiAddr);
receiveSocket.receive(multiDatagram);


说明:broadcastPort>1024就可以,ttl是广播包允许经过的路由器的个数,越大则越可以被更多的客户端监听到,但是给网络带来太大负担。broadcastAddress理论的取值范围为244.0.0.0到239.255.255.255,但是有些保留地址224.0.0.0到244.0.0.255。
其它细节可以参考书籍或者API。
skyyoung 2002-01-04
  • 打赏
  • 举报
回复
http://jchat.sourceforge.net/screenshots.htm

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧