1、1,Java语言程序设计,2,概述 URL应用 Socket应用 UDP数据报,第十章 Java网络编程,3,概述,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.,4,概述,Applet Applet程序嵌在HTML文件中,通过网络下载Applet程序代码,在本地Java-enabled brows
2、er 中执行 HTTP 通过URL类获取服务器端的HTML文件 Socket(套接字) 实现Client/Server结构的应用 JDBC (Java Database Connectivity) 通过网络访问关系型数据库 Oracle, MS SQL, Sybase Servlet/JSP (Java Server Page) WEB服务器端的动态编程,5,概述,网络基础-TCP/IP协议簇 网络层(Network Layer) Internet Protocol (IP), IP地址, 32比特 传输层(Transport Layer) 传输控制协议(TCP: Transport Cont
3、rol Protocol) 用户数据报协议(UDP: User Datagram Protocol) 端口(Port, 16比特, 065535) 应用层(Application Layer) HTTP, FTP, SMTP, POP3, Telnet, DNS,TCP or UDP,Port,应用,Port,Port,Port,Port,数据1,应用,应用,应用,Port,数据2,主机,6,概述,Java语言中基本网络类 Package .URL .URLC.Socket .ServerS.DatagramPacket .DatagramS.MulticastSocket,7,概述 URL
4、应用 Socket应用 UDP数据报,第十章 Java网络编程,8,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 host, String file
5、) 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 URLConnection op
6、enConnection() throws IOException Returns a URLConnection object that represents a connection to the remote object referred to by the URL,10,URL应用,.URL类-示例 “http:/ new URL(“http:/“); http:/ new URL(“http:/ new URL(“http“, ““, “/academic/index.html“); new URL(“http“, ““, 80, “/academic/index.html“);,
7、11,URL应用,实例,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() != null)System.out.println
8、(inputLine);in.close(); ,.URL类 public final InputStream openStream() throws IOException,12,URL应用,.URL类-实例2,StringBuffer document = new StringBuffer(); String urlString = “http:/”; try URL url = new URL(urlString);URLConnection conn = url.openConnection();BufferedReader reader = new BufferedReader(ne
9、w 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 co
10、nnected to URL:” + urlString); System.out.println(document.toString();,.URL类 openStream() is a shorthand for openConnection().getInputStream(),13,URL应用,.URL类 操作流程,用所要连接资源的有效 URL实例化一个 URL对象(如有问题则抛出 MalformedURLException) 打开该 URL对象上的一个连接 把该连接的 InputStream 包装进 BufferedReader 以便能按行读取 用 BufferedReader 读文
11、档 关闭 BufferedReader (关闭该URL),14,概述 URL应用 Socket应用 UDP数据报,第十章 Java网络编程,15,Socket应用,TCP协议 从功能上来讲,建立一个可靠的、端到端的通信连接 操作系统实现了TCP协议的内容 Socket(套接字) 代表了TCP所定义的双向通信连接的一个端点 通信双方(两台机器) 一个作为客户端,一个作为服务器端 客户/服务器的本质区别 服务器方(Server)总在监听一个特定的端口 客户(Client)则向该端口发出连接请求 Windows系统TCP/UDP连接状态的监测 netstat -a,16,Socket应用,.Sock
12、et类 表示TCP连接的客户方(Client),和谁连接 指定对方的IP地址和端口号 public Socket(String host, int port) throws UnknownHostException, IOException Socket对象包括两个流 Socket代表了TCP所定义的双向通信连接的一个端点 输入流(读取通过网络进来的数据) public InputStream getInputStream() throws IOException 输出流(将数据写入输出流中,并通过网络发送) public OutputStream getOutputStream() thro
13、ws IOException 操作步骤 先建立连接 进行流的读写操作,17,Socket应用,对客户端对Socket进行读写-实例, ServerSocket,Localhost Socket,OutputStream,InputStream,InputStream,OutputStream,客户端,服务器端,18,Socket应用,对客户端对Socket进行读写-实例,import .*; import java.io.*;public class SimpleClient public static void main(String args) Socket s = new Socket(
14、“”, 5432);InputStream in = s.getInputStream();DataInputStream dis = new DataInputStream(in);String st = dis.readUTF();System.out.println(st);in.close();s.close(); ,建立连接,打开输入流,读取输入流,关闭输入流,关闭连接,19,Socket应用,.ServerSocket类 TCP连接的服务器方(Server),监听端口 等待自客户端发来的连接 public ServerSocket(int port) throws IOExcept
15、ion 接收连接请求 public Socket accept() throws IOException Listens for a connection to be made to this socket and accepts it. The method blocks(阻塞) until a connection is made 服务器端通过所接收到的Socket对象和客户端通信 Socket代表了TCP所定义的双向通信连接的一个端点 操作步骤 监听端口 接收连接 进行流的读写操作,20,Socket应用,对ServerSocket的实现-实例,客户端,服务器端,21,Socket应用,
16、ServerSocket s = null; String hello = “Hello World!”; try s = new ServerSocket(5432); catch(IOException e) System.out.println(e);System.exit(1); while(true) try Socket cs = s.accept();OutputStream out = cs.getOutputStream();DataOutputStream dos = new DataOutputStream(out);dos.writeUTF(hello);out.clo
17、se();cs.close(); catch(IOException e) System.out.println(e); ,接收连接,打开输出流,写入输出流,关闭输出流,关闭连接,监听端口,22,Socket应用,客户端与服务器端的实现,客户端,服务器端,23,Socket应用,多线程的服务器实现 为每个客户的连接(Socket)分配一个线程,让其独立处理 两种实现方式 作为java.lang.Thread类的子类 实现java.lang.Runnable接口,Thread 1,Thread n,Client 1 Socket,Client n Socket,1,2,3,1,2,3,24,So
18、cket应用,多线程的服务器实现-实例,ServerSocket s = new ServerSocket(5432); boolean listening = true; while (listening) new ServerThread(s.accept().start(); s.close();,class ServerThread extends Thread public void run() ,25,概述 URL应用 Socket应用 UDP数据报,第十章 Java网络编程,26,UDP数据报应用,数据报(Datagram) 通过UDP协议发送数据报, 各个数据报是相互独立, 数
19、据报是否到达(可能丢失)、到达时间、到达顺序不能保证 .DatagramPacket 构造一个要发送/接收的数据报对象 .DatagramSocket 构造一个用于发送/接收数据报的socket对象 .MulticastSocket 构造一个用于发送/接收组播数据报的socket对象,27,UDP数据报应用,数据报(Datagram) 的收/发流程 发送 构造用于发送的数据报对象(指定要发送的地址和端口号) public DatagramPacket(byte buf, int length, InetAddress address, int port) 构造用于发送数据报的socket对象
20、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 IOException Th
21、is method blocks until a datagram is received,28,UDP数据报应用,数据报客户端的实现-实例,客户端,服务器端,29,UDP数据报应用,数据报客户端的实现,DatagramSocket socket = new DatagramSocket(); String s = “hello”; byte buf = s.getBytes(); InetAddress address = InetAddress.getByName(“”); DatagramPacket packet = new DatagramPacket(buf, buf.length
22、, 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 D
23、atagramPacket(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,从数据报中获取数据,接收数据报,30,UDP数据报应用,数据报服务
24、端的实现-实例,客户端,服务器端,31,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 p
25、ort = 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() publi
26、c int getPort() public DatagramSocket(int port) throws SocketException public void receive(DatagramPacket p) throws IOException public void send(DatagramPacket p) throws IOException,构造数据报Socket,监听端口,构造发送数据报,得到要发送的地址,构造接收数据报,关闭数据报Socket,接收数据报,接收到的字符串,得到要发送的端口,发送数据报,32,UDP数据报应用,组播数据报(Multicast Datagra
27、m) 特定的IP地址(组播地址) 224.0.0.0 239.255.255.255 该IP地址作为组的标识 一个应用向一个组播地址/组发送一个消息,所有组成员都能从该组播地址和端口上接收到该消息。该应用可以不是组成员 类似与邮件列表 当一个应用成为一个组播地址/端口的成员,则它可以接收到其他成员发送的数据报,33,UDP数据报应用,组播数据报(Multicast Datagram) .MulticastSocket类 指定组播地址和端口 加入组/离开组,5,组播地址组(224.0.0.1),1,2,4,加入组,离开组,3,34,UDP数据报应用,组播数据报(Multicast Datagra
28、m)的实例,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 DatagramP
29、acket(buf, buf.length); s.receive(recv); s.leaveGroup(grp); s.close();,.MulticastSocket 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 p) throws IOException,定义一个组播地址,构造接收数据报,构造组播Socket,关闭数据报Socket,加入该组,构造发送数据报,发送,接收数据报,离开该组,35,第十章 结束 !,