加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java.net.MulticastSocket Example--reference

发布时间:2020-12-14 06:19:29 所属栏目:Java 来源:网络整理
导读:In this example we are going to explain how to use?MulticastSocket ?in Java,in order to enable a server to easily send?information to? multiple clients ,which are all connected to the same port and address. We will?describe?the whole proce

In this example we are going to explain how to use?MulticastSocket?in Java,in order to enable a server to easily send?information to?multiple clients,which are all connected to the same port and address. We will?describe?the whole process,by creating?both the server and the?client,and guide you through the main concepts that need to be understood to create this type of applications.

1. MulticastSocket Server

We are going to use a?DatagramSocket,?to enable the server to send packets of information to the client/clients. A datagram,by definition,is?“an independent,self-contained message sent over the network whose arrival,arrival time,and content are not guaranteed”. Essentially,we are opening a?DatagramSocket?in order to send?DatagramPacket?messages to the client. We are using the datagram classes (instead of standard sockets) because they allow us to broadcast information to multiple clients,that are all connected to a.

Let’s see the code of the server:

MulticastSocketServer.java

01?
02?
03?
04?
05?
06
07??
08?
09??
10???
11
12????
13
14
15?
16
17??
18??
19?
20
21
22
23?
24
25
26?
27?
28
29
30?
31
32
33
34

One thing that we need to take into consideration here,is that there are specific addresses that allow us to use a?MulticastSocket?are limited,specifically?in the range of?224.0.0.0 to 239.255.255.255. Some of them are reserved,like?224.0.0.0. The address that we are using,?224.0.0.3,can be used safely.

?

2. MulticastSocket Client

Regarding the client,we are going to move a little bit differently. We are going to create a client class,that will accept incoming messages from the server,and then we are going to duplicate this class. The point here is that by using the same code,we can connect to the server seamlessly,while having as many clients as we like.

Let’s see the code of the client:

MulticastSocketClient.java

01?
02?
03?
04?
05?
06
07??
08?
09??
10???
11
12????
13
14
15?
16
17
18
19?
20?
21
22
23??
24
25
26?
27?
28
29?
30
31
32?
33?
34
35?
36
37
38
39

First,we start the client,which will keep waiting for incoming packets of information. As soon as we start the server,it will send the information packets and the client will receive them and print the information on the screen.

Server Output

1
2
3
4
5

Client Output

1
2
3
4
5

In order to use multiple clients,just create anew Java project and copy-paste the code of the client,but change the output to?Socket 2instead of?Socket 1. You will see that when the server runs,the messages will be sent to both clients,and both clients will print the same results (except for the socket number part). Take a look at this screenshot here. We are running the first client through eclipse,the second through the command line,and the server through the command line as well.

?

3. Download the project

This was an example of MulticastSocket usage in Java.

DownloadYou can download the full source code of this example here :?,?

reference from:

http://examples.javacodegeeks.com/core-java/net/multicastsocket-net/java-net-multicastsocket-example/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读