第十三章 Java网络编程.ppt_第1页
第十三章 Java网络编程.ppt_第2页
第十三章 Java网络编程.ppt_第3页
第十三章 Java网络编程.ppt_第4页
第十三章 Java网络编程.ppt_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Java Networking Programing,URL,What Is a URL? Definition: URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.,:8080/index.html#Musics,URL,Creating a URL URL gamelan = new URL(,URL(URL baseURL, String relativeURL),URL gamelan = new URL( URL ga

2、melanGames = new URL(gamelan, Gamelan.game.html); URL gamelanNetwork = new URL(gamelan, G.html);,URL(String absoluteURL),URL gamelan = new URL( http, , 80, “pages/Gwork.html); This creates a URL object for the following URL: :80/pages/Gwork.html,MalformedURLException,try URL myURL = new URL(. . .) c

3、atch (MalformedURLException e) . . . / exception handler code here ,Parsing a URL,getProtocol Returns the protocol identifier component of the URL. getHost Returns the host name component of the URL. getPort Returns the port number component of the URL. The getPort method returns an integer that is

4、the port number. If the port is not set, getPort returns -1. getFile Returns the filename component of the URL. getRef Returns the reference component of the URL.,Heres the output displayed by the program: protocol = http host = filename = /docs/books/tutorial/index.html port = 80 ref = DOWNLOADING,

5、Reading Directly from a URL,Connecting to a URL,Reading from a URLConnection,Writing to a URLConnection,Sockets,Sometimes your programs require lower-level network communication. for example, when you want to write a client-server application. To communicate, the client and the server each reads fro

6、m and writes to the socket bound to the connection.,What Is a Socket?,Definition: A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be se

7、nt.,What Is a Socket?,Reading from and Writing to a Socket,This client program is straightforward and simple because the Echo server implements a simple protocol. The client sends text to the server, and the server echoes it back. When your client programs are talking to a more complicated server su

8、ch as an HTTP server, your client program will also be more complicated. However, the basics are much the same as they are in this program: Open a socket. Open an input stream and output stream to the socket. Read from and write to the stream according to the servers protocol. Close the streams. Clo

9、se the socket.,All About Datagrams,Some applications that you write to communicate over the network will not require the reliable, point-to-point channel provided by TCP. Rather, your applications might benefit from a mode of communication that delivers independent packages of information whose arri

10、val and order of arrival are not guaranteed. The UDP protocol provides a mode of network communication whereby applications send packets of data, called datagrams, to one another. A datagram is an independent, self-contained message sent over the network whose arrival, arrival time, and content are

11、not guaranteed. The DatagramPacket and DatagramSocket classes in the package implement system-independent datagram communication using UDP.,What Is a Datagram? Writing a Datagram Client and Server Broadcasting to Multiple Recipients,What Is a Datagram?,Clients and servers that communicate via a reli

12、able channel, such as a URL or a socket, have a dedicated point-to-point channel between themselves, or at least the illusion of one. To communicate, they establish a connection, transmit the data, and then close the connection. All data sent over the channel is received in the same order in which i

13、t was sent. This is guaranteed by the channel. In contrast, applications that communicate via datagrams send and receive completely independent packets of information. These clients and servers do not have and do not need a dedicated point-to-point channel. The delivery of datagrams to their destinations is not guaranteed. Nor is the order of their arrival. Definition: A datagram

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论