1.什么是UDP,它的优缺点是什么?
UDP:用户数据报协议[无连接的协议] 缺点:不能保证数据的安全与可靠。 优点:消耗小,效率高
2.建立基于UDP的网络通讯程序需要使用的类和常用方法
基于UDP的网络通讯程序是不区分客户端和服务器。 java.net.DatagramPacket---该类表示数据报包。【打包被发送的数据/接收发来的数据】 构造方法: DatagramPacket(byte[]?buf, int?length)? 构造一个 DatagramPacket用于接收长度的数据包length。【接收发来的数据的数据包】 DatagramPacket(byte[]?buf, int?length, InetAddress?address, int?port)? 构造用于发送长度的分组的数据报包 length指定主机上到指定的端口号。 【打包被发送的数据】
实例方法: byte[]?? ?getData() 返回数据缓冲区。? int?? ?getLength() 返回要发送的数据的长度或接收到的数据的长度。? InetAddress?? ?getAddress() 返回该数据报发送或接收数据报的计算机的IP地址。 int?? ?getPort() 返回发送数据报的远程主机上的端口号,或从中接收数据报的端口号。
java.net.DatagramSocket 此类表示用于发送和接收数据报数据包的套接字。 构造方法 DatagramSocket() 构造数据报套接字并将其绑定到本地主机上的任何可用端口。【发包】? DatagramSocket(int?port) 构造数据报套接字并将其绑定到本地主机上的指定端口。【收包】
实例方法 void?? ?send(DatagramPacket?p) 从此套接字发送数据报包。? void?? ?receive(DatagramPacket?p) 从此套接字接收数据报包。? void?? ?close() 关闭此数据报套接字。?
3.基于UDP协议的通信程序创建步骤:
发送方步骤: 1.将需要发送的数据通过 DatagramPacket(byte[]?buf, int?length, InetAddress?address, int?port) 构造方法打包好 2.通过DatagramSocket()的构造方法创建DatagramSocket对象。 3.DatagramSocket对象的send方法将打包好的数据报包,发送。 4.关闭DatagramSocket。
接收方步骤 1.通过DatagramSocket(int?port)创建接收数据的DatagramSocket对象。 2.通过DatagramPacket(byte[]?buf, int?length) 创建一个空的数据报包。 3.通过调用接收数据的DatagramSocket对象的receive方法接收数据报包到空的数据报包。 4.解析数据报包 5.关闭DatagramSocket
实例1:客户端向服务器发送数据,服务器接收客户端发送来的数据
package com.wangxing.udpdemo1;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPClient1 {
public static void main(String[] args)throws Exception {
BufferedReader reader=null;
String info=null;
DatagramPacket fasongbao=null;
InetAddress address=null;
int port=3000;
DatagramSocket sendSocket=null;
reader=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入需要发送的数据:");
info=reader.readLine();
// DatagramPacket(byte[]?buf, int?length, InetAddress?address, int?port)
byte databyte[]=info.getBytes();
address=InetAddress.getLocalHost();
fasongbao=new DatagramPacket(databyte,databyte.length,address,port);
//DatagramSocket()
sendSocket=new DatagramSocket();
//void send(DatagramPacket?p) 从此套接字发送数据报包
sendSocket.send(fasongbao);
//void close() 关闭此数据报套接字。
sendSocket.close();
reader.close();
}
}
package com.wangxing.udpdemo1;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPServer1 {
public static void main(String[] args)throws Exception {
DatagramSocket jieshouSocket=null;
int port=3000;
DatagramPacket jieshoubao=null;
String info=null;
//DatagramSocket(int?port)
jieshouSocket=new DatagramSocket(port);
//DatagramPacket(byte[]?buf, int?length)
byte databyte[]=new byte[1024];
jieshoubao=new DatagramPacket(databyte,databyte.length);
jieshouSocket.receive(jieshoubao);
//拆数据包
//byte[] getData()
byte shoudata[]=jieshoubao.getData();
//int getLength()
int length=jieshoubao.getLength();
//InetAddress getAddress()
String fasongfangip=jieshoubao.getAddress().getHostAddress();
//int getPort()
int fasongfangport=jieshoubao.getPort();
info=new String(shoudata,0,length);
System.out.println("接收来自"+fasongfangip+"从"+fasongfangport+"发出的数据是:"+info);
jieshouSocket.close();
}
}
实例2:客户端向服务器持续发送数据,服务器持续接收客户端发送来的数据
package com.wangxing.udpdemo2;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPClient1 {
public static void main(String[] args)throws Exception {
BufferedReader reader=null;
String info=null;
DatagramPacket fasongbao=null;
InetAddress address=null;
int port=3000;
DatagramSocket sendSocket=null;
boolean flag=true;
reader = new BufferedReader(new InputStreamReader(System.in));
address = InetAddress.getLocalHost();
//DatagramSocket()
sendSocket = new DatagramSocket();
while(flag) {
System.out.println("请输入需要发送的数据:");
info = reader.readLine();
if(info.equals("bey")){
flag=false;
System.out.println("关闭退出");
}
// DatagramPacket(byte[]?buf, int?length, InetAddress?address, int?port)
byte databyte[] = info.getBytes();
fasongbao = new DatagramPacket(databyte, databyte.length, address, port);
//void send(DatagramPacket?p) 从此套接字发送数据报包
sendSocket.send(fasongbao);
}
//void close() 关闭此数据报套接字。
sendSocket.close();
reader.close();
}
}
package com.wangxing.udpdemo2;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPServer1 {
public static void main(String[] args)throws Exception {
DatagramSocket jieshouSocket=null;
int port=3000;
DatagramPacket jieshoubao=null;
String info=null;
boolean flag=true;
//DatagramSocket(int?port)
jieshouSocket = new DatagramSocket(port);
//DatagramPacket(byte[]?buf, int?length)
byte databyte[] = new byte[1024];
jieshoubao = new DatagramPacket(databyte, databyte.length);
while(flag) {
jieshouSocket.receive(jieshoubao);
//拆数据包
//byte[] getData()
byte shoudata[] = jieshoubao.getData();
//int getLength()
int length = jieshoubao.getLength();
//InetAddress getAddress()
String fasongfangip = jieshoubao.getAddress().getHostAddress();
//int getPort()
int fasongfangport = jieshoubao.getPort();
info = new String(shoudata, 0, length);
if(info.equals("bey")){
flag=false;
System.out.println("关闭退出");
}
System.out.println("接收来自" + fasongfangip + "从" + fasongfangport + "发出的数据是:" + info);
}
jieshouSocket.close();
}
}
实例3:客户端向服务器持续发送并接收数据,服务器持续接收客户端发送并接收数据。
package com.wangxing.udpdemo3;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPclient {
public static void main(String[] args)throws Exception {
BufferedReader bufferedReader=null;
String info=null;
DatagramPacket fasongbao=null;
InetAddress serveradsress=null;
int serverport=3000;
DatagramSocket sendsocket=null;
DatagramPacket jeishoubao=null;
byte jieshoubyte[]=null;
DatagramSocket jieshousocket=null;
int port=4000;
String msg=null;
boolean flag=true;
bufferedReader=new BufferedReader(new InputStreamReader(System.in));
serveradsress=InetAddress.getLocalHost();
sendsocket=new DatagramSocket();
jieshoubyte=new byte[1024];
jeishoubao=new DatagramPacket(jieshoubyte,jieshoubyte.length);
jieshousocket=new DatagramSocket(port);
while (flag) {
System.out.println("请输入需要发送的数据:");
info = bufferedReader.readLine();
byte fasongbyte[]=info.getBytes();
fasongbao=new DatagramPacket(fasongbyte,fasongbyte.length,serveradsress,serverport);
sendsocket.send(fasongbao);
if(info.equals("886")){
flag=false;
System.out.println("退出程序");
}else{
//接收数据
jieshousocket.receive(jeishoubao);
//拆数据
msg=new String(jeishoubao.getData(),0,jeishoubao.getLength());
System.out.println("接收来自服务器的数据是:"+msg);
}
}
jieshousocket.close();
sendsocket.close();
bufferedReader.close();
}
}
package com.wangxing.udpdemo3;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPserver {
public static void main(String[] args) throws Exception{
DatagramPacket jeishoubao=null;
byte jieshoubyte[]=null;
DatagramSocket jieshousocket=null;
int port=3000;
String msg=null;
DatagramPacket fasongbao=null;
InetAddress clientadsress=null;
int clientport=4000;
DatagramSocket sendsocket=null;
boolean flag=true;
jieshoubyte=new byte[1024];
jeishoubao=new DatagramPacket(jieshoubyte,jieshoubyte.length);
jieshousocket=new DatagramSocket(port);
System.out.println("服务器启动");
clientadsress=InetAddress.getLocalHost();
sendsocket=new DatagramSocket();
while(flag){
jieshousocket.receive(jeishoubao);
msg=new String(jeishoubao.getData(),0,jeishoubao.getLength());
System.out.println("接收到来自客户端的数据是:"+msg);
if(msg.equals("886")){
flag=false;
System.out.println("退出程序");
}else{
msg="server-"+msg;
byte sendbyte[]=msg.getBytes();
fasongbao=new DatagramPacket(sendbyte,sendbyte.length,clientadsress,clientport);
sendsocket.send(fasongbao);
}
}
sendsocket.close();
jieshousocket.close();
}
}
|