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

上传人:sh****n 文档编号:7424034 上传时间:2020-03-21 格式:PPT 页数:16 大小:357.34KB
返回 下载 相关 举报
JAVA程序员培训定制课程.ppt_第1页
第1页 / 共16页
JAVA程序员培训定制课程.ppt_第2页
第2页 / 共16页
JAVA程序员培训定制课程.ppt_第3页
第3页 / 共16页
点击查看更多>>
资源描述
第十五章 网络编程 2 本章内容 使用包中的类在程序中实现网络通信URL类及其用法Socket通信 3 URL URL UniformResourceLocator 统一资源定位器 表示Internet上某一资源的地址 URL组成 协议名和资源名protocol resourceNameURL举例 4 URL类 常用构造方法publicURL Stringspec URLu1 newURL URLu2 newURL u1 welcome html publicURL Stringprotocol Stringhost Stringfile URLu3 newURL http developers index html publicURL Stringprotocol Stringhost intport Stringfile URLu4 newURL http 80 developers index html 5 URL类应用举例 1 importjava io publicclassURLReader publicstaticvoidmain Stringargs try URLtirc newURL BufferedReaderin newBufferedReader newInputStreamReader tirc openStream Strings while s in readLine null System out println s in close catch MalformedURLExceptione System out println e catch IOExceptione System out println e 6 URL类应用举例 2 程序URLReader java输出结果 清华大学网站首页 7 Socket 两个Java应用程序可通过一个双向的网络通信连接实现数据交换 这个双向链路的一端称为一个socket socket通常用来实现client server连接 包中定义的两个类Socket和ServerSocket 分别用来实现双向连接的client和server端建立连接时所需的寻址信息远程计算机的机器名或IP地址试图连接的端口号 Portnumber 8 Socket通信模型 Server ServerSockets port s accept 等待连接 Socket OutputStream InputStream socket close Client Socket host port Attempttoconnect OutputStream InputStream socket close 9 网络编程的四个基本步骤 创建socket 打开连接到socket的输入 输出流 按照一定的协议对socket进行读 写操作 关闭socket 10 创建socket Socket ServerSocket类的构造方法Socket InetAddressaddress intport Socket InetAddressaddress intport booleanstream Socket Stringhost intport Socket Stringhost intport booleanstream ServerSocket intport ServerSocket intport intcount 11 客户端Socket的建立try Socketsocket newSocket 127 0 0 1 2000 catch IOExceptione System out println Error e 12 服务器端Socket的建立ServerSocketserver null try server newServerSocket 2000 catch IOExceptione System out println cannotlistento e Socketsocket null try socket server accept catch IOExceptione System out println Error e 13 打开输入 出流PrintStreamos newPrintStream newBufferedOutputStream socket getOutputStream DataInputStreamis newDataInputStream socket getInputStream 关闭Socketos close is close socket close 14 简单的client server程序 importjava io publicclassTestServer publicstaticvoidmain Stringargs ServerSockets null try s newServerSocket 8888 catch IOExceptione while true try Sockets1 s accept OutputStreamos s1 getOutputStream DataOutputStreamdos newDataOutputStream os dos writeUTF Hello bye bye dos close s1 close catch IOExceptione 15 简单的client server程序 importjava io publicclassTestClient publicstaticvoidmain Stringargs try Sockets1 newSocket 127 0 0 1 8888 InputStreamis s1 getInputStream DataInputStreamdis newDataInputStream is System out println dis readUTF dis close s1 close catch ConnectExceptionconnExc System err println 服务器连接失败 catch IOExceptione 16 Ex1 分析并运行M15 14 15页的client server程序 体会Socket编程原理及其实现机制
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!