java udp编程_java-udp编程

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

客户端发送消息

a92d43df5ec68e1e2f6598cb3b880793.png

服务端接收消息

12eaf450cd05b9ec63ebccfba5e74532.png

多线程进行聊天

一、客户端

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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值