2019java大学教程ppt13.ppt

上传人:上海哈登 文档编号:2808656 上传时间:2019-05-20 格式:PPT 页数:37 大小:174.01KB
返回 下载 相关 举报
2019java大学教程ppt13.ppt_第1页
第1页 / 共37页
2019java大学教程ppt13.ppt_第2页
第2页 / 共37页
2019java大学教程ppt13.ppt_第3页
第3页 / 共37页
2019java大学教程ppt13.ppt_第4页
第4页 / 共37页
2019java大学教程ppt13.ppt_第5页
第5页 / 共37页
点击查看更多>>
资源描述

《2019java大学教程ppt13.ppt》由会员分享,可在线阅读,更多相关《2019java大学教程ppt13.ppt(37页珍藏版)》请在三一文库上搜索。

1、1,JAVA语言程序设计,周敏彤 ,2,第十二讲 Java图形用户界面,概述 发展 AWT/Swing/JFC 组件分类 布局管理 界面设计 事件处理 应用实例 设计原则,3,概述 URL应用 Socket应用 UDP数据报 关于InetAddress,第十三讲 网络编程,4,概述,The Java platform is highly regarded in part because of its suitability for writing programs that use and interact with the resources on the Internet and the

2、World Wide Web.,5,概述,Applet Applet程序嵌在HTML文件中,通过网络下载Applet程序代码,在本地Java-enabled browser 中执行 HTTP 通过URL类获取服务器端的HTML文件 Socket(套接字) 实现Client/Server结构的应用 JDBC (Java Database Connectivity) 通过网络访问关系型数据库 Servlet/JSP (Java Server Page) WEB服务器端的动态编程,6,概述,网络基础-TCP/IP协议簇 网络层(Network Layer) Internet Protocol(IP)

3、, IP地址(32比特) 传输层(Transport Layer) Transport Control Protocol (TCP) User Datagram Protocol (UDP) Port (端口号, 16比特, 065535) 应用层(Application Layer) HTTP, FTP, SMTP, POP3, Telnet, DNS,7,概述,Java语言中基本网络类 Package .URL .URLConnection .Socket .ServerSocket .DatagramPacket .DatagramSocket .MulticastSocket,8,概

4、述 URL应用 Socket应用 UDP数据报 关于InetAddress,第十三讲 网络编程,9,URL应用,什么是URL? 统一资源定位符(Uniform Resource Locator) a reference (an address, a pointer) to a resource on the Internet.,http,,:/,协议标识符,资源名 (主机名, 端口号, 文件名),http, 构造方法 public URL(String spec) throws MalformedURLException public URL(String protocol, String h

5、ost, String file) throws MalformedURLException public URL(String protocol, String host, int port, String file) throws MalformedURLException 实例方法 public final InputStream openStream() throws IOException Opens a connection to this URL and returns an InputStream for reading from that connection public

6、URLConnection openConnection() throws IOException Returns a URLConnection object that represents a connection to the remote object referred to by the URL,11,URL应用,.URL类-示例 “http:/ new URL(“http:/ http:/ new URL(“http:/ new URL(“http“, ““, “/academic/index.html“); new URL(“http“, ““, 80, “academic/in

7、dex.html“);,12,URL应用,.URL类-实例1,import .*; import java.io.*; public class URLReader public static void main(String args) throws Exception URL pku = new URL(“http:/ BufferedReader in = new BufferedReader(new InputStreamReader( pku.openStream(); String inputLine; while (inputLine = in.readLine() != nul

8、l) System.out.println(inputLine); in.close(); ,.URL类 public final InputStream openStream() throws IOException,13,URL应用,.URL类-实例2,StringBuffer document = new StringBuffer(); String urlString = “http:/”; try URL url = new URL(urlString); URLConnection conn = url.openConnection(); BufferedReader reader

9、 = new BufferedReader(new InputStreamReader(conn.getInputStream(); String line = null; while(line = reader.readLine() != null) document.append(line + “n”); reader.close(); catch(MalformedURLException e) System.out.println(“Unable to connection to URL:” + urlString); catch (IOException e) System.out.

10、println(“IOException when connected to URL:” + urlString); System.out.println(document.toString();,.URL类 openStream() is a shorthand for openConnection().getInputStream(),14,URL应用,.URL类 操作流程,用所要连接资源的有效 URL实例化一个 URL对象(如有问题则抛出 MalformedURLException) 打开该 URL对象上的一个连接 把该连接的 InputStream 包装进 BufferedReader

11、 以便能按行读取 用 BufferedReader 读文档 关闭 BufferedReader,15,概述 URL应用 Socket应用 UDP数据报 关于InetAddress,第十三讲 网络编程,16,Socket应用,TCP为Client/Server应用建立一个可靠的、端到端的通信连接 Socket(套接字) TCP双向通信连接的一个端点 通信双方: 客户端、服务器端 客户/服务器的本质区别 服务器方(Server)总在监听一个特定的端口 客户(Client)则向该端口发出连接请求 Windows系统TCP/UDP连接状态的监测 netstat -a,17,Socket应用,.Sock

12、et类 表示TCP连接的客户方(Client),和谁连接 public Socket(String host, int port) throws UnknownHostException, IOException 具有双向流 public InputStream getInputStream() throws IOException public OutputStream getOutputStream() throws IOException,18,Socket应用,对客户端对Socket进行读写-实例,Socket echoSocket = null; PrintWriter out =

13、null; BufferedReader in = null; try echoSocket = new Socket(““, 7); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream(); BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in); String userInput; whil

14、e (userInput = stdIn.readLine() != null) out.println(userInput); System.out.println(“echo: “ + in.readLine(); out.close(); in.close(); stdIn.close(); echoSocket.close(); catch (UnknownHostException e) System.out.println(“Dont know about host: .“); System.exit(1); catch (IOException e) System.out.pri

15、ntln(“Couldnt get I/O for “ + “the connection to: .“); System.exit(1); , ServerSocket,Localhost Socket,OutputStream,InputStream,InputStream,OutputStream,19,Socket应用,.ServerSocket类 TCP连接的服务器方(Server),监听端口 public ServerSocket(int port) throws IOException 接收连接请求 public Socket accept() throws IOExceptio

16、n Listens for a connection to be made to this socket and accepts it. The method blocks(阻塞) until a connection is made 排队的服务类型,20,Socket应用,对ServerSocket的实现-实例,ServerSocket serverSocket = null; try serverSocket = new ServerSocket(7); boolean listening = true; while(listening) try Socket clientSocket =

17、 serverSocket.accept(); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader( clientSocket.getInputStream(); String inputLine; while (inputLine = in.readLine() != null) out.println(inputLine); out.close(); in.close(); c

18、lientSocket.close(); catch (IOException e) System.out.println(e); serverSocket.close(); catch (IOException e) System.out.println(e); System.exit(1); ,21,Socket应用,多线程的服务器实现 为每个客户的连接(Socket)分配一个线程,让其独立处理 作为java.lang.Thread类的子类 实现java.lang.Runnable接口,Thread 1,Thread n,Client 1 Socket,Client n Socket,22

19、,Socket应用,多线程的服务器实现-实例,ServerSocket serverSocket = new ServerSocket(7); boolean listening = true; while (listening) new EchoServerThread(serverSocket.accept().start(); serverSocket.close();,class EchoServerThread extends Thread public void run() ,23,概述 URL应用 Socket应用 UDP数据报 关于InetAddress,第十三讲 网络编程,2

20、4,UDP数据报应用,数据报(Datagram) 通过UDP协议发送数据报, 各个数据报是相互独立, 数据报是否到达(可能丢失)、到达时间、到达顺序不能保证 .DatagramPacket 构造一个要发送/接收的数据报对象 .DatagramSocket 构造一个用于发送/接收数据报的socket对象 .MulticastSocket 构造一个用于发送/接收组播数据报的socket对象,25,UDP数据报应用,数据报(Datagram) 的收/发流程 发送 构造用于发送的数据报对象 public DatagramPacket(byte buf, int length, InetAddress

21、address, int port) 构造用于发送数据报的socket对象 public DatagramSocket() throws SocketException 发送 public void send(DatagramPacket p) throws IOException 接收 构造用于接收的数据报对象 public DatagramPacket(byte buf, int length) 构造用于接收数据报的socket对象 public DatagramSocket(int port) throws SocketException 接收 public void receive(D

22、atagramPacket p) throws IOException This method blocks until a datagram is received,26,UDP数据报应用,数据报客户端的实现,DatagramSocket socket = new DatagramSocket(); String s = “hello”; byte buf = s.getBytes(); InetAddress address = InetAddress.getByName(“”); DatagramPacket packet = new DatagramPacket(buf, buf.le

23、ngth, address, 6666); socket.send(packet); packet = new DatagramPacket(buf, buf.length); socket.receive(packet); String received = new String(packet.getData(); System.out.println(“Received: “ + received); socket.close();,public DatagramPacket(byte buf, int length, InetAddress address, int port) publ

24、ic DatagramPacket(byte buf, int length) public byte getData() public DatagramSocket() throws SocketException public void receive(DatagramPacket p) throws IOException public void send(DatagramPacket p) throws IOException,构造数据报Socket,构造发送数据报, 发送,要发送的地址,构造接收数据报,关闭数据报Socket,从数据报中获取数据,接收数据报,27,UDP数据报应用,数

25、据报服务端的实现,DatagramSocket socket = new DatagramSocket(6666); byte buf = new byte256; DatagramPacket packet = new DatagramPacket(buf, buf.length); socket.receive(packet); String received = new String(packet.getData().trim(); InetAddress address = packet.getAddress(); int port = packet.getPort(); packet

26、 = new DatagramPacket(buf, buf.length, address, port); socket.send(packet); socket.close();,public DatagramPacket(byte buf, int length, InetAddress address, int port) public DatagramPacket(byte buf, int length) public byte getData() public InetAddress getAddress() public int getPort() public Datagra

27、mSocket(int port) throws SocketException public void receive(DatagramPacket p) throws IOException public void send(DatagramPacket p) throws IOException,构造数据报Socket,监听端口,构造发送数据报,得到要发送的地址,构造接收数据报,关闭数据报Socket,接收数据报,接收到的字符串,得到要发送的端口,发送数据报,28,UDP数据报应用,组播数据报(Multicast Datagram) D类IP地址(组播地址) 224.0.0.0 239.

28、255.255.255 组的标识 一个应用向一个组播地址/组发送一个消息,所有组成员都能从该组播地址和端口上接收到该消息。该应用可以不是组成员 当一个应用成为一个组播地址/端口的成员,则它可以接收到其他成员发送的数据报,29,UDP数据报应用,组播数据报(Multicast Datagram) .MulticastSocket类 指定组播地址和端口 加入组/离开组,30,UDP数据报应用,组播数据报(Multicast Datagram)的实例,String msg = “Hello“; InetAddress grp = InetAddress.getByName(“228.5.6.7“);

29、 MulticastSocket s = new MulticastSocket(6789); s.joinGroup(grp); DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(), grp, 6789); s.send(hi); byte buf = new byte1000; DatagramPacket recv = new DatagramPacket(buf, buf.length); s.receive(recv); s.leaveGroup(grp); s.close();,.Multicast

30、Socket extends DatagramSocket public MulticastSocket(int port) throws IOException public void joinGroup(InetAddress mcastaddr) throws IOException public void leaveGroup(InetAddress mcastaddr) throws IOException public void send(DatagramPacket p) throws IOException public void receive(DatagramPacket

31、p) throws IOException,定义一个组播地址,构造接收数据报,构造组播Socket,关闭数据报Socket,加入该组,构造发送数据报,发送,接收数据报,离开该组,31,概述 URL应用 Socket应用 UDP数据报 关于InetAddress,第十三讲 网络编程,32,关于InetAddress,.InetAddress类 表示IP地址 没有构造方法 一些实用方法 public static InetAddress getAllByName(String host) throws UnknownHostException public static InetAddress g

32、etByName(String host) throws UnknownHostException public static InetAddress getLocalHost() throws UnknownHostException public String getHostName() /获得主机名 public String getCanonicalHostName() /得到对应该IP地址的完全域名 public String toString() /得到hostname/IP address,33,关于InetAddress,.InetAddress类-实例(无连网),InetAd

33、dress inet = null; try inet = InetAddress.getLocalHost(); catch (UnknownHostException e) System.out.println(e); System.out.println(inet); System.out.println(inet.getHostName(); System.out.println(inet.getCanonicalHostName();,运行结果: peer/127.0.0.1 peer peer,System.out.println(inet); 等效于 System.out.pri

34、ntln(inet.toString();,localhost 本地主机 127.0.0 1 回送地址 (loopback address),34,关于InetAddress,.InetAddress类-实例(连网),InetAddress inet = null; try inet = InetAddress.getLocalHost(); catch (UnknownHostException e) System.out.println(e); System.out.println(inet); System.out.println(inet.getHostName(); System.out.println(inet.getCanonicalHostName();,运行结果: peer/162.105.130.97 peer peer,System.out.println(inet); 等效于 System.out.println(inet.toString();,35,概述 Servlet JSP(Java Server Page),第十三讲 Java WEB编程,36,概述 Connection ResultSet Statement PreparedStatement ConnectionPool,第十三讲 Java数据库连接(JDBC),37,第十三讲 结束 !,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1