您的位置:首页 > 理论基础 > 计算机网络

Java基础--网络编程之UDP

2015-04-28 14:36 477 查看
凌风博客原创作品。转载请注明出处:

UDP
1.将数据及源和目的封装成数据包中,不需要建立连接

2.每个数据包的大小限制在64K内

3.因无连接,是不可靠协议

4.不需要建立连接,速度快

举例:聊天,视频共享

--------------------------

TCP

1.建立连接,形成传输数据的通道

2.在连接中进行大数据量传输

3.通过三次握手完成连接,是可靠协议

4.必须建立连接,效率会稍低

[java] view
plaincopy





import java.net.*;

class IPDemo_1

{

public static void main(String[] args) throws Exception

{

InetAddress i = InetAddress.getLocalHost();

System.out.println(i.toString());

System.out.println("address:"+i.getHostAddress());

System.out.println("name:"+i.getHostName())l

//在给定主机名的情况下确定主机的 IP 地址

InetAddress ia = InetAddress.getByName("www.baidu.com");

System.out.println("address:"+ia.getHostAddress());

System.out.println("name:"+ia.getHostName());

}

}

需求:通过UDP传输方式,将一段文字数据发送出去

定义一个UDP的发送端

思路:

1.建立udpsocket服务

2.提供数据,并将数据封装到数据包中

3.通过socket服务的发送功能,将数据包发出去。

4.关闭资源

[java] view
plaincopy





import java.net.*;

class UDPSend

{

public static void main(String[] args) throws Exception

{

//1.创建UDP服务,通过DatagramSocket对象

DatagramSocket ds = new DatagramSocket(8888); //指定发送端口

//2.确定数据,并封装成数据包 DatagramPacket(byte[] buf, int length, InetAddress address, int port)

byte[] buf = "UDP ge men lai le ".getBytes();

DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.102"),10000);

//3.通过socket服务,将已有的数据包发送出去,通过send方法

ds.send(dp);

//4.关闭资源

ds.close();

}

}

需求:

定义一个应用程序,用于接收UDP 协议传输的数据并处理的

定义UDP的接收端

思路:

1.定义UDPsocket服务,通常会监听一个端口,其实就是给这个接收网络应用程序定义数字标识。

方便于明确哪些数据过来该应用程序可以处理。

2.定义一个数据包,因为要存储接收到的字节数据

因为数据包对象中有更多功能可以提取字节数据中的不同数据信息。

3.通过socket服务的receive方法将收到的数据存入已定义好的数据包中

4.通过数据包对象的特有功能,将这些不同的数据取出,打印在控制台上。

5.关闭资源

[java] view
plaincopy





class UDPRece

{

public static void main(String[] args) throws Exception

{

//1.创建UDP socket,建立端点

DatagramSocket ds = new DatagramSocket(10000); //指定接收端口

while (true)//无限接收数据

{

//2.定义数据包,用于存储数据

byte[] buf = new byte[1024];

DatagramPacket dp = new DatagramPacket(buf,buf.length);

//3.通过服务的receive方法将收到数据存入数据包中,

ds.receive(dp); //阻塞式方法

//4.通过数据包的方法获取其中的数据

String ip = dp.getAddress().getHostAddress();//接收IP

String data = new String(dp.getData(),0,dp.getLength());//接收数据的有效长度

int port = dp.getPort(); //接收发送端的端口号

System.out.println(ip+"..."+data+"..."+port);

}

//5.关闭资源

ds.close();

}

}

练习

[java] view
plaincopy





import java.io.*;

import java.net.*;

class UDPSend

{

public static void main(String[] args) throws Exception

{

//1.创建UDP服务,通过DatagramSocket对象

DatagramSocket ds = new DatagramSocket(8888); //指定发送端口

BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));

String line = null;

while ((line=bufr.readLine())!=null)

{

if("886".equals(line))

break;

//2.确定数据,并封装成数据包 DatagramPacket(byte[] buf, int length, InetAddress address, int port)

byte[] buf = line.getBytes();

DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.102"),10000);

//3.通过socket服务,将已有的数据包发送出去,通过send方法

ds.send(dp);

}

//4.关闭资源

ds.close();

}

}

class UDPRece

{

public static void main(String[] args) throws Exception

{

//1.创建UDP socket,建立端点

DatagramSocket ds = new DatagramSocket(10000); //指定接收端口

while (true)//无限接收数据

{

//2.定义数据包,用于存储数据

byte[] buf = new byte[1024];

DatagramPacket dp = new DatagramPacket(buf,buf.length);

//3.通过服务的receive方法将收到数据存入数据包中,

ds.receive(dp); //阻塞式方法

//4.通过数据包的方法获取其中的数据

String ip = dp.getAddress().getHostAddress();//接收IP

String data = new String(dp.getData(),0,dp.getLength());//接收数据的有效长度

int port = dp.getPort(); //接收发送端的端口号

System.out.println(ip+"..."+data+"..."+port);

}

//5.关闭资源

//ds.close();

}

}

编写一个聊天程序。

有收数据的部分,和发数据的部分

这两部分需要同时执行

那就需要用到多线程技术

一个线程控制收,一个线程控制发。

因为收和发动作时不一致的,所以要定义两个run方法

而且这两个方法要封装到不同的类中

[java] view
plaincopy





import java.net.*;

import java.io.*;

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)

{

if("886".equals(line))

break;

byte[] buf= line.getBytes();

DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.255"),10002);

ds.send(dp);

}

}

catch (Exception e)

{

throw new RuntimeException("发送端失败");

}

}

}

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 data = new String(dp.getData(),0,dp.getLength());

System.out.println(ip+"::"+data);

}

}

catch (Exception e)

{

throw new RuntimeException("接收端失败");

}

}

}

class ChatDemo_4

{

public static void main(String[] args) throws Exception

{

DatagramSocket sendSocket = new DatagramSocket();

DatagramSocket receSocket = new DatagramSocket(10002);

new Thread(new Send(sendSocket)).start();

new Thread(new Rece(receSocket)).start();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: