欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-23 16:56:57
本文实例为大家分享了java网络编程udp协议发送接收数据的具体代码,供大家参考,具体内容如下udp协议发送数据步骤a:创建发送端socket对象;b:创建数据,并把数据打包;c:调用socket对象...

本文实例为大家分享了java网络编程udp协议发送接收数据的具体代码,供大家参考,具体内容如下

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: Java UDP 数据