JAVA中UDP接受与发送数据初步步骤
2014-09-19 15:34
393 查看
UDP是一种高速,无连接的数据交换方式,他的特点是,即使没有连接到(也不许要连接)接收方也可以封包发送,就像在一个多人使用的步话机环境中,你不知道你的信息是否被需要的人接受到,但是你的信息确实被传递然后消失了,有时候速度比数据完整性重要,在比如视频会议中,丢失几帧画面是可以接受的。但在需要数据安全接受的环境就不适用了。
发送步骤:
使用 DatagramSocket(int port) 建立socket(套间字)服务。
将数据打包到DatagramPacket中去
通过socket服务发送 (send()方法)
关闭资源
[java]
view plaincopyprint?
import java.io.IOException;
import java.net.*;
public class Send {
public static
void main(String[] args) {
DatagramSocket ds = null;
//建立套间字udpsocket服务
try {
ds = new DatagramSocket(8999);
//实例化套间字,指定自己的port
} catch (SocketException e) {
System.out.println("Cannot open port!");
System.exit(1);
}
byte[] buf=
"Hello, I am sender!".getBytes(); //数据
InetAddress destination = null ;
try {
destination = InetAddress.getByName("192.168.1.5");
//需要发送的地址
} catch (UnknownHostException e) {
System.out.println("Cannot open findhost!");
System.exit(1);
}
DatagramPacket dp =
new DatagramPacket(buf, buf.length, destination ,
10000);
//打包到DatagramPacket类型中(DatagramSocket的send()方法接受此类,注意10000是接受地址的端口,不同于自己的端口!)
try {
ds.send(dp); //发送数据
} catch (IOException e) {
}
ds.close();
}
}
接收步骤:
使用 DatagramSocket(int port) 建立socket(套间字)服务。(我们注意到此服务即可以接收,又可以发送),port指定监视接受端口。
定义一个数据包(DatagramPacket),储存接收到的数据,使用其中的方法提取传送的内容
通过DatagramSocket 的receive方法将接受到的数据存入上面定义的包中
使用DatagramPacket的方法,提取数据。
关闭资源。
[java]
view plaincopyprint?
import java.net.*;
public class Rec {
public static
void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(10000);
//定义服务,监视端口上面的发送端口,注意不是send本身端口
byte[] buf =
new byte[1024];//接受内容的大小,注意不要溢出
DatagramPacket dp = new DatagramPacket(buf,0,buf.length);//定义一个接收的包
ds.receive(dp);//将接受内容封装到包中
String data = new String(dp.getData(),
0, dp.getLength());//利用getData()方法取出内容
System.out.println(data);//打印内容
ds.close();//关闭资源
}
}
发送步骤:
使用 DatagramSocket(int port) 建立socket(套间字)服务。
将数据打包到DatagramPacket中去
通过socket服务发送 (send()方法)
关闭资源
[java]
view plaincopyprint?
import java.io.IOException;
import java.net.*;
public class Send {
public static
void main(String[] args) {
DatagramSocket ds = null;
//建立套间字udpsocket服务
try {
ds = new DatagramSocket(8999);
//实例化套间字,指定自己的port
} catch (SocketException e) {
System.out.println("Cannot open port!");
System.exit(1);
}
byte[] buf=
"Hello, I am sender!".getBytes(); //数据
InetAddress destination = null ;
try {
destination = InetAddress.getByName("192.168.1.5");
//需要发送的地址
} catch (UnknownHostException e) {
System.out.println("Cannot open findhost!");
System.exit(1);
}
DatagramPacket dp =
new DatagramPacket(buf, buf.length, destination ,
10000);
//打包到DatagramPacket类型中(DatagramSocket的send()方法接受此类,注意10000是接受地址的端口,不同于自己的端口!)
try {
ds.send(dp); //发送数据
} catch (IOException e) {
}
ds.close();
}
}
import java.io.IOException; import java.net.*; public class Send { public static void main(String[] args) { DatagramSocket ds = null; //建立套间字udpsocket服务 try { ds = new DatagramSocket(8999); //实例化套间字,指定自己的port } catch (SocketException e) { System.out.println("Cannot open port!"); System.exit(1); } byte[] buf= "Hello, I am sender!".getBytes(); //数据 InetAddress destination = null ; try { destination = InetAddress.getByName("192.168.1.5"); //需要发送的地址 } catch (UnknownHostException e) { System.out.println("Cannot open findhost!"); System.exit(1); } DatagramPacket dp = new DatagramPacket(buf, buf.length, destination , 10000); //打包到DatagramPacket类型中(DatagramSocket的send()方法接受此类,注意10000是接受地址的端口,不同于自己的端口!) try { ds.send(dp); //发送数据 } catch (IOException e) { } ds.close(); } }
接收步骤:
使用 DatagramSocket(int port) 建立socket(套间字)服务。(我们注意到此服务即可以接收,又可以发送),port指定监视接受端口。
定义一个数据包(DatagramPacket),储存接收到的数据,使用其中的方法提取传送的内容
通过DatagramSocket 的receive方法将接受到的数据存入上面定义的包中
使用DatagramPacket的方法,提取数据。
关闭资源。
[java]
view plaincopyprint?
import java.net.*;
public class Rec {
public static
void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(10000);
//定义服务,监视端口上面的发送端口,注意不是send本身端口
byte[] buf =
new byte[1024];//接受内容的大小,注意不要溢出
DatagramPacket dp = new DatagramPacket(buf,0,buf.length);//定义一个接收的包
ds.receive(dp);//将接受内容封装到包中
String data = new String(dp.getData(),
0, dp.getLength());//利用getData()方法取出内容
System.out.println(data);//打印内容
ds.close();//关闭资源
}
}
import java.net.*; public class Rec { public static void main(String[] args) throws Exception { DatagramSocket ds = new DatagramSocket(10000); //定义服务,监视端口上面的发送端口,注意不是send本身端口 byte[] buf = new byte[1024];//接受内容的大小,注意不要溢出 DatagramPacket dp = new DatagramPacket(buf,0,buf.length);//定义一个接收的包 ds.receive(dp);//将接受内容封装到包中 String data = new String(dp.getData(), 0, dp.getLength());//利用getData()方法取出内容 System.out.println(data);//打印内容 ds.close();//关闭资源 } }
相关文章推荐
- JAVA中UDP 接受与发送数据的初步步骤
- JAVA中UDP 接受与发送数据的初步步骤
- Java中通过UDP协议发送和接受数据
- UDP java接受数据与发送数据
- JAVA中UDP 接受与发送数据
- Java中通过UDP协议发送和接受数据
- Java中通过UDP协议发送和接受数据
- Java基础知识强化之网络编程笔记04:UDP之发送端的数据来自于键盘录入案例
- Java_网络编程_使用UDP协议发送及接收数据
- Java网络编程 UDP发送与接收数据
- java中socket发送数据接收数据(udp)
- java 实现tcp与udp读取与发送数据
- java模拟http发送请求与接受返回数据
- Java 组播发送数据和接受数据实例
- android发送UDP广播及在PC上接受广播数据
- JAVA-UDP初步:数据交换示例
- 网络编程—UDP协议传输接受与发送数据
- iOS客户端发送json数据,java(servlet)服务器接受json数据
- C#用UDP同步发送数据和接受数据
- JAVA---udp数据的发送与接收