黑马程序员——Java基础---网络编程(UDP编程)

来源:互联网 发布:金十数据系列软件 编辑:程序博客网 时间:2024/05/02 08:47

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

网络编程

网络模型

OSI参考模型

TCP/IP参考模型

网络通讯要素

IP地址

端口号

传输协议

网络通讯要素

IP地址:InetAddress

网络中设备的标识

不易记忆,可用主机名

本地回环地址:127.0.0.1 主机名:localhost

端口号

用于标识进程的逻辑地址,不同进程的标识

有效端口:0~65535,其中0~1024系统使用或保留端口。

传输协议

通讯的规则

常见协议:TCPUDP

TCPUDP

UDP

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

每个数据报的大小在限制在64k

因无连接,是不可靠协议

不需要建立连接,速度快

TCP

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

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

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

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

Socket

Socket就是为网络服务提供的一种机制。

通信的两端都有Socket

网络通信其实就是Socket间的通信。

数据在两个Socket间通过IO传输。

UDP传输

DatagramSocketDatagramPacket

建立发送端,接收端。

建立数据包。

调用Socket的发送接收方法。

关闭Socket

发送端与接收端是两个独立的运行程序。

代码示例:

import java.net.*;/* 需求:通过udp传输方式,将一段文字数据发送出去。思路:1,建立udpsocket服务。2,提供数据,并将数据封装到数据包中。3,通过socket服务的发送功能,将数据包发出去。4,关闭资源。 */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 is coming".getBytes();DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("lin-PC"),10000);//3,通过socket服务,将已有的数据包发送出去,通过send方法。ds.send(dp);//4,关闭资源。ds.close();}}/* 需求:定义一个应用程序,用于接收udp协议传输的数据并处理。思路:1,定义udpsocket服务。通常会监听一个端口,其实就是给这个接收网络应用程序定义数字标识。2,定义一个数据包,因为要存储接收到的字节数据。因为数据包对象中有更多功能可以提取字节数据中的不同数据信息。3,通过socket服务的receive方法将接收到的数据存入已定义好的数据包中。4,通过数据包对象的特有功能,将这些不同的数据取出,打印到控制台上。5,关闭资源。 */class UdpReceive{public static void main(String[] args)throws Exception{//1,创建udpsocket,建立端点。DatagramSocket ds = new DatagramSocket(10000);while(true){//2,定义数据包,用于存储数据。byte[] buf = new byte[1024];DatagramPacket dp = new DatagramPacket(buf,buf.length);//3,通过socket服务的receive方法将接收到的数据存入已定义好的数据包中。ds.receive(dp);//4,通过数据包的方法获取其中的数据。String ip = dp.getAddress().getHostAddress();String data = new String(dp.getData(),0,dp.getLength());int port = dp.getPort();System.out.println(ip+"::"+data+"::"+port);//5,关闭资源。//ds.close();}}}

发送端

在发送端,要在数据包对象中明确目的地 IP及端口。

DatagramSocket ds = new DatagramSocket();

byte[] by = hello,udp.getBytes();

DatagramPacket dp = new DatagramPacket(by,0,by.length,

InetAddress.getByName(127.0.0.1),10000);

ds.send(dp);

ds.close();

发送端代码示例:

import java.net.*;import java.io.*;class UdpSend2{public static void main(String[] args)throws Exception{DatagramSocket ds = new DatagramSocket();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("lin-PC"),10001);ds.send(dp);}ds.close();}}

接收端

在接收端,要指定监听的端口。

DatagramSocket ds = new DatagramSocket(10000);

byte[] by = new byte[1024];

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

ds.receive(dp);

String str = new String(dp.getData(),0,dp.getLength());

System.out.println(str+"--"+dp.getAddress());

ds.close();

接受端代码示例:

import java.net.*;import java.io.*;class UdpRece2{public static void main(String[] args)throws Exception{DatagramSocket ds = new DatagramSocket(10001);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);}}}

UDP应用场景代码示例:

/* 编写一个聊天程序。有收数据的部分和发数据的部分。这两部分需要同时执行。那就需要用到多线程技术。一个线程控制收,一个线程控制发。因为收和发是不一致的,所以要定义两个run方法。而且两个run方法要定义在不同类中。 */import java.io.*;import java.net.*;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("lin-PC"),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{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();}}

总结

网络编程分为TCP和UDP编程,而且他们应用的场景也有所不。在学习过程中我发现他们之间的不同,而且也知道了他们的实际应用场景,但是还有很多的细节没有熟悉,还需要继续努力学习。

0 0