JAVA程序员培训定制课程c15.ppt

上传人:本田雅阁 文档编号:3481870 上传时间:2019-09-01 格式:PPT 页数:16 大小:142.52KB
返回 下载 相关 举报
JAVA程序员培训定制课程c15.ppt_第1页
第1页 / 共16页
JAVA程序员培训定制课程c15.ppt_第2页
第2页 / 共16页
JAVA程序员培训定制课程c15.ppt_第3页
第3页 / 共16页
JAVA程序员培训定制课程c15.ppt_第4页
第4页 / 共16页
JAVA程序员培训定制课程c15.ppt_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《JAVA程序员培训定制课程c15.ppt》由会员分享,可在线阅读,更多相关《JAVA程序员培训定制课程c15.ppt(16页珍藏版)》请在三一文库上搜索。

1、第十五章,网络编程,2,本章内容,使用包中的类在程序中实现网络通信 URL类及其用法 Socket通信,3,URL,URL(Uniform Resource Locator)-统一资源定位器,表示Internet上某一资源的地址。 URL组成: 协议名和资源名 protocol:resourceName URL举例: http:/ http:/ http:/ public URL(String spec); URL u1 = new URL(“http:/ public URL(URL context, String spec); URL u2 = new URL(u1, “welcome.h

2、tml”); public URL(String protocol, String host, String file); URL u3 = new URL(“http”, “”, “developers/index.html” ); public URL (String protocol, String host, int port, String file); URL u4 = new URL(“http”, “”, 80, “developers/index.html” );,5,URL类应用举例(1),import java.io.*; import .*; public class

3、URLReader public static void main(String args) try URL tirc = new URL(“http:/ BufferedReader in = new BufferedReader(new InputStreamReader(tirc.openStream(); String s; while(s = in.readLine()!=null) System.out.println(s); in.close(); catch(MalformedURLException e) System.out.println(e); catch(IOExce

4、ption e) System.out.println(e); ,6,URL类应用举例(2),程序URLReader.java输出结果: 清华大学网站首页 ,7,Socket,两个Java应用程序可通过一个双向的网络通信连接实现数据交换,这个双向链路的一端称为一个socket。 socket通常用来实现client-server连接。 包中定义的两个类Socket和ServerSocket,分别用来实现双向连接的client和server端 建立连接时所需的寻址信息 远程计算机的机器名或IP地址 试图连接的端口号(Port number),8,Socket通信模型,Server,ServerS

5、ocket s (port #),s.accept()/等待连接,Socket(),OutputStream,InputStream,socket.close(),Client,Socket (host, port #) (Attempt to connect),OutputStream,InputStream,socket.close(),9,网络编程的四个基本步骤,创建socket; 打开连接到socket的输入/输出流; 按照一定的协议对socket进行读/写操作; 关闭socket;,10,创建socket,Socket/ServerSocket类的构造方法 Socket(InetAd

6、dress address, int port); Socket(InetAddress address, int port, boolean stream); Socket(String host, int port); Socket(String host, int port, boolean stream); ServerSocket(int port); ServerSocket(int port, int count);,11,客户端Socket的建立 try Socket socket=new Socket(”127.0.0.1“,2000); catch(IOException

7、e) System.out.println(“Error:“+e); ,12,服务器端Socket的建立 ServerSocket server=null; try server=new ServerSocket(2000); catch(IOException e) System.out.println(“can not listen to :“+e); Socket socket=null; try socket=server.accept(); catch(IOException e) System.out.println(“Error:“+e); ,13,打开输入/出流 PrintSt

8、ream os=new PrintStream(new BufferedOutputStream(socket.getOutputStream(); DataInputStream is=new DataInputStream(socket.getInputStream(); 关闭Socket os.close(); is.close(); socket.close();,14,简单的client/server程序,import .*; import java.io.*; public class TestServer public static void main(String args)

9、ServerSocket s = null; try s = new ServerSocket(8888); catch (IOException e) while (true) try Socket s1 = s.accept(); OutputStream os = s1.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); dos.writeUTF(“Hello,bye-bye!“); dos.close(); s1.close(); catch (IOException e) ,15,简单的client/

10、server程序,import .*; import java.io.*; public class TestClient public static void main(String args) try Socket s1 = new Socket(“127.0.0.1“, 8888); InputStream is = s1.getInputStream(); DataInputStream dis = new DataInputStream(is); System.out.println(dis.readUTF(); dis.close(); s1.close(); catch (ConnectException connExc) System.err.println(“服务器连接失败!“); catch (IOException e) ,16,Ex1,分析并运行M15-14、15页的client/server程序,体会Socket编程原理及其实现机制;,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1