亲宝软件园·资讯

展开

Java UDP协议发送接收数据 Java网络编程UDP协议发送接收数据

绿芽在山顶 人气:0
想了解Java网络编程UDP协议发送接收数据的相关内容吗,绿芽在山顶在本文为您仔细讲解Java UDP协议发送接收数据的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java,UDP协议发送接收数据,Java,UDP发送接收数据,Java发送接收数据,下面大家一起来学习吧。

UDP协议发送数据步骤

A:创建发送端socket对象;

B:创建数据,并把数据打包;

C:调用socket对象的发送方法发送数据包;

D:释放资源

package net;
 
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
 
public class SendDemo {
 public static void main(String[] args) throws IOException {
  //A
  DatagramSocket ds = new DatagramSocket();
  
  //B
  byte[] by = "Hello,UDP".getBytes();
  int length = by.length;
  InetAddress addr = InetAddress.getByName("192.168.1.22");
  int port = 10010;
  DatagramPacket dp = new DatagramPacket(by, length, addr, port);
  
  //C
  ds.send(dp);
  
  //D
  ds.close();
  
 }
}

UDP协议接收数据步骤

A:创建接收端socket对象;

B:创建一个数据包(接收容器);

C:调用socket对象的接收方法接收数据;

D:解析数据,显示到控制台;

E:释放资源

package net;
 
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
 
public class ReceiveDemo {
 public static void main(String[] args) throws IOException {
  //A
  DatagramSocket ds = new DatagramSocket(10010);
  
  //B
  byte[] by = new byte[1024];
  int length = by.length;
  DatagramPacket dp = new DatagramPacket(by, length);
  
  //C
  ds.receive(dp);
  
  //D
  //获取对方ip
  InetAddress addr = dp.getAddress();
  String ip = addr.getHostAddress();
  byte[] by2 = dp.getData();
  int len = by2.length;
  String s = new String(by2, 0, len);
  System.out.println(ip+"发送的数据是:"+s);
  
  //E
  ds.close();
 }
}

先运行接收端代码,再运行发送端代码。

多次从键盘接收发送数据版本

package net;
 
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 SendDemo {
 public static void main(String[] args) throws IOException {
  //A
  DatagramSocket ds = new DatagramSocket();
  //数据来自键盘录入
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String line = null;
  while((line = br.readLine()) != null){
   //当输入jieshu时,结束
   if("jieshu".equals(line)){
    break;
   }
   //B
   byte[] by = line.getBytes();
   int length = by.length;
   InetAddress addr = InetAddress.getByName("192.168.1.22");
   int port = 10010;
   DatagramPacket dp = new DatagramPacket(by, length, addr, port);
    
   //C
   ds.send(dp);
  }
  //D
  ds.close();
  
 }
}
package net;
 
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
 
public class ReceiveDemo {
 public static void main(String[] args) throws IOException {
  //A
  DatagramSocket ds = new DatagramSocket(10010);
  //多次接受版本
  while(true){
   //B
   byte[] by = new byte[1024];
   int length = by.length;
   DatagramPacket dp = new DatagramPacket(by, length);
   
   //C
   ds.receive(dp);
   
   //D
   //获取对方ip
   InetAddress addr = dp.getAddress();
   String ip = addr.getHostAddress();
   byte[] by2 = dp.getData();
   int len = by2.length;
   String s = new String(by2, 0, len);
   System.out.println(ip+"发送的数据是:"+s);
  }
  
  //E
  //ds.close();
 }
}

加载全部内容

相关教程
猜你喜欢
用户评论