我以前经常写的是基于TCP的网络编程,由于TCP建立连接鼻血要经过三次握手连接,服务器端需要阻塞式等待客户端的连接。而UDP则是可以直接向目的地址的目的端口上发送数据包,由于它只负责发送出去就好,不管对方是否正确接受到与否,所以当网络性能不好时它容易出现丢包的问题。(注意:UDP是基于数据报为单位进行传输的,而TCP是一种基于流进行传输的)
但是UDP很好的模拟了我们呢平时聊天的方式,可以很好的实现连续多次发送和接受,也就是简单的QQ聊天的功能。
现在来简要介绍Java中有关UDP编程相关的类:
一个是DatagramPacket,主要用来将数据字节填充到UDP数据报中,用来解包接受数据的,用来收和发UDP数据报。一个是DatagramSocket主要用来是建立客户端和服务器端,用来接受和发送数据报数据的。
(1)DatagramPacket类
UDP首部向IP首部添加了8个字节,包含了源端口和目标端口,IP首部以后内容的长度和校验和,最多有65507个字节数。UDP所使用的端口和TCP使用的端口不一样的,是一个final类。
对于接受数据,将接受到的数据存储到DatagramPacket,然后从该对象中读取数据。
对于发送数据,将发送的数据先存到DatagramPacket中,然后将该对象发送。
以下是我写的简单的一个聊天程序:
Send_Thread.java
package Chat_UDP; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.InetSocketAddress; import java.util.Scanner; public class Send_Thread extends Thread{ //发送的socket端 private DatagramSocket sender = null; //待发送的目标地址 private InetSocketAddress address = null; //从键盘输入 Scanner scan = new Scanner(System.in); public Send_Thread(DatagramSocket sender,InetSocketAddress address) { this.sender = sender; this.address = address; } @Override public void run() { // TODO Auto-generated method stub try { while(true) { //输入待发送的内容 String input = scan.nextLine(); if(input.equals("exit")) break; byte[] data = null; data = input.getBytes("UTF-8"); //创建UDP数据报 DatagramPacket pack = new DatagramPacket(data, data.length,address); sender.send(pack); } System.out.println("Exit!"); }catch(IOException e) { System.out.println("IOException"); } } }
Receive_Thread.java
package Chat_UDP; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class Receive_Thread extends Thread { private static final int MAX_RECEIVE_BUFFER = 1024; private DatagramSocket server; private DatagramPacket packet; byte[] buffer = new byte[MAX_RECEIVE_BUFFER]; public Receive_Thread(DatagramSocket server) { this.server = server; packet = new DatagramPacket(buffer, buffer.length); } @Override public void run() { // TODO Auto-generated method stub try { while(true) { //接收数据包 server.receive(packet); String s = new String(packet.getData(),packet.getOffset(),packet.getLength(),"UTF-8"); System.out.println(packet.getAddress()+" at port "+packet.getPort()+" Says :"+s); packet.setLength(buffer.length); } } catch(IOException e) { System.out.println("IOException"); } } }
Chat_Client.java
1 package Chat_UDP; 2 3 import java.net.DatagramSocket; 4 import java.net.InetSocketAddress; 5 6 public class Chat_Client { 7 8 private static final int DEST_PORT = 9000; 9 private static final int SEND_PORT = 10000; 10 private static final int RECE_PORT = 8888; 11 private static final String IP = "127.0.0.1"; 12 13 public static void main(String[] args) 14 { 15 try{ 16 Send_Thread send_thread = null; 17 Receive_Thread rece_thread = null; 18 InetSocketAddress address = null; 19 //创建待接受数据包的目的机的端口号和IP地址 20 address = new InetSocketAddress(IP, DEST_PORT); 21 //创建发送的Socket端 22 DatagramSocket sendsocket = new DatagramSocket(SEND_PORT); 23 //创建接受的Socket端 24 DatagramSocket recesocket = new DatagramSocket(RECE_PORT); 25 //发送线程建立 26 send_thread = new Send_Thread(sendsocket, address); 27 //接受线程的建立 28 rece_thread = new Receive_Thread(recesocket); 29 30 send_thread.start(); 31 rece_thread.start(); 32 33 }catch(Exception e) 34 { 35 System.out.println("Exception!"); 36 } 37 38 } 39 40 41 }
Chat_Server.java
1 package Chat_UDP; 2 3 import java.net.DatagramSocket; 4 import java.net.InetSocketAddress; 5 6 public class Chat_Server { 7 8 private static final int DEST_PORT = 8888; 9 private static final int SEND_PORT = 10001; 10 private static final int RECE_PORT = 9000; 11 private static final String IP = "127.0.0.1"; 12 13 public static void main(String[] args) 14 { 15 try{ 16 Send_Thread send_thread = null; 17 Receive_Thread rece_thread = null; 18 InetSocketAddress address = null; 19 //创建待接受数据包的目的机的端口号和IP地址 20 address = new InetSocketAddress(IP, DEST_PORT); 21 //创建发送的Socket端 22 DatagramSocket sendsocket = new DatagramSocket(SEND_PORT); 23 //创建接受的Socket端 24 DatagramSocket recesocket = new DatagramSocket(RECE_PORT); 25 //发送线程建立 26 send_thread = new Send_Thread(sendsocket, address); 27 //接受线程的建立 28 rece_thread = new Receive_Thread(recesocket); 29 send_thread.start(); 30 rece_thread.start(); 31 }catch(Exception e) 32 { 33 System.out.println("Exception!"); 34 } 35 36 } 37 38 }
Java UDP实现聊天功能代码,布布扣,bubuko.com
原文:http://www.cnblogs.com/woshijpf/p/3735684.html