1、建立UDP的Socket服务;
2、将要发送的数据封装到数据包中;
3、通过UDP的Socket服务将数据包发送出去;
4、关闭Socket服务。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPSend {
public static void main(String[] args) throws IOException {
System.out.println( "发送端启动......" );
// 1、创建UDP的Socket,使用DatagramSocket对象
DatagramSocket ds = new DatagramSocket();
// 2、将要发送的数据封装到数据包中
String str = "UDP传输演示:I'm coming!" ;
byte [] buf = str.getBytes(); //使用DatagramPacket将数据封装到该对象的包中
DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName( "192.168.191.1" ), 10000 );
// 3、通过UDP的Socket服务将数据包发送出去,使用send方法
ds.send(dp);
// 4、关闭Socket服务
ds.close();
}
}
|
1、建立UDP的Socket服务,因为要接收数据,所以必须明确一个端口号;
2、创建数据包,用于存储接收到的数据,方便用数据包对象的方法解析这些数据;
3、使用UDP的Socket服务的receive方法接收数据并存储到数据包中;
4、通过数据包的方法解析这些数据;
5、关闭Socket服务。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPReceive {
public static void main(String[] args) throws IOException {
System.out.println( "接收端启动......" );
// 1、建立UDP的Socket服务
DatagramSocket ds = new DatagramSocket( 10000 );
// 2、创建数据包
byte [] buf = new byte [ 1024 ];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
// 3、使用接收方法将数据存储到数据包中
ds.receive(dp); // 该方法为阻塞式的方法
// 4、通过数据包对象的方法解析这些数据,例如:地址、端口、数据内容等
String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();
String text = new String(dp.getData(), 0 , dp.getLength());
System.out.println(ip + ":" + port + ":" + text);
// 5、关闭Socket服务
ds.close();
}
}
|
由于在前两部分中,我们一次只能发送(或接收)一条消息,然后就关闭服务啦!因此如果我们想要发送多条消息,则需要不断的在发送端修改发送的内容,并且还需要重新启动服务器,比较麻烦。为了克服以上的缺点,我们可以对其进行优化,即:
1、在发送端,创建BufferedReader,从键盘录入内容;
2、在接收端,添加while(ture)循环,不断的循环接收内容。
?
1
2
3
4
5
6
7
8
9
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
|
/**
*优化UDP传输的发送端
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPSend {
public static void main(String[] args) throws IOException {
System.out.println( "发送端启动......" );
// 创建UDP的Socket,使用DatagramSocket对象
DatagramSocket ds = new DatagramSocket();
BufferedReader bufr = new BufferedReader( new InputStreamReader(System.in));
String line = null ;
while ((line = bufr.readLine()) != null ) {
// 使用DatagramPacket将数据封装到该对象的包中
byte [] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName( "192.168.191.1" ), 10000 );
// 通过UDP的Socket服务将数据包发送出去,使用send方法
ds.send(dp);
// 如果输入信息为over,则结束循环
if ( "over" .equals(line))
break ;
}
// 关闭Socket服务
ds.close();
}
}
|
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
/**
*优化UDP传输的接收端
*/
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPReceive {
public static void main(String[] args) throws IOException {
System.out.println( "接收端启动......" );
// 建立UDP的Socket服务
DatagramSocket ds = new DatagramSocket( 10000 );
while ( true ) {
// 创建数据包
byte [] buf = new byte [ 1024 ];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
// 使用接收方法将数据存储到数据包中
ds.receive(dp); // 该方法为阻塞式的方法
// 通过数据包对象的方法解析这些数据,例如:地址、端口、数据内容等
String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();
String text = new String(dp.getData(), 0 , dp.getLength());
System.out.println(ip + ":" + port + ":" + text);
}
}
}
|
四、创建聊天室
根据UDP(User Datagram Protocol, 用户数据报协议)的相关性质,我们可以进一步创建一个简单的基于UDP传输协议下的聊天室,实现互动聊天的功能。
?
1
2
3
4
5
6
7
8
9
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
|
/**
*创建UDP传输下的聊天室发送端
*/
package chat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class Send implements Runnable {
private DatagramSocket ds;
public Send(DatagramSocket ds) {
this .ds = ds;
}
public void run() {
try {
BufferedReader bufr = new BufferedReader( new InputStreamReader(System.in));
String line = null ;
while ((line = bufr.readLine()) != null ) {
byte [] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName( "192.168.191.255" ), 10001 );
ds.send(dp);
if ( "886" .equals(line))
break ;
}
ds.close();
} catch (Exception e) {
System.out.println( "对不起,发生错误啦!" );
}
}
}
|
?
1
2
3
4
5
6
7
8
9
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
|
/**
*创建UDP传输下的聊天室接收端
*/
package chat;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Rece implements Runnable {
private DatagramSocket ds;
public Rece(DatagramSocket ds) {
this .ds = ds;
}
public void run() {
try {
while ( true ) {
byte [] buf = new byte [ 1024 ];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
String ip = dp.getAddress().getHostAddress();
String text = new String(dp.getData(), 0 , dp.getLength());
System.out.println(ip + ":::" + text);
if (text.equals( "886" )){
System.out.println(ip+ "......退出聊天室!" );
}
}
} catch (Exception e) {
System.out.println( "对不起,发生错误啦!" );
}
}
}
|
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/**
*创建UDP传输下的聊天室
*/
package chat;
import java.io.IOException;
import java.net.DatagramSocket;
public class ChatRoom {
public static void main(String[] args) throws IOException {
DatagramSocket send = new DatagramSocket();
DatagramSocket rece = new DatagramSocket( 10001 );
new Thread( new Send(send)).start();
new Thread( new Rece(rece)).start();
}
}
|
原文链接:http://blog.csdn.net/qq_35246620/article/details/53557668
相关文章
猜你喜欢
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 63
-
2025-05-25 74
-
2025-05-27 87
-
2025-05-29 61
-
2025-05-25 79
热门评论