标签:wds cep dna 网络应用 cbo save idg ssr 返回
Java中实现UDP协议的两个类,分别是DatagramPacket数据包类以及DatagramSocket套接字类。
UDP的套接字DatagramSocket相比于Socket、ServerSocket来说,是一个非常简单的概念,没有连接的含义。套接字只需要知道侦听和发送数据包的本地端口即可。
也就是在TCP协议中庸Socket类和ServerSocket类进行功能划分,UDP协议中只用一个数据包套接字DatagramSocket发送和接受数据即可。
发送和接收数据、地址和端口都封装在数据包类DatagramPacket中。
public DatagramPacket(byte[] buf,int length); /*offset为buf缓冲区的偏移量*/ public DatagramPacket(byte[] buf,int offset,int length); /*address表示目的地址 port表示目的端口*/ public DatagramPacket(byte[] buf,int offset,int length,InetAdress address, int port)
/*返回接受数据包的主机IP地址,通常用于获取接受到数据包的发送主机IP*/ public InetAdress getAdress(); /*返回数据包的主机端口号*/ public int getPort(); /*返回被接收或发送的缓冲区数据*/ public byte[] getData(); /*设置返回发出数据包的主机IP地址*/ public void setAdress(InetAdress iaddr); /*设置要将此数据包发往远程主机上的端口号*/ public void setPort(int iport);
对于数据包套接字DatagramSocket来说,它只是发送或接收数据包的端口,不需要考虑链接。因此在客户端和服务器端都需要建立一个DatagramSocket对象,然后通过receive()方法接受数据包,通过send()方法发送数据包。
客户端发送1-26数字给服务器端;
服务器端返回对应的A-Z给客户端。
package web; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.util.Scanner; public class UDPCilent { public static void main(String[] args) { try { /*UDP Cilent SEND*/ DatagramSocket socket=new DatagramSocket(); System.out.println("UDP Cilent Start..."); System.out.println("Enter 1 to 26 ,Server Will Return U A to Z:"); String info=new Scanner(System.in).next(); socket.send(new DatagramPacket(info.getBytes(), info.length(),InetAddress.getByName("127.0.0.1"),3000)); System.out.println("UDP Cilent Send Message..."); byte[] rec=new byte[1024]; DatagramPacket recivepacket=new DatagramPacket(rec, rec.length); socket.receive(recivepacket); System.out.println("Receive"); String reciveData=new String(recivepacket.getData(),0,recivepacket.getLength()); System.out.println("Receive from"+recivepacket.getAddress().getHostAddress()+":"+reciveData); socket.close(); System.out.println("UDP Cilent close..."); } catch (Exception e) { // TODO: handle exception } } }
package web; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.Socket; public class UDPServer { public static void main(String[] args) { try { /*UDP Receive Message*/ DatagramSocket socket=new DatagramSocket(3000); byte []bf=new byte[1024]; DatagramPacket packet=new DatagramPacket(bf, 1024); System.out.println("UDP Server Start... Wait to Receive Message..."); socket.receive(packet); String info=new String(packet.getData(),0,packet.getLength())+"from"+packet.getAddress().getHostAddress()+":"+packet.getPort(); System.out.println(info); /*feedback*/ InetAddress address= packet.getAddress(); int port=packet.getPort(); String str=new String(packet.getData(),0,packet.getLength()); int renum=Integer.parseInt(str); int feedback=‘A‘; for(int i=1;i<=26;i++) { if(renum==i){ feedback=feedback+(i-1); } } char back=(char) feedback; String fbString=back+""; byte[] fstr=fbString.getBytes(); DatagramPacket feed= new DatagramPacket(fstr,fstr.length,address,port); socket.send(feed); socket.close(); System.out.println("UDP Server close..."); } catch (Exception e) { // TODO: handle exception } } }
标签:wds cep dna 网络应用 cbo save idg ssr 返回
原文地址:http://www.cnblogs.com/rekent/p/6934757.html