“开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 2 天,点击查看活动详情”
1.网络编程的概述
1)地球村:我们在北京,但是我有一个其他国家的朋友。
2)什么是计算机网络?
计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管鲤软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。
3)网络编程的目的
无线电台:传播交流信息,数据交换,通信。
4)想要达到这个效果需要什么?
a)如何准确定位到网络上的主机:例如192.168.16.124端口,定位到该计算机上的某个资源。
b)找到主机后如何进行数据的传输?
javaweb:网页编程 B/S
网络编程:TCP/IP C/S
2.IP协议
新建一个java的普通项目工程
1.查询本机地址:
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);
InetAddress inetAddress2=InetAddress.getByName("localhost");
System.out.println(inetAddress2);
InetAddress inetAddress3=InetAddress.getLocalHost();
System.out.println(inetAddress3);
运行结果如下:
2.查询某个网站的ip地址:
运行结果如下:
3.常用的方法:
System.out.println(inetAddress4.getAddress());
//规范的名字:
System.out.println(inetAddress4.getCanonicalHostName());
//ip
System.out.println(inetAddress4.getHostAddress());
//域名或者自己的电脑名字
System.out.println(inetAddress4.getHostName());
运行结果如下:
程序代码如下:
public class TestInetAddress {
public static void main(String[] args) {
//查询本机地址
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);
InetAddress inetAddress2=InetAddress.getByName("localhost");
System.out.println(inetAddress2);
InetAddress inetAddress3=InetAddress.getLocalHost();
System.out.println(inetAddress3);
//查询网站的ip地址
InetAddress inetAddress4=InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress4);
}
}
“开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 2 天,点击查看活动详情”