概述

1.1什么是网络

1.2网络通信的要素

主要问题
1、如何准确定位到网络上的主机
2、找到主机之后如何通信
如何实现网络的通信
1、通信的双方地址:
IP地址
端口号
2、规则:网络通信协议
TCP/IP参考模型
OSI参考模型

IP地址

  1. import java.net.InetAddress;
  2. import java.net.UnknownHostException;
  3. //测试IP
  4. public class TestInetAddress {
  5. public static void main(String[] args) {
  6. try {
  7. //创建对应IP的对象
  8. //静态方法,通过名字返回对象
  9. InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
  10. System.out.println(inetAddress1);
  11. //查询网站IP地址
  12. InetAddress inetAddress2 = InetAddress.getByName("www.baodu.com");
  13. System.out.println(inetAddress2);
  14. InetAddress inetAddress3 = InetAddress.getByName("localhost");
  15. System.out.println(inetAddress3);
  16. //获取本机地址
  17. InetAddress inetAddress4 = InetAddress.getLocalHost();
  18. System.out.println(inetAddress4);
  19. //常用方法
  20. System.out.println(inetAddress2.getCanonicalHostName());//获取规范域名
  21. System.out.println(inetAddress2.getHostAddress());//获取IP地址
  22. System.out.println(inetAddress2.getHostName());//域名,或者自己电脑名
  23. } catch (UnknownHostException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. }

端口

端口表示计算机上的一个程序的进程

  1. 不同的进程有不同的端口号,用来区分软件
  2. 被规定:0~65536
  3. TCP,UDP,每一种协议都有0~65536个端口,不同的协议端口相同不会产生影响
  4. 端口分类

    1. 公有端口:0~ 1023
      1. HTTP:80
      2. HTTPS:443
      3. FTP:21
      4. Telent:23
    2. 程序注册端口:2014~49151,分配给用户或者程序
      1. tomcat :8080
      2. MYSQL:3306
      3. Orcal:1521
    3. 动态、私有:49152~65535

      1. netstat -ano #查看所有的端口
      2. netstat -ano|findstr "5900" #查询5900端口
      3. tasklist|finder "8696" #查看指定端口的进程
      1. import java.net.InetSocketAddress;
      2. public class TestSockAddress {
      3. public static void main(String[] args) {
      4. //创建端口和IP对象
      5. InetSocketAddress socketAddress1 = new InetSocketAddress("127.0.0.1", 8080);
      6. InetSocketAddress socketAddress2 = new InetSocketAddress("localhost", 8080);
      7. System.out.println(socketAddress1);
      8. System.out.println(socketAddress2);
      9. System.out.println(socketAddress1.getAddress());
      10. System.out.println(socketAddress1.getHostName());
      11. System.out.println(socketAddress1.getPort());
      12. }
      13. }

      通信协议

      TCP/IP协议簇
      TCP:用户传输协议
      TDP:
      IP:网络互联协议
      image.png

      TCP

      编写客户端和服务器 ```java import java.io.IOException; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket;

public class TcpClientDemo1 { public static void main(String[] args) throws IOException { OutputStream os = null; Socket socket=null; try { //创建一个ip地址对象 InetAddress serverIP = InetAddress.getByName(“127.0.0.1”); //端口号 int port = 9999; //创建一个socket连接 socket = new Socket(serverIP, port); //发送消息IO流 os=socket.getOutputStream(); os.write(“你好,这是一个测试信息”.getBytes()); } catch (Exception e) { e.printStackTrace(); }finally { if (os != null) os.close(); if (socket != null)socket.close(); }

  1. }

}

  1. ```java
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.net.ServerSocket;
  7. import java.net.Socket;
  8. public class TcpServerDemo1 {
  9. public static void main(String[] args) throws IOException {
  10. ServerSocket serverSocket=null;
  11. InputStream is = null;
  12. Socket socket=null;
  13. ByteArrayOutputStream baos = null;
  14. try {
  15. //创建一个地址对象
  16. serverSocket = new ServerSocket(9999);
  17. //等待用户连接.accept方法会把线程阻塞,知道接收客户端发送的信息
  18. socket=serverSocket.accept();
  19. //读取用户传递的信息
  20. is = socket.getInputStream();
  21. //管道流
  22. baos = new ByteArrayOutputStream();
  23. byte[] buffer = new byte[1024];
  24. int len;
  25. while ((len = is.read(buffer)) != -1) {
  26. baos.write(buffer, 0, len);
  27. }
  28. System.out.println(baos.toString());
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }finally {
  32. //关闭资源
  33. if (baos != null)baos.close();
  34. if (is != null)is.close();
  35. if (socket != null)socket.close();
  36. if (serverSocket != null)serverSocket.close();
  37. }
  38. }
  39. }