import jdk.jfr.StackTrace;
import org.junit.Test;
import java.lang.annotation.Target;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* 网络编程
*
* 一、目的:与其他计算机实现数据交换,进行通讯
*
* 二、网络编程中主要问题
* 1.定位主机(IP),定位应用(端口号)
* 2.如何进行高可靠性传输数据(网络通信协议)
*
* 三、网络编程中的两要素
* 1、(1)IP: 域名:
*
* (2)端口号:(16位)标识正在计算机上运行的进程
* (0-1023)公认端口
* (1024-49151)注册端口
* (49152-65535)动态/私有端口
*端口号+IP ----> 网络套接字 Socket
* 2、网络通信协议
* (1)、TCP/IP参考模型
* 应用层
* 传输层
* 网络层
* 数据链路层+物理层
* (2)传输层:UDP、TCP协议
* TCP:TCP三次握手
* 四次挥手
* 可靠连接
* 两个通信应用进程:客户端、服务端
* 可进行大数据量传输
* 效率低
* //打电话
* UDP:不可靠连接
* 无需建立连接
* 数据限制64k
* 可以广播
* 发送数据结束无需释放资源,开销小,速度快
* //视频、发短信、发电报
*
*
* (3)网络层:IP协议
*
*
* @author orz
*/
public class Test1 {
//ip地址
/**
* 1.使用InetAddress代表IP
* 2.IP分类:IPV6、IPV4;万维网、局域网
* 3.域名: 如 www.baidu.com
* 4.回路地址:127.0.0.1 localhost(本机)
* 5.如何实例化InetAddress:两个方法:getByName(String host)
* getLocalHost()
* 两个常用方法:getHostName()
* getHostAddress()
*
*/
@Test
public void test1()
{
try {
//1.byIP地址
InetAddress inetAddress=InetAddress.getByName("192.168.0.1");
//2.by域名
InetAddress inetAddress1=InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress);
System.out.println(inetAddress1);
//3.本地地址 InetAddress.getLocalHost()
InetAddress inetAddress2=InetAddress.getLocalHost();
System.out.println(inetAddress2);
//getHostName()
System.out.println(inetAddress1.getHostName());
//getHostAddress()
System.out.println(inetAddress1.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
/**
* 端口号:标识正在计算机上运行的进程
*
*/
/**
* Socket
*/
@Test
public void test2()
{
}
}