java大学教程ppt13

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1JAVA语言程序设计周敏彤zhoumintong@suda.edu.cn2第十二讲Java图形用户界面概述发展AWT/Swing/JFC组件分类布局管理界面设计事件处理应用实例设计原则3概述URL应用Socket应用UDP数据报关于InetAddress第十三讲网络编程4概述TheJavaplatformishighlyregardedinpartbecauseofitssuitabilityforwritingprogramsthatuseandinteractwiththeresourcesontheInternetandtheWorldWideWeb.5概述1.AppletApplet程序嵌在HTML文件中,通过网络下载Applet程序代码,在本地Java-enabledbrowser中执行2.HTTP通过URL类获取服务器端的HTML文件3.Socket(套接字)实现Client/Server结构的应用4.JDBC(JavaDatabaseConnectivity)通过网络访问关系型数据库5.Servlet/JSP(JavaServerPage)WEB服务器端的动态编程6概述网络基础-TCP/IP协议簇网络层(NetworkLayer)InternetProtocol(IP),IP地址(32比特)传输层(TransportLayer)TransportControlProtocol(TCP)UserDatagramProtocol(UDP)Port(端口号,16比特,0~65535)应用层(ApplicationLayer)HTTP,FTP,SMTP,POP3,Telnet,DNSTCPorUDPPort应用PortPortPortPort数据应用应用应用7概述Java语言中基本网络类Packagejava.netjava.net.URLjava.net.URLConnectionjava.net.Socketjava.net.ServerSocketjava.net.DatagramPacketjava.net.DatagramSocketjava.net.MulticastSocket8概述URL应用Socket应用UDP数据报关于InetAddress第十三讲网络编程9URL应用什么是URL?统一资源定位符(UniformResourceLocator)areference(anaddress,apointer)toaresourceontheInternet.httpjava.sun.com://协议标识符资源名(主机名,端口号,文件名)http://ftpftp.pku.edu.cn/pub/://10URL应用java.net.URL类构造方法publicURL(Stringspec)throwsMalformedURLExceptionpublicURL(Stringprotocol,Stringhost,Stringfile)throwsMalformedURLExceptionpublicURL(Stringprotocol,Stringhost,intport,Stringfile)throwsMalformedURLException……实例方法publicfinalInputStreamopenStream()throwsIOExceptionOpensaconnectiontothisURLandreturnsanInputStreamforreadingfromthatconnectionpublicURLConnectionopenConnection()throwsIOExceptionReturnsaURLConnectionobjectthatrepresentsaconnectiontotheremoteobjectreferredtobytheURL11URL应用java.net.URL类-示例1.“”newURL();2.newURL();newURL(http,);newURL(http,“academic/index.html);12URL应用java.net.URL类-实例1importjava.net.*;importjava.io.*;publicclassURLReader{publicstaticvoidmain(String[]args)throwsException{URLpku=newURL();BufferedReaderin=newBufferedReader(newInputStreamReader(pku.openStream()));StringinputLine;while((inputLine=in.readLine())!=null)System.out.println(inputLine);in.close();}}java.net.URL类publicfinalInputStreamopenStream()throwsIOException13URL应用java.net.URL类-实例2StringBufferdocument=newStringBuffer();StringurlString=“”;try{URLurl=newURL(urlString);URLConnectionconn=url.openConnection();BufferedReaderreader=newBufferedReader(newInputStreamReader(conn.getInputStream()));Stringline=null;while((line=reader.readLine())!=null)document.append(line+“\n”);reader.close();}catch(MalformedURLExceptione){System.out.println(“UnabletoconnectiontoURL:”+urlString);}catch(IOExceptione){System.out.println(“IOExceptionwhenconnectedtoURL:”+urlString);}System.out.println(document.toString());java.net.URL类openStream()isashorthandforopenConnection().getInputStream()14URL应用java.net.URL类操作流程1.用所要连接资源的有效URL实例化一个URL对象(如有问题则抛出MalformedURLException)2.打开该URL对象上的一个连接3.把该连接的InputStream包装进BufferedReader以便能按行读取4.用BufferedReader读文档5.关闭BufferedReader15概述URL应用Socket应用UDP数据报关于InetAddress第十三讲网络编程16Socket应用TCP为Client/Server应用建立一个可靠的、端到端的通信连接Socket(套接字)TCP双向通信连接的一个端点通信双方:客户端、服务器端客户/服务器的本质区别服务器方(Server)总在监听一个特定的端口客户(Client)则向该端口发出连接请求Windows系统TCP/UDP连接状态的监测netstat-a17Socket应用java.net.Socket类表示TCP连接的客户方(Client),和谁连接publicSocket(Stringhost,intport)throwsUnknownHostException,IOException具有双向流publicInputStreamgetInputStream()throwsIOExceptionpublicOutputStreamgetOutputStream()throwsIOException18Socket应用对客户端对Socket进行读写-实例SocketechoSocket=null;PrintWriterout=null;BufferedReaderin=null;try{echoSocket=newSocket(“jalpha.pku.edu.cn,7);out=newPrintWriter(echoSocket.getOutputStream(),true);in=newBufferedReader(newInputStreamReader(echoSocket.getInputStream()));BufferedReaderstdIn=newBufferedReader(newInputStreamReader(System.in));StringuserInput;while((userInput=stdIn.readLine())!=null){out.println(userInput);System.out.println(“echo:“+in.readLine());}out.close();in.close();stdIn.close();echoSocket.close();}catch(UnknownHostExceptione){System.out.println(Don'tknowabouthost:jalpha.pku.edu.cn.);System.exit(1);}catch(IOExceptione){System.out.println(Couldn'tgetI/Ofor“+theconnectionto:jalpha.pku.edu.cn.);System.exit(1);}jalpa.pku.edu.cnServerSocketLocalhostSocketOutputStreamInputStreamInputStreamOutputStream19Socket应用java.net.ServerSocket类TCP连接的服务器方(Server),监听端口publicServerSocket(intport)throwsIOException接收连接请求publicSocketaccept()throwsIOExceptionListensforaconnectiontobemadetothissocketandacceptsit.Themethodblocks(阻塞)untilaconnectionismade排队的服务类型20Socket应用对ServerSocket的实现-实例ServerSocketserverSocket=null;try{serverSocket=newServerSocket(7);booleanlistening=true;while(listening){try{SocketclientSocket=serverSocket.accept();PrintWriterout=newPrintWriter(clientSocket.getOutputStream(),true);BufferedReaderin=newBufferedReader(newInpu

1 / 37
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功