您的位置:首页 > 其它

UDP传输演示

2016-01-10 23:28 218 查看
演示1

class  sendDemo{
public static void main(String[] args){
/*
* 创建UDP的传输发送端
*
* 1:建立UDP的socket服务
*
* 2:把要发送的数据,封装成数据包
*
* 3:通过udp的socket服务发送数据出去
*
* 4:关闭服务
*
*
* */

//1
DatagramSocket  ds=new DatagramSocket();
//2
String str="你好数据";
DatagramPacket  dp=new DatagramPacket(str.getByte(),str.getBute().length,InetAddress.getByName("192.168.0.108"),10000);
//3
ds.send(dp);
//4
ds.close();

}

}

class receDemo{
public static void main(String[] args){
/*
*
* 建立udp的接受端的思路
*
*1.建立socket服务
*
*2.建立数据包,用于存储接收到的数据,方便用数据包对象的方法解析这些数据。
*
*3.使用socket服务的receive方法将接受的数据存储到数据包中。
*
*4.通过数据包的方法解析数据包中的数据
*
*5.关闭资源。
*
*
* */
Datagramsocket  ds=new DatagramSocket(10000);
byte[]  buf=new byte[];
DatagramPacket  dp=new DatagramPacket(buf,buf.length);
ds.receive(dp);

String text=new String(buf,0,buf.length);

System.out.println(text);

ds.close();

}

}


需求:使用UDP做一个键盘录入数据,循环发送数据,当且仅当输入,886之后,才停止程序

class   sendChat{
public static void main(String[] args){
System.out.println("发送端");

InputStream is=System.in;
InputStreamReader isr=new InputStreamReader(is);
BufferedReader  bs=new BufferedReader(isr);
DatagramSocket  ds=new DatagramSocket();
String len=0;
while((len=bs.readLine())!=null){
DatagramPacket dp=new DatagramPacket(len.getBytes(),len.getBytes().length,InteAddress.getName("192.168.1.100"),10000);

if(886.equals(len)){
break;
}

}
bs.close();
ds.close();
}

}

//接收端
class  ReceChat{
public static void main(String[] args){
//接收端
DatagramSocket  ds=new DatagramSocket(10000);
while(true){
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,buf.length);
ds.receive(dp);
int port=ds.getPort();
String ip=dp.getAddress().getHostAddress();
String text=new String(buf,buf.length);
System.out.println(text);
}
}
}


//UDP和多线程的技术的结合(模仿QQ)

class QQSend implements Runable{
private DatagramSocket  ds;

public QQSend(DatagramSocket ds){
this.ds=ds;
}

public void setDs(DatagramSocket ds){
this.ds=ds;
}

public DatagramSocket  getDs(){
return ds;
}

public void run(){
System.out.println("发送端");
InputStream is=System.in;
InputStreamReder isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
String line= null;
try{

while((line=br.readLine())!=null){
DatagramPacket dp=new DatagramPacket(line.getbytes(),line.length,InetAddress.getByName(192.168.0.100),10000);
ds.send(dp);

if("886".equals(line)){
break;
}
}
}catch{

}
}
}

//接受端
class QQRece  implements Runable{
private DatagramSocket ds=null;
public QQRece(DatagramSocket ds){
this.ds=ds;
}

public void setDs(DatagramSocket ds){
this.ds=ds;
}

public DatahramSocket getDs(){
return ds;
}

public void run(){
System.out.println("接收端");
try{
while(true){
byte[] buf=new byte[1024];
Datagr
4000
amPacket dp=new DatagramPacket(buf,buf.length);
ds.receive(dp);

String text=new String(buf,0,buf.length);

System.our.println(text);

}
}catch{}
}

}

class  QQThread {
public static void main(String[] args){
DatagramSocket send=new DatagramSocket();
DatagramSocket rece=new DatagramSocket(10000);

new Thread(new QQSend(send)).start();
new Thread(new QQRece(rece)).start();
}

}


总结

使用UDP的传输

记得对象 DatagramSocket 创建socket流

DatagramPacket 数据传输对象

DatagramScoket 的send()发送数据 receive()接收数据。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  socket udp