版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第十五章,网络编程,本章内容,使用包中的类在程序中实现网络通信 URL类及其用法 Socket通信,URL,URL(Uniform Resource Locator)-统一资源定位器,表示Internet上某一资源的地址。 URL组成: 协议名和资源名 protocol:resourceName URL举例: ,.URL类,常用构造方法 public URL(String spec); URL u1 = new URL(“ public URL(URL context, String spec); URL u2 = new URL(u1, “welcome.html”); public URL
2、(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” );,URL类应用举例(1),import java.io.*; import .*; public class URLReader public st
3、atic void main(String args) try URL tirc = new URL( 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(IOException e) System.out.printl
4、n(e); ,URL类应用举例(2),程序URLReader.java输出结果: 清华大学网站首页 ,Socket,两个Java应用程序可通过一个双向的网络通信连接实现数据交换,这个双向链路的一端称为一个socket。 socket通常用来实现client-server连接。 包中定义的两个类Socket和ServerSocket,分别用来实现双向连接的client和server端 建立连接时所需的寻址信息 远程计算机的机器名或IP地址 试图连接的端口号(Port number),Socket通信模型,Server,ServerSocket s (port #),s.accept()/等待连接
5、,Socket(),OutputStream,InputStream,socket.close(),Client,Socket (host, port #) (Attempt to connect),OutputStream,InputStream,socket.close(),网络编程的四个基本步骤,创建socket; 打开连接到socket的输入/输出流; 按照一定的协议对socket进行读/写操作; 关闭socket;,创建socket,Socket/ServerSocket类的构造方法 Socket(InetAddress address, int port); Socket(Inet
6、Address 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);,客户端Socket的建立 try Socket socket=new Socket(”,2000); catch(IOException e) System.out.println(Error:+e); ,服务器端Soc
7、ket的建立 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); ,打开输入/出流 PrintStream os=new PrintStream(new BufferedOutputStream(sock
8、et.getOutputStream(); DataInputStream is=new DataInputStream(socket.getInputStream(); 关闭Socket os.close(); is.close(); socket.close();,简单的client/server程序,import .*; import java.io.*; public class TestServer public static void main(String args) ServerSocket s = null; try s = new ServerSocket(8888); c
9、atch (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) ,简单的client/server程序,import .*; import java.io.*; public class TestClient public static void main(String args) try Socket s1 = new Socket(, 8888); InputStream is = s1.getInputStream(); DataInputStream dis = new DataInputStream(is); System.out.println(dis.readUTF();
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高二英语选择性必修第四册Unit 5 Section III Using Language深度学习教学设计
- 高中三年级数学“利用导数研究函数的单调性”一轮复习教学设计
- 小学四年级科学《认识矿物》教学设计-苏教版四年级上册
- 小学五年级班队活动课《平安迎新年 假期不“放假”》教学设计
- 高中地理选择性必修1 太阳视运动与昼夜长短变化教学设计
- 雨课堂学堂在线学堂云《Selective Reading of Russian Literary Works(郑州大学)》单元测试考核答案
- 综合探究二 践行社会责任 促进社会进步(学习指导) 2025-2026学年高一政治高效课堂示范教学设计+讲义+检测(统编版必修2)
- 人教版选修四 2.1 化学反应速率教学设计
- 新教材高中化学 2.2.1 电解质的电离教学设计(1)鲁科版必修第一册
- 全国江西科学技术版小学信息技术五年级下册第二单元第10课《主题活动:最快路径》教学设计
- 2026年上海高三三模高考数学模拟预测试卷(含答案详解)
- 疾控中心实验室项目初步设计
- 2026年统战部公务员面试模拟题库
- 2026陕西事业单位综合应用能力真题
- 《绍兴市电动自行车集中充停场所消防安全标准(试行)》
- 2026年网络与数据安全知识竞赛题库及答案(120题)
- 课堂碎嘴子的代价 课件2025-2026学年高一下学期纪律主题班会
- 2026年下半年教资笔试科目一《综合素质》真题试卷(附答案)
- 建筑业10项新技术深度解读
- 2026新疆新业有资产经营(集团)有限责任公司招聘备考题库及答案详解(夺冠系列)
- 西医学习中医培训结业考试大纲(试行 )
评论
0/150
提交评论