java大学教程ppt13.ppt
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 World Wide Web.,5,概述,AppletApplet程序嵌在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),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.URLC.S.ServerS.DatagramP.DatagramS.MulticastSocket,8,概述URL应用Socket应用UDP数据报关于InetAddress,第十三讲 网络编程,9,URL应用,什么是URL?统一资源定位符(Uniform Resource Locator)a reference(an address,a pointer)to a resource on the Internet.,http,:/,协议标识符,资源名(主机名,端口号,文件名),http,:/,ftp,:/,10,URL应用,.URL类构造方法public URL(String spec)throws MalformedURLExceptionpublic URL(String protocol,String host,String file)throws MalformedURLExceptionpublic URL(String protocol,String host,int port,String file)throws MalformedURLException 实例方法public final InputStream openStream()throws IOExceptionOpens a connection to this URL and returns an InputStream for reading from that connectionpublic URLConnection openConnection()throws IOExceptionReturns a URLConnection object that represents a connection to the remote object referred to by the URL,11,URL应用,.URL类-示例“http:/URL(http:/URL(http:/URL(http,/academic/index.html);new URL(http,80,“academic/index.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:/in=new BufferedReader(new InputStreamReader(pku.openStream();String inputLine;while(inputLine=in.readLine()!=null)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=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.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 以便能按行读取用 BufferedReader 读文档关闭 BufferedReader,15,概述URL应用Socket应用UDP数据报关于InetAddress,第十三讲 网络编程,16,Socket应用,TCP为Client/Server应用建立一个可靠的、端到端的通信连接Socket(套接字)TCP双向通信连接的一个端点通信双方:客户端、服务器端客户/服务器的本质区别服务器方(Server)总在监听一个特定的端口客户(Client)则向该端口发出连接请求Windows系统TCP/UDP连接状态的监测netstat-a,17,Socket应用,.Socket类表示TCP连接的客户方(Client),和谁连接public Socket(String host,int port)throws UnknownHostException,IOException具有双向流public InputStream getInputStream()throws IOExceptionpublic OutputStream getOutputStream()throws IOException,18,Socket应用,对客户端对Socket进行读写-实例,Socket echoSocket=null;PrintWriter out=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;while(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.println(Couldnt get I/O for“+the connection to:.);System.exit(1);,ServerSocket,LocalhostSocket,OutputStream,InputStream,InputStream,OutputStream,19,Socket应用,.ServerSocket类TCP连接的服务器方(Server),监听端口public ServerSocket(int port)throws IOException接收连接请求public Socket accept()throws IOExceptionListens 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=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();clientSocket.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 1Socket,Client nSocket,22,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,第十三讲 网络编程,24,UDP数据报应用,数据报(Datagram)通过UDP协议发送数据报,各个数据报是相互独立,数据报是否到达(可能丢失)、到达时间、到达顺序不能保证.DatagramPacket构造一个要发送/接收的数据报对象.DatagramSocket构造一个用于发送/接收数据报的socket对象.MulticastSocket构造一个用于发送/接收组播数据报的socket对象,25,UDP数据报应用,数据报(Datagram)的收/发流程发送构造用于发送的数据报对象public DatagramPacket(byte buf,int length,InetAddress 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(DatagramPacket p)throws IOExceptionThis 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.length,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)public DatagramPacket(byte buf,int length)public byte getData()public DatagramSocket()throws SocketExceptionpublic void receive(DatagramPacket p)throws IOExceptionpublic void send(DatagramPacket p)throws IOException,构造数据报Socket,构造发送数据报,发送,要发送的地址,构造接收数据报,关闭数据报Socket,从数据报中获取数据,接收数据报,27,UDP数据报应用,数据报服务端的实现,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=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 DatagramSocket(int port)throws SocketExceptionpublic void receive(DatagramPacket p)throws IOExceptionpublic void send(DatagramPacket p)throws IOException,构造数据报Socket,监听端口,构造发送数据报,得到要发送的地址,构造接收数据报,关闭数据报Socket,接收数据报,接收到的字符串,得到要发送的端口,发送数据报,28,UDP数据报应用,组播数据报(Multicast Datagram)D类IP地址(组播地址)224.0.0.0 239.255.255.255组的标识一个应用向一个组播地址/组发送一个消息,所有组成员都能从该组播地址和端口上接收到该消息。该应用可以不是组成员当一个应用成为一个组播地址/端口的成员,则它可以接收到其他成员发送的数据报,29,UDP数据报应用,组播数据报(Multicast Datagram).MulticastSocket类指定组播地址和端口加入组/离开组,30,UDP数据报应用,组播数据报(Multicast Datagram)的实例,String msg=Hello;InetAddress grp=InetAddress.getByName(228.5.6.7);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();,.MulticastSocket extends DatagramSocketpublic MulticastSocket(int port)throws IOExceptionpublic void joinGroup(InetAddress mcastaddr)throws IOExceptionpublic void leaveGroup(InetAddress mcastaddr)throws IOExceptionpublic void send(DatagramPacket p)throws IOExceptionpublic void receive(DatagramPacket p)throws IOException,定义一个组播地址,构造接收数据报,构造组播Socket,关闭数据报Socket,加入该组,构造发送数据报,发送,接收数据报,离开该组,31,概述URL应用Socket应用UDP数据报关于InetAddress,第十三讲 网络编程,32,关于InetAddress,.InetAddress类表示IP地址没有构造方法一些实用方法public static InetAddress getAllByName(String host)throws UnknownHostExceptionpublic static InetAddress getByName(String host)throws UnknownHostExceptionpublic static InetAddress getLocalHost()throws UnknownHostExceptionpublic String getHostName()/获得主机名public String getCanonicalHostName()/得到对应该IP地址的完全域名public String toString()/得到hostname/IP address,33,关于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/127.0.0.1peerpeer,System.out.println(inet);等效于System.out.println(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.97peerpeer,System.out.println(inet);等效于System.out.println(inet.toString();,35,概述ServletJSP(Java Server Page),第十三讲 Java WEB编程,36,概述ConnectionResultSetStatementPreparedStatementConnectionPool,第十三讲 Java数据库连接(JDBC),37,第十三讲 结束!,