




已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Writing a single byte at a time is often inefficient. For example, every TCP segment that goes out your Ethernet card contains at least 40 bytes of overhead for routing and error correction. If each byte is sent by itself, you may be stuffing(充满,塞满) the network with 41 times more data than you think you are! Consequently, most TCP/IP implementations buffer data to some extent. That is, they accumulate bytes in memory and send them to their eventual destination only when a certain number have accumulated or a certain amount of time has passed. However, if you have more than one byte ready to go, its not a bad idea to send them all at once. Using write(byte data) or write(byte data, int offset, int length) is normally much faster than writing all the components of the data array one at a time. For instance, heres an implementation of the generateCharacters() method that sends a line at a time by packing a complete line into a byte array:The .InetAddress class is Javas high-level representation of an IP address, both IPv4 and IPv6. It is used by most of the other networking classes, including Socket, ServerSocket, URL, DatagramSocket, DatagramPacket, and more. Generally, it includes both a hostname and an IP address.public class InetAddress extends Object implements SerializableInetAddress类中没有公有的构造函数,但是它有3个静态方法There are no public constructors in the InetAddress class. However, InetAddress has three static methods that return suitably initialized InetAddress objects given a little information. They are:public static InetAddress getByName(String hostName) throws UnknownHostExceptionpublic static InetAddress getAllByName(String hostName) throws UnknownHostExceptionpublic static InetAddress getLocalHost( ) throws UnknownHostExceptionSince DNS lookups can be relatively expensive (on the order of several seconds for a request that has to go through several intermediate servers, or one thats trying to resolve an unreachable host) the InetAddress class caches the results of lookups. Once it has the address of a given host, it wont look it up again, even if you create a new InetAddress object for the same host. As long as IP addresses dont change while your program is running, this is not a problem.InetAddress类缓存了查找的结果下面程序只对java 1.4以前的版本有效Example 6-2. A program that prints the address of 7import .*;public class OReillyByAddress public static void main (String args) try InetAddress address = InetAddress.getByName(7); System.out.println(address); catch (UnknownHostException ex) System.out.println(Could not find 7); Heres the result in Java 1.3 and earlier:% java OReillyByAddress /7When you call getByName( ) with an IP address string as an argument, it creates an InetAddress object for the requested IP address without checking with DNS. This means its possible to create InetAddress objects for hosts that dont really exist and that you cant connect to. The hostname of an InetAddress object created from a string containing an IP address is initially set to that string. A DNS lookup for the actual hostname is performed only when the hostname is requested, either explicitly via getHostName( ) or implicitly through toString( ). Thats how was determined from the dotted quad address 7. If at the time the hostname is requested and a DNS lookup is finally performed the host with the specified IP address cant be found, then the hostname remains the original dotted quad string. However, no UnknownHostException is thrown.The toString( ) method in Java 1.4 behaves a little differently than in earlier versions. It does not do a reverse name lookup; thus, the host is not printed unless it is already known, either because it was provided as an argument to the factory method or because getHostName( ) was invoked. In Java 1.4, Example 6-2 produces this output:/7import .*; import .*;public class Test public static void main(String args) String add=; try InetAddress address=InetAddress.getByName(add); InetAddress addr=InetAddress.getByName(0); InetAddress alladdress=InetAddress.getAllByName(add); System.out.println(address); System.out.println(addr); for(int i=0;ialladdress.length;i+) System.out.println(alladdressi); catch(UnknownHostException e) e.getMessage(); /此程序获取域名下的所有IPpublic class Test public static void main(String args) String add=; try InetAddress address=InetAddress.getByName(add); InetAddress addr=InetAddress.getByName(0); System.out.println(address); System.out.println(addr); catch(UnknownHostException e) e.getMessage(); import .*;public class Test public static void main(String args) try/获取本机的名称,ip InetAddress add=InetAddress.getLocalHost(); System.out.println(add); catch(UnknownHostException e) e.getMessage(); The .URL class is an abstraction of a Uniform Resource Locator such as / or /pub/. It extends java.lang.Object, and it is a final class that cannot be subclassed. Rather than relying on inheritance to configure instances for different kinds of URLs, it uses the strategy design pattern. Protocol handlers are the strategies, and the URL class itself forms the context through which the different strategies are selected:public final class URL extends Object implements SerializableAlthough storing a URL as a string would be trivial(琐碎的), it is helpful to think of URLs as objects with fields that include the scheme (a.k.a. 亦称the protocol), hostname, port, path, query string, and fragment identifier (a.k.a. the ref), each of whi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 直播电商2025年主播与品牌合作模式创新策略与风险防控
- 2025-2030年全球新能源产业海外投资案例分析报告
- 机关工会活动策划方案范文
- 熊皮人阅读测试题及答案
- 教育测评考试试题及答案
- 南通会议考试试题及答案
- 2025年电商知识产权保护与知识产权保护法律风险评价报告
- DB65T 4498-2022 高压气地下储气井定期检验规则
- DB65T 4415-2021 机采棉田土壤封闭除草技术规范
- 唱歌 爸爸妈妈听我说说课稿-2025-2026学年小学音乐西师大版三年级下册-西师大版
- T/CCOA 52-2023富硒小麦粉
- 2025年护士执业资格考试题库(社区护理学专项)护理法律法规知识试题卷
- 老人就餐免责协议书
- 班组长安全管理职责
- 应聘家教兼职简历
- 慢阻肺的健康教育讲座
- 光伏电站安全风险辨识及风险库管理制度
- 铁路机车防火安全教育
- 企业内训师培养与培训课程设计
- 网架防腐刷漆施工方案
- 《采矿方法的选择案例综述》1700字
评论
0/150
提交评论