您的位置:首页 > 理论基础 > 计算机网络

Java基础—网络编程

2015-07-20 15:19 549 查看
要素:
IP地址
端口号
传输协议

package com.Train;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

public class NetTest {

public static void main(String[] args) throws UnknownHostException{

/** ===========    IP地址被封装为 InetAddress对象  ============ */

//This class represents an Internet Protocol (IP) address
//通过InetAddress类的静态方法获取IP对象:
/**获取环回IP InetAddress iaLoopback = InetAddress.getLoopbackAddress();
* 获取本机IP InetAddress iaLocalHost = InetAddress.getLocalHost();
* 根据主机名获取IP
*  static InetAddress getLocalHost() 返回本地主机。
*  static InetAddress[] getAllByName(String host) 在给定主机名的情况下,根据系统上配置的名称服务返回其 IP 地址所组成的数组。
*  static InetAddress getByAddress(byte[] addr) 在给定原始 IP 地址的情况下,返回 InetAddress 对象。
*  static InetAddress getByAddress(String host, byte[] addr) 根据提供的主机名和 IP 地址创建 InetAddress。
*  static InetAddress getByName(String host) 在给定主机名的情况下确定主机的 IP 地址。
*
* 获得的InetAddress类的对象,里面封装了获取主机名和IP地址的非静态方法
* */
InetAddress iaLocalHost = InetAddress.getLocalHost();
System.out.println(iaLocalHost.toString());
System.out.println(iaLocalHost.getHostName());
System.out.println(iaLocalHost.getHostAddress());
System.out.println("------------------");

InetAddress ia = InetAddress.getByName("www.baidu.com");
System.out.println(ia.getHostName());
System.out.println(ia.getHostAddress());
System.out.println("------------------");

//TCP:面向连接--可靠-三次握手
/*    三次握手建立连接:
在吗?
在!
我知道你在了!
*/
//UDP:面向无连接---不可靠、速度快
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: