IP
IP地址:Internet Protocol
-
唯一定位一台网络上的计算机
-
127.0.0.1:本地 localhost
-
ip地址分类
- ipv4、ipv6
- ipv4:127.0.01 ,四个字节组成,0-255, 42亿;30亿都在北美,亚洲 4亿。2011年就用尽了;
- ipv6:fe80::79ee:9ad9:6e79:da76%2,128位。8个无符号整数
- 公网(互联网) - 私网(局域网)
- ABCD类地址
- 192.168.xx.xx,专门给组织内部使用的
- ipv4、ipv6
-
域名:记忆IP问题!
-
package com.ljb.demo3; import java.net.InetAddress; import java.net.UnknownHostException; public class TestInetAddress { public static void main(String[] args) { try { //查询本地地址 InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1"); System.out.println(inetAddress1); InetAddress inetAddress3 = InetAddress.getByName("localhost"); System.out.println(inetAddress1); InetAddress inetAddress4 = InetAddress.getLocalHost(); System.out.println(inetAddress1); //查询网站ip地址 InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com"); System.out.println(inetAddress2); }catch (UnknownHostException e){ e.printStackTrace(); } } }
-
端口
端口表示计算机上的一个程序的进程
-
不同的进程有不同的端口号,用来区分软件
-
被规定0-65535
-
TCP,UDP:65535*2 tcp:80,udp:80。但是单个协议下端口号不能冲突
-
端口分类:
-
共有端口0~1023
- HTTP:80
- HTTPS:443
- FTP:21
- Telent: 23
-
程序注册端口:1024~49151,分配给用户或者程序
- Tomct:8080
- MySQL:3306
- Oracle:1521
-
动态、私有:49152~65535
-
netstat -ano #查看所有端口 netstat - anofindstr "8696" #查看指定端口 tasklist|findstr "8696" #查看指定端口的进程
-
通信协议
网络通信协议:速率,传输码率,代码结构,传输控制…
TCP/IP协议簇实际是一组协议
重要的协议:
- TCP:用户传输协议
- UDP:用户数据报协议
出门的协议:
- TCP:
- IP:网络互联协议
TCP UDP对比
TCP:打电话
-
连接稳定
-
“三次握手”,“四次挥手”:
-
最少需要三次,保证稳定连接! A:你愁啥? B:瞅你乍地? C:干一场 A:我要断开了 B:我知道你要断开了 B:你真的断开了吗 A:我真的断开了
-
客户端、服务端
-
传输完成,释放连接,效率低
UDP:发短信
- 不连接,不稳定
- 客服端、服务端:没有明确的界限
- 不管有没有准备好,都可以发给你
TCP
服务端
- 建立服务的端口ServerSocket
- 等待用户的连接accpet
- 接受用户的消息
客户端
- 连接服务器Socket
- 发送消息
-
服务端
-
package com.ljb.demo3; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; //服务端 public class TcpServerDemo01 { public static void main(String[] args) { ServerSocket serverSocket = null; Socket socket = null; InputStream is = null; ByteArrayOutputStream baos = null; try { //1.我得有一个地址 serverSocket = new ServerSocket(9999); while (true) { //2.等待客户端连接过来 socket = serverSocket.accept(); //3.读取客户端得消息 is = socket.getInputStream(); //管道流 baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) != -1) { baos.write(buffer, 0, len); } System.out.println(baos.toString()); } } catch (IOException e) { e.printStackTrace(); }finally { //关闭资源 if (baos!=null){ try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } if (is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket!=null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if (serverSocket!=null){ try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
-
客户端
-
package com.ljb.demo3; import java.io.IOException; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; //客户端 public class TCPClientDemo01 { public static void main(String[] args) { Socket socket = null; OutputStream os = null; try { //1.要知道服务器的地址,端口号 InetAddress serverIP = InetAddress.getByName("127.0.0.1"); int port = 9999; //2.创建一个socket连接 socket = new Socket(serverIP,port); //3.发送消息IO流 os = socket.getOutputStream(); os.write("你好".getBytes()); } catch (Exception e) { e.printStackTrace(); }finally { if (socket!=null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if (os!=null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
文件上传
Tomcat
UDP
URL
统一资源定位符:定位资源的,定位互联网上的某一个资源
协议://ip地址:端口号/项目名/资源