基于UDP协议的通信程序
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)
? ? ? ? 构造用于发送长度的分组数据包lenght指定主机上到指定的端口号【打包被发送的数据】
实例方法:
? ? ? ? byte[]? ? ? ? getData()返回数据缓冲区
? ? ? ? Int? ? ? ? ? ? ?getLength()返回要发送的数据的长度或接收到的数据的长度
? ? ? ? InetAddrss? ? ? ? getAddress()返回该数据报发送或接收数据报的计算机的IP地址
? ? ? ? Int? ? ? ? ? ? ?getProt()返回发送数据报的远程主机上的端口号,或从中接收数据报的端口号
? ? ? ? java.net.DatagramSocket此类表示用于发送和接收数据报数据包的套接字
构造方法:
? ? ? ? DatagramSocket()构造数据报套接字并将其绑定到本地主机上的任何可用端口【发包】?
????????DatagramSocket(int prot)构造数据套接字并将其绑定到本地主机上的指定端口【收包】
实例方法:
? ? ? ? void? ? ? ? send(DatagramPackt p)从此套接字发送数据报包
? ? ? ? void? ? ? ? ?receive(DatagramPacket p)从此套接字接收数据报包
? ? ? ? void? ? ? ? close()关闭此数据报套接字
3.基于UDP协议的通信程序创建步骤:
发送方步骤:
1.将需要发送的数据通过DatagramPacket(byte[] buf,int length,InerAddress 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.test2;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class ClentUDP {
public static void main(String[] args) throws Exception {
//建立一个Socket
DatagramSocket socket=new DatagramSocket();
System.out.println("请输入要发送的数据");
BufferedReader buff=new BufferedReader(new InputStreamReader(System.in));
String msg=buff.readLine();
byte data[]=msg.getBytes();
int len=data.length;
InetAddress local=InetAddress.getLocalHost();
int port=3001;
//建个包
DatagramPacket packet=new DatagramPacket(data, 0, len,local, port);
//发送包
socket.send(packet);
socket.close();
}
}
package com.test2;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class ServerUDP {
public static void main(String[] args) throws Exception {
//创建接收数据的Socket对象
//开放端口
DatagramSocket socket=new DatagramSocket(3001);
//创建一个空的包
byte data[]=new byte[1024];
DatagramPacket packet=new DatagramPacket(data,data.length);
//接收数据
socket.receive(packet);
//解析数据
byte[] data1=packet.getData();
int len=packet.getLength();
//返回数据报发送的的计算机地址
String msg=new String(data,0,len);
InetAddress address1=packet.getAddress();
int port=packet.getPort();
System.out.println("接收到来自"+address1+";"+port+"的信息"+msg);
}
}
实例2:客户端向服务器持续发送数据,服务器持续接收客户端发送来的数据
package com.test1;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class ClentUDP {
public static void main(String[] args) throws Exception {
//建立一个Socket
DatagramSocket socket=new DatagramSocket();
BufferedReader buff=new BufferedReader(new InputStreamReader(System.in));
InetAddress local=InetAddress.getLocalHost();
int port=3001;
//建个包
boolean flag=true;
while(flag){
System.out.println("请输入要发送的数据");
String msg=buff.readLine();
byte data[]=msg.getBytes();
int len=data.length;
DatagramPacket packet=new DatagramPacket(data, 0, len,local, port);
socket.send(packet);
if(msg.equals("886")){
flag=false;
}
}
//发送包
socket.close();
}
}
package com.test1;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class ServerUDP {
public static void main(String[] args) throws Exception {
//创建接收数据的Socket对象
//开放端口
DatagramSocket socket=new DatagramSocket(3001);
//创建一个空的包
byte data[]=new byte[1024];
DatagramPacket packet=new DatagramPacket(data,data.length);
boolean flag=true;
while(flag){
//接收数据
socket.receive(packet);
//解析数据
byte[] data1=packet.getData();
int len=packet.getLength();
//返回数据报发送的的计算机地址
String msg=new String(data,0,len);
InetAddress address1=packet.getAddress();
int port=packet.getPort();
System.out.println("接收到来自"+address1+";"+port+"的信息"+msg);
if(msg.equals("886")){
flag=false;
}
}
socket.close();
}
}
实例3:客户端向服务器持续发送数据同时接收服务器发送来的的,服务器持续接收客户端发送来的数据同时发送得到的数据
package com.test3;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class ClentUDP {
public static void main(String[] args) throws Exception {
int port=3001;
int port2=3002;
//建立一个Socket
DatagramSocket socket=new DatagramSocket();
BufferedReader buff=new BufferedReader(new InputStreamReader(System.in));
InetAddress local=InetAddress.getLocalHost();
//开放端口
DatagramSocket socket2=new DatagramSocket(port2);
//定义一个接收的包
byte data2[]=new byte[1024];
DatagramPacket packet2=new DatagramPacket(data2,data2.length);
//建个包
boolean flag=true;
while(flag){
System.out.println("请输入要发送的数据");
String msg=buff.readLine();
byte data[]=msg.getBytes();
int len=data.length;
DatagramPacket packet1=new DatagramPacket(data, 0, len,local, port);
socket.send(packet1);
//接收数据
socket2.receive(packet2);
//解析数据
byte[] data1=packet2.getData();
int len2=packet2.getLength();
String msg2=new String(data,0,len);
InetAddress address1=packet2.getAddress();
int receiveport=packet2.getPort();
System.out.println("接收到来自server"+address1+";"+receiveport+"的信息"+msg);
if(msg.equals("886")){
flag=false;
}
}
//发送包
socket.close();
socket2.close();
}
}
package com.test3;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class ServerUDP {
public static void main(String[] args) throws Exception {
int port=3001;
int port2=3002;
//创建接收数据的Socket对象
DatagramSocket socket=new DatagramSocket();
//开放端口
DatagramSocket socket2=new DatagramSocket(port);
//创建一个空的包
byte data[]=new byte[1024];
DatagramPacket packet=new DatagramPacket(data,data.length);
InetAddress local=InetAddress.getLocalHost();
boolean flag=true;
while(flag){
//接收数据
socket2.receive(packet);
//解析数据
byte[] data1=packet.getData();
int len=packet.getLength();
//返回数据报发送的的计算机地址
String msg=new String(data,0,len);
InetAddress address1=packet.getAddress();
int port1=packet.getPort();
System.out.println("接收到来自"+address1+";"+port1+"的信息"+msg);
//发送包
String msg1="server"+msg;
byte data2[]=msg1.getBytes();
int len2=data.length;
DatagramPacket packet1=new DatagramPacket(data, 0, len,local, port2);
socket.send(packet1);
if(msg.equals("886")){
flag=false;
}
}
socket.close();
socket2.close();
}
}
实例4:多个客户端客户端向服务器持续发送数据同时接收服务器发送来的的,服务器持续接收客户端发送来的数据同时向客户端发送得到的数据
package com.test4;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class ClientMain {
public static void main(String[] args) throws Exception{
//创建发送Socket对象
DatagramSocket send=new DatagramSocket();
//键盘输入
BufferedReader buff=new BufferedReader(new InputStreamReader(System.in));
//持续发送
boolean flag=true;
while(flag){
System.out.println("请输入要发送的数据");
String msg=buff.readLine();
byte buf[]=msg.getBytes();
int len=buf.length;
InetAddress address=InetAddress.getLocalHost();
int port=3000;
//创建需要发送的包
DatagramPacket senpacket=new DatagramPacket(buf,len,address,port);
//发送包
send.send(senpacket);
int jieshou=send.getLocalPort();
//创建接收的包
byte data[]=new byte[1024];
DatagramPacket servicepacket=new DatagramPacket(data,data.length);
//接收发送来的数据
send.receive(servicepacket);
//解析数据
byte[] servicedata =servicepacket.getData();
String servicemsg=new String(servicedata,0,servicedata.length);
System.out.println("接收到来自服务器的信息是---"+servicemsg);
if(msg.equals("886")){
flag=false;
}
}
send.close();
}
}
package com.test4;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class ServerThread implements Runnable{
boolean flag=true;
int port=3000;
@Override
public void run() {
try {
//创建Socket对象
DatagramSocket receive=new DatagramSocket(port);
while(flag){
//创建接收的包
byte buf[]=new byte[1024];
DatagramPacket receivepacket=new DatagramPacket(buf, buf.length);
receive.receive(receivepacket);
byte[] data=receivepacket.getData();
String msg=new String(data,0,data.length);
int receiveport=receivepacket.getPort();
InetAddress receiveaddress= receivepacket.getAddress();
System.out.println("接收到来自/"+receiveaddress+":"+receiveport+"的信息"+msg);
//创建发送Socket对象
DatagramSocket sendsocket=new DatagramSocket();
InetAddress address=InetAddress.getLocalHost();
//创建要发送的包
DatagramPacket sendpacket=new DatagramPacket(data,data.length,address,receiveport);
sendsocket.send(sendpacket);
}
receive.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
package com.test4;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class ServerMain {
public static void main(String[] args) throws Exception {
System.out.println("等待客户端响应..");
ServerThread receive =new ServerThread();
Thread receivethread=new Thread(receive);
receivethread.start();
}
}
|