网络编程之UDP编程

Posted luojack

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了网络编程之UDP编程相关的知识,希望对你有一定的参考价值。

UDP编程中,如果要完成通信,通信双方必须要创建DatagramSocket,通过DatagramSocket完成通信。

 

数据报包用来实现无连接包投递服务。每条报文仅根据该包中包含的信息从一台机器路由到另一台机器

 

UDP步骤:

[1] 创建一个DatagramSocket用于表示发送端,通过send方法发送数据报

[2] 创建一个DatagramSocket用于表示接收端,通过receive方法发送数据报

 

需求:完成一次单向的UDP通信。

发送端:

 

public class UDPDemo1 {

	public static void main(String[] args) {
			System.out.println("发送端开始发送信息..."); 
			try {
				//1.创建Socket,指定端口号
				DatagramSocket datagramSocket=new DatagramSocket(9000);
				
				//2.把发送的信息打包
				String string="IG 牛逼!";
				byte[] bytes = string.getBytes("UTF-8");
				DatagramPacket datagramPacket=new DatagramPacket(bytes, bytes.length, InetAddress.getLocalHost(),8000);
				
				//3.通过socket的send方法,把信息传出
				datagramSocket.send(datagramPacket);
				
				//4.关闭socket
				datagramSocket.close();
				
			} catch (SocketException e) {
				e.printStackTrace();
			} catch (UnknownHostException e) {				
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {				
				e.printStackTrace();
			} catch (IOException e) {			
				e.printStackTrace();
			}
			
	}

}

 

  接收端:

 

public class UDPDemo01 {

	public static void main(String[] args) {
		System.out.println("接收端开始接受信息...");	
		try {
			DatagramSocket datagramSocket=new DatagramSocket(8000);
			
			byte[] buf=new byte[1024];
				DatagramPacket datagramPacket=new DatagramPacket(buf, buf.length);
				
				datagramSocket.receive(datagramPacket);
				
				String string=new String(buf, 0, datagramPacket.getLength(), "UTF-8");
				
				System.out.println(string);
				
				datagramSocket.close();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

	}

}

  

 

需求:实现双向通信。

这一端:

public class UDPdouble {

	public static void main(String[] args) {
		System.out.println("创建连接...");
		Scanner scanner=new Scanner(System.in);
		DatagramSocket socket=null;
		try {
			socket=new DatagramSocket(8002);
			while(true){
		
			//1.A向B发送信息
						
			System.out.print("A说:");
			String Astring = scanner.nextLine();		
			byte[] buf = Astring.getBytes("UTF-8");			
			DatagramPacket datagramPacket=new DatagramPacket(buf, buf.length,
					InetAddress.getLocalHost(),9002);
			socket.send(datagramPacket);
			
			
			//2.A接受B的信息			
			
			byte[] buf2=new byte[1024];
			DatagramPacket datagramPacket2=new DatagramPacket(buf2, buf2.length);
			socket.receive(datagramPacket2);
			String bstring=new String(buf2, 0, datagramPacket2.getLength(), "UTF-8");
			System.out.println("B说:"+bstring);
			
			
			
			}
		
			
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			socket.close();
		}

	}

}

  

另一端:

public class UDPdouble {

	public static void main(String[] args)  {
		System.out.println("创建连接...");
		Scanner scanner=new Scanner(System.in);		
		DatagramSocket socket=null;
		try {
			socket=new DatagramSocket(9002);
			while(true){
			//1.B接受A的信息
			byte[] buf=new byte[1024];
			DatagramPacket datagramPacket=new DatagramPacket(buf, buf.length);
			socket.receive(datagramPacket);
			String Astring =new String(buf, 0, datagramPacket.getLength(), "UTF-8");
			System.out.println("A说:"+Astring);
			
			
			//2.B向A发送信息
			System.out.print("B说:");
			String bstring=scanner.nextLine();
			byte[] buf2=bstring.getBytes("UTF-8");
			DatagramPacket datagramPacket2=new DatagramPacket(buf2, 0,buf2.length,
					datagramPacket.getAddress(),datagramPacket.getPort() );
			socket.send(datagramPacket2);
			
			
			
			}	
			
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			socket.close();
		}
		
		
		

	}

}

  注意:运行中,若需要修改代码,请先停止程序,即停止使用端口。否则,修改完毕后,重新启动会报错,因为上次的端口还在使用。

以上是关于网络编程之UDP编程的主要内容,如果未能解决你的问题,请参考以下文章

面向面试编程代码片段之GC

Python之UDP编程

socket编程之TCP/UDP

Java网络编程之UDP网络编程

自测之Lesson15:TCP&UDP网络编程

网络编程之UDP编程