1JAVA语言程序设计周敏彤zhoumintong@suda.edu.cn2第十二讲Java图形用户界面概述发展AWT/Swing/JFC组件分类布局管理界面设计事件处理应用实例设计原则3概述URL应用Socket应用UDP数据报关于InetAddress第十三讲网络编程4概述TheJavaplatformishighlyregardedinpartbecauseofitssuitabilityforwritingprogramsthatuseandinteractwiththeresourcesontheInternetandtheWorldWideWeb.5概述1.AppletApplet程序嵌在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.netjava.net.URLjava.net.URLConnectionjava.net.Socketjava.net.ServerSocketjava.net.DatagramPacketjava.net.DatagramSocketjava.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)throwsMalformedURLExceptionpublicURL(Stringprotocol,Stringhost,Stringfile)throwsMalformedURLExceptionpublicURL(Stringprotocol,Stringhost,intport,Stringfile)throwsMalformedURLException……实例方法publicfinalInputStreamopenStream()throwsIOExceptionOpensaconnectiontothisURLandreturnsanInputStreamforreadingfromthatconnectionpublicURLConnectionopenConnection()throwsIOExceptionReturnsaURLConnectionobjectthatrepresentsaconnectiontotheremoteobjectreferredtobytheURL11URL应用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()throwsIOExceptionpublicOutputStreamgetOutputStream()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()throwsIOExceptionListensforaconnectiontobemadetothissocketandacceptsit.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