TCP/IP UDP都是基于传输层的;而udp发送数据会出现丢包的情况,发送一个数据不管对方接收不接收,发送过去就完事了;
udp的特点:将数据源和目的封装成数据包中,不要建立连接;(DatagramPacket)
每个数据报的大小在限制64K以内
因无连接,是不可靠协议
不需要建立连接,速度快;
下面将编写一代代码展现UDP
一、client 端
public classUDPSocketClient {public static void main(String[] args) throwsThrowable {//send();
keySend();
}private static void keySend() throwsThrowable {try{//1、创建UDP服务
DatagramSocket socket=newDatagramSocket();
BufferedReader reader=new BufferedReader(newInputStreamReader(System.in));
String message=null;while((message=reader.readLine())!=null) {if(message.equals("886"))break;//2、 封装数据包
DatagramPacket send=new DatagramPacket(message.getBytes(),message.getBytes().length,InetAddress.getByName("localhost"), 8088);//阻塞式 3、发送数据
socket.send(send);
}
//4、释放资源
socket.close();
}catch(SocketException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
二、SERVER端
public classUDPSocketServer {public static void main(String[] args) throwsThrowable {//receive();
Keyreceive();
}private static void Keyreceive() throwsThrowable {//1、创建udp socket并指定端口
DatagramSocket socket = new DatagramSocket(8088);while(true) {//2、定义数据 要接收的报文
byte[] bytes = new byte[1024];
DatagramPacket packet= newDatagramPacket(bytes, bytes.length);
socket.receive(packet);//阻塞式
String message=new String(packet.getData(),0,packet.getData().length);
System.out.println("Server收到的消息为:"+message);
}
}
}
三、测试结果;
先把server端启动,然后开启client
客户端发送消息
服务端接收消息
多线程进行聊天
一、客户端
public class UDPSocketClientThread implementsRunnable{privateDatagramSocket datagramSocket;publicUDPSocketClientThread(DatagramSocket datagramSocket) {this.datagramSocket=datagramSocket;
}
@Overridepublic voidrun() {
BufferedReader reader=new BufferedReader(newInputStreamReader(System.in));
String message=null;try{while((message=reader.readLine())!=null) {if(message.equals("886"))break;//2、发送数据
DatagramPacket send=new DatagramPacket(message.getBytes(),message.getBytes().length,InetAddress.getByName("192.168.43.255"), 8088);//阻塞式
this.datagramSocket.send(send);
}
}catch(UnknownHostException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}
}
二、服务端
public class UDPSocketServerThread implementsRunnable{
DatagramSocket socket;publicUDPSocketServerThread(DatagramSocket socket) {this.socket=socket;
}
@Overridepublic voidrun() {while(true) {//2、定义数据 要接收的报文
byte[] bytes = new byte[1024];
DatagramPacket packet= newDatagramPacket(bytes, bytes.length);try{
socket.receive(packet);
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}//阻塞式
String message=new String(packet.getData(),0,packet.getData().length);
InetAddress inetAddress=null;try{
inetAddress=InetAddress.getLocalHost();
System.out.println(inetAddress.getHostAddress()+"的消息为:"+message);
}catch(UnknownHostException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
三、测试运行
public classUDPSocketRun {public static void main(String[] args) throwsThrowable {
DatagramSocket send=newDatagramSocket();
DatagramSocket receive=new DatagramSocket(8088);new Thread(newUDPSocketClientThread(send)).start();new Thread(newUDPSocketServerThread(receive)).start();
}
}