JavaSE系列代码70:利用InetAddress编程

InputStream: an object that can read a sequence of bytes. All classes related to reading byte sequences inherit from InputStream. OutputStream: an object that can write a sequence of bytes. All classes related to write byte sequences inherit from OutputStream. because byte oriented streams are not convenient for handling information stored as Unicode (two bytes per character), Java has introduced a class hierarchy for handling Unicode characters, derived from abstract classes reader and writer, which are used to read and write double byte Unicode characters instead of single byte characters.

import java.net.*;
public class Javase_70
{
  InetAddress myIPaddress=null;
  InetAddress myServer=null;
  public static void main(String[] args)
  {
    Javase_70 Search=new Javase_70();
    System.out.println("您主机的IP地址为:"+ Search.MyIP());
    System.out.println("服务器的IP地址为:"+ Search.ServerIP());
  }
  public InetAddress MyIP()  //获取本地主机IP地址的方法
  {
    try    //获取本机的IP地址
    { myIPaddress=InetAddress.getLocalHost(); }
    catch(UnknownHostException e) {  }
    return (myIPaddress);
  }
  public InetAddress ServerIP()  //获取本机所联服务器IP地址的方法
  {
    try   //获取服务器的IP地址
    { myServer=InetAddress.getByName("www.cat.com"); }
    catch(UnknownHostException e) {  }
    return (myServer);
  }
}
发布了73 篇原创文章 · 获赞 189 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/blog_programb/article/details/105569424