1.什么是UDP,它的优点是什么?
UDP---用户数据报协议[无连接协议]
缺点:不能保证数据的安全与可靠
优点:消耗小,效率高
2.建立基于UDP的网络通讯程序需要使用的类和常用方法
基于UDP的网路通讯程序是不区分客户端和服务器
java.net.DatagramPacket---该类表示数据报包。【打包被发送的数据/接收发来的数据】
构造方法:
DatagramPacket(byte[ ],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 prot)---构造数据报套接字并将其绑定到本地主机上的指定端口[收包]
实例方法:
void? ?send(DatagramPacket p)---从次套接字发送数据包
void? ?receive(DatagramPacket p)---从此套接字接收数据包。
void? ?close()---贯标此数据报套接字。
3.基于UDP协议的通信程序创建步骤
发送发步骤
1.将需要发送的数据通过DatagramPacket(byte[] buf,int length,InetAddress address,int prot)构造方法打包好
2.通过DatagramSocket()的构造方法创建DatagramSocket对象
3.DatagramSocket()对象的send方法将打包好的数据报包发送
4.关闭DatagarmSocket.
接收方步骤
1.通过DatagramSocket(int prot)创建数据的DatagramSock对象
2.通过DatagramPacket(byte[ ] buf,int length)创建一个空数据包
3.通过调用接收数据的DatagramSocket对象的receive方法接收数据报包到空的数据报包。
4.解析数据包
5.关闭DatagramSocket
实例1:客户端向服务器发送数据,服务器接收客户端发送的数据?
package com.wangxing.test1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class ClientSend {
public static void main(String[] args) throws Exception {
BufferedReader buff=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入要发送的内容");
String info=buff.readLine();
byte buf[]=info.getBytes();
int len=buf.length;
//返回包含本地ip地址的InetAddress对象
InetAddress address=InetAddress.getLocalHost();
int port=2000;
// DatagramPacket(byte[]?buf, int?length, InetAddress?address, int?port) 构造方法打包好
DatagramPacket sendpack=new DatagramPacket(buf, len,address,port);
//DatagramSocket() 构造数据报套接字并将其绑定到本地主机上的任何可用端口。【发包】
DatagramSocket sendsock=new DatagramSocket();
//void send(DatagramPacket?p) 从此套接字发送数据报包。
sendsock.send(sendpack);
//void close() 关闭此数据报套接字。
sendsock.close();
}
}
package com.wangxing.test1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.xml.crypto.Data;
public class ServerReceive {
public static void main(String[] args) throws Exception {
int prot=2000;
//DatagramSocket(int?port)创建接收数据的DatagramSocket对象。
DatagramSocket receivesock=new DatagramSocket(prot);
//创建一个空的数据报包。DatagramPacket(byte[]?buf, int?length)
byte buf[]=new byte[1024];
DatagramPacket receiverpack=new DatagramPacket(buf, buf.length);
//void receive(DatagramPacket?p) 从此套接字接收数据报包。
//接收数据
receivesock.receive(receiverpack);
//解析数据
byte data[]=receiverpack.getData();
int len=receiverpack.getLength();
String msg=new String(data,0,len);
String clientip=receiverpack.getAddress().getHostAddress();
int clientsentport=receiverpack.getPort();
System.out.println("接收到来自"+clientip+":"+clientsentport+"的信息:"+msg);
receivesock.close();
}
}
实例2:完成客户端持续向服务器送数据,服务器持续接收到客户端发送来的数据。
package com.wangxing.test2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class ClientSend {
public static void main(String[] args) throws Exception {
String sendmsg=null;
int serverport=2000;
DatagramPacket sendpacket=null;
boolean flag=true;
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
InetAddress serversddress=InetAddress.getLocalHost();
DatagramSocket sendsocket=new DatagramSocket();
while(flag){
System.out.println("请输入需要发送的内容");
sendmsg=reader.readLine();
sendpacket=new DatagramPacket(sendmsg.getBytes(),sendmsg.getBytes().length,serversddress,serverport);
sendsocket.send(sendpacket);
if(sendmsg.equals("exit")){
flag=false;
}
}
sendsocket.close();
}
}
package com.wangxing.test2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.xml.crypto.Data;
public class ServerReceive {
public static void main(String[] args) throws Exception {
DatagramSocket receivesocket=null;
DatagramPacket receivepacket=null;
String receivemsg=null;
InetAddress clientaddress=null;
int serverport=2000;
byte data[]=null;
int len=0;
int clientport=0;
boolean flag=true;
receivesocket=new DatagramSocket(serverport);
byte buf[]=new byte[1024];
receivepacket=new DatagramPacket(buf,buf.length);
while(flag){
receivesocket.receive(receivepacket);
data=receivepacket.getData();
len=receivepacket.getLength();
clientaddress=receivepacket.getAddress();
clientport=receivepacket.getPort();
receivemsg=new String(data,0,len);
System.out.println("服务器接收到来自:"+clientaddress.getHostAddress()+":"+clientport+"的信息是=="+receivemsg);
if(receivemsg.equals("886")){
flag=false;
}
}
receivesocket.close();
}
}
实例3:完成客户端持续向服务器送数据,还能接收服务器返回的信息,服务器持续接收到客户端发送来的数据,还可以向客户端返回信息
package com.wangxing.test3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class ClientSend {
public static void main(String[] args) throws Exception {
String sendmsg=null;
int serverport=2000;
DatagramPacket sendpacket=null;
boolean flag=true;
DatagramSocket receviesock=null;
DatagramPacket receviepacket=null;
String receviemsg=null;
byte data[ ]=null;
int len=0;
int receiveport=3000;
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
InetAddress serversddress=InetAddress.getLocalHost();
DatagramSocket sendsocket=new DatagramSocket();
receviesock=new DatagramSocket(receiveport);
byte buf[]=new byte[1024];
receviepacket=new DatagramPacket(buf, buf.length);
while(flag){
System.out.println("请输入需要发送的内容");
sendmsg=reader.readLine();
sendpacket=new DatagramPacket(sendmsg.getBytes(),sendmsg.getBytes().length,serversddress,serverport);
sendsocket.send(sendpacket);
receviesock.receive(receviepacket);
data=receviepacket.getData();
len=receviepacket.getLength();
receviemsg=new String(data,0,len);
System.out.println("接受到来自服务器的数据:"+receviemsg);
if(sendmsg.equals("exit")){
flag=false;
}
}
sendsocket.close();
receviesock.close();
}
}
package com.wangxing.test3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.xml.crypto.Data;
public class ServerReceive {
public static void main(String[] args) throws Exception {
DatagramSocket receivesocket=null;
DatagramPacket receivepacket=null;
String receivemsg=null;
InetAddress clientaddress=null;
int receiveport=2000;
byte data[]=null;
int len=0;
int clientport=0;
boolean flag=true;
String sendmsg=null;
InetAddress serversddress=null;
int sendport=3000;
DatagramSocket sendsocket=null;
DatagramPacket sendpacket=null;
receivesocket=new DatagramSocket(receiveport);
byte buf[]=new byte[1024];
receivepacket=new DatagramPacket(buf,buf.length);
serversddress=InetAddress.getLocalHost();
sendsocket=new DatagramSocket();
while(flag){
receivesocket.receive(receivepacket);
data=receivepacket.getData();
len=receivepacket.getLength();
clientaddress=receivepacket.getAddress();
clientport=receivepacket.getPort();
receivemsg=new String(data,0,len);
System.out.println("服务器接收到来自:"+clientaddress.getHostAddress()+":"+clientport+"的信息是=="+receivemsg);
sendmsg="server-"+receivemsg;
sendpacket=new DatagramPacket(sendmsg.getBytes(),sendmsg.getBytes().length,serversddress,sendport);
sendsocket.send(sendpacket);
if(receivemsg.equals("exit")){
flag=false;
}
}
sendsocket.close();
receivesocket.close();
}
}
|