java 中模拟UDP传输的发送端和接收端实例详解

公司主营业务:成都网站设计、网站建设、外贸网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出禹王台免费做网站回馈大家。
一、创建UDP传输的发送端
1、建立UDP的Socket服务;
2、将要发送的数据封装到数据包中;
3、通过UDP的Socket服务将数据包发送出去;
4、关闭Socket服务。
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPSend {
 public static void main(String[] args) throws IOException {
  System.out.println("发送端启动......");
  // 1、创建UDP的Socket,使用DatagramSocket对象
  DatagramSocket ds = new DatagramSocket();
  // 2、将要发送的数据封装到数据包中
  String str = "UDP传输演示:I'm coming!";
  byte[] buf = str.getBytes(); //使用DatagramPacket将数据封装到该对象的包中
  DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.191.1"), 10000);
  // 3、通过UDP的Socket服务将数据包发送出去,使用send方法
  ds.send(dp);
  // 4、关闭Socket服务
  ds.close();
 }
}
二、创建UDP传输的接收端
1、建立UDP的Socket服务,因为要接收数据,所以必须明确一个端口号;
2、创建数据包,用于存储接收到的数据,方便用数据包对象的方法解析这些数据;
3、使用UDP的Socket服务的receive方法接收数据并存储到数据包中;
4、通过数据包的方法解析这些数据;
5、关闭Socket服务。
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPReceive {
 public static void main(String[] args) throws IOException {
  System.out.println("接收端启动......");
  // 1、建立UDP的Socket服务
  DatagramSocket ds = new DatagramSocket(10000);
  // 2、创建数据包
  byte[] buf = new byte[1024];
  DatagramPacket dp = new DatagramPacket(buf, buf.length);
  // 3、使用接收方法将数据存储到数据包中
  ds.receive(dp); // 该方法为阻塞式的方法
  // 4、通过数据包对象的方法解析这些数据,例如:地址、端口、数据内容等
  String ip = dp.getAddress().getHostAddress();
  int port = dp.getPort();
  String text = new String(dp.getData(), 0, dp.getLength());
  System.out.println(ip + ":" + port + ":" + text);
  // 5、关闭Socket服务
  ds.close();
 }
}
三、优化UDP传输的发送端和接收端
由于在前两部分中,我们一次只能发送(或接收)一条消息,然后就关闭服务啦!因此如果我们想要发送多条消息,则需要不断的在发送端修改发送的内容,并且还需要重新启动服务器,比较麻烦。为了克服以上的缺点,我们可以对其进行优化,即:
1、在发送端,创建BufferedReader,从键盘录入内容;
2、在接收端,添加while(ture)循环,不断的循环接收内容。
/**
*优化UDP传输的发送端
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPSend {
 public static void main(String[] args) throws IOException {
  System.out.println("发送端启动......");
  // 创建UDP的Socket,使用DatagramSocket对象
  DatagramSocket ds = new DatagramSocket();
  BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
  String line = null;
  while ((line = bufr.readLine()) != null) {
   // 使用DatagramPacket将数据封装到该对象的包中
   byte[] buf = line.getBytes();
   DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.191.1"), 10000);
   // 通过UDP的Socket服务将数据包发送出去,使用send方法
   ds.send(dp);
   // 如果输入信息为over,则结束循环
   if ("over".equals(line))
    break;
  }
  // 关闭Socket服务
  ds.close();
 }
}
/**
*优化UDP传输的接收端
*/
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPReceive {
 public static void main(String[] args) throws IOException {
  System.out.println("接收端启动......");
  // 建立UDP的Socket服务
  DatagramSocket ds = new DatagramSocket(10000);
  while(true) {
   // 创建数据包
   byte[] buf = new byte[1024];
   DatagramPacket dp = new DatagramPacket(buf, buf.length);
   // 使用接收方法将数据存储到数据包中
   ds.receive(dp); // 该方法为阻塞式的方法
   // 通过数据包对象的方法解析这些数据,例如:地址、端口、数据内容等
   String ip = dp.getAddress().getHostAddress();
   int port = dp.getPort();
   String text = new String(dp.getData(), 0, dp.getLength());
   System.out.println(ip + ":" + port + ":" + text);
  }
 }
}
四、创建聊天室
根据UDP(User Datagram Protocol, 用户数据报协议)的相关性质,我们可以进一步创建一个简单的基于UDP传输协议下的聊天室,实现互动聊天的功能。
/**
*创建UDP传输下的聊天室发送端
*/
package chat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class Send implements Runnable {
 private DatagramSocket ds;
 public Send(DatagramSocket ds) {
  this.ds = ds;
 }
 public void run() {
  try {
   BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
   String line = null;
   while ((line = bufr.readLine()) != null) {
    byte[] buf = line.getBytes();
    DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.191.255"), 10001);
    ds.send(dp);
    if ("886".equals(line))
     break;
   }
   ds.close();
  } catch (Exception e) {
   System.out.println("对不起,发生错误啦!");
  }
 }
}
/**
*创建UDP传输下的聊天室接收端
*/
package chat;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Rece implements Runnable {
 private DatagramSocket ds;
 public Rece(DatagramSocket ds) {
  this.ds = ds;
 }
 public void run() {
  try {
   while (true) {
    byte[] buf = new byte[1024];
    DatagramPacket dp = new DatagramPacket(buf, buf.length);
    ds.receive(dp);
    String ip = dp.getAddress().getHostAddress();
    String text = new String(dp.getData(), 0, dp.getLength());
    System.out.println(ip + ":::" + text);
    if(text.equals("886")){
     System.out.println(ip+"......退出聊天室!");
    }
   }
  } catch (Exception e) {
   System.out.println("对不起,发生错误啦!");
  }
 }
}
/**
*创建UDP传输下的聊天室
*/
package chat;
import java.io.IOException;
import java.net.DatagramSocket;
public class ChatRoom {
 public static void main(String[] args) throws IOException {
  DatagramSocket send = new DatagramSocket();
  DatagramSocket rece = new DatagramSocket(10001);
  new Thread(new Send(send)).start();
  new Thread(new Rece(rece)).start();
 }
}
当前名称:java中模拟UDP传输的发送端和接收端实例详解
分享链接:http://www.cqwzjz.cn/article/gdcdes.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 