Day08_网络编程(上)

Day08_网络编程(上)

javaWeb:网页编程,B/S

网络编程:TCP/IP,C/S

网络编程中两个主要的问题

  • 如何准确的定位到网络上一台或者多台主机
  • 找到主机后如何进行通信

网络编程中的要素

  • IP和端口号
  • 网络通信协议 UDP,TCP

IP

IP地址:InetAddress

  • 唯一定位一台网络上计算机
  • 127.0.0.1 :本机localhost
  • IP地址分类:
    • IPv4,IPv6
    • 公网,私网

InetAddress

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 inetAddress2= InetAddress.getByName("localhost");
            System.out.println(inetAddress2);
            InetAddress inetAddress3= InetAddress.getLocalHost();
            System.out.println(inetAddress3);
            System.out.println("====================");
            //查询网站IP地址
            InetAddress inetAddress4= InetAddress.getByName("www.baidu.com");
            System.out.println(inetAddress4);
            //常用方法
            System.out.println(inetAddress4.getCanonicalHostName());//规范的名字
            System.out.println(inetAddress4.getHostAddress());//ip
            System.out.println(inetAddress4.getHostName());//域名,或者自己电脑的名字
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

输出:

/127.0.0.1
localhost/127.0.0.1
bogon/192.168.1.51
====================
www.baidu.com/111.206.223.173
111.206.223.173
111.206.223.173
www.baidu.com

端口

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

  • 不同的进程有不同的端口号,用来区分软件

  • 被规定0-65535

  • TCP,UDP每个协议都有65535个端口可以同时用,故同时可以调用65535*2个端口。 例如:tcp和udp可以同时用80端口。但是单个协议下端口不能冲突。

  • 端口分类

    • 公有端口1-1023,一般会被内置进程或者服务器使用。
      • http:80
      • https:43
      • FTP:21
      • Telnet:23
    • 程序注册端口:1024-49151,分配给用户或者程序
      • tomcat:8080
      • MySQL:3306
      • Oracle:1521
    • 动态、私有:49152-65535

    InetSocketAddress类

    import java.net.InetSocketAddress;
    
    public class testInetSocketAddress {
        public static void main(String[] args) {
            InetSocketAddress socketAddress= new InetSocketAddress("127.0.0.1",8080);
            InetSocketAddress socketAddress2= new InetSocketAddress("localhost",8080);
            System.out.println(socketAddress);
            System.out.println(socketAddress2);
    
            System.out.println(socketAddress.getAddress());
            System.out.println(socketAddress.getHostName());
            System.out.println(socketAddress.getPort());
        }
    }
    

    输出:

    /127.0.0.1:8080
    localhost/127.0.0.1:8080
    /127.0.0.1
    localhost
    8080
    

通信协议

TCP:用户传输协议

UDP:用户数据报协议

IP:网络互联协议

TCP和IP协议实际是一组协议

TCP:

  • 面向连接,稳定
  • 三次握手四次挥手
  • 客户端,服务端
  • 传输完成,释放连接,效率低

UDP

  • 无连接,不稳定
  • 客户端,服务端:没有明显的界限
  • 不管有没有准备好,都可以发给你
  • DDos:洪水攻击!(饱和攻击)

TCP

服务器

  1. 建立服务的端口ServerSocket
  2. 等待用户的连接 accept
  3. 接受用的消息
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 dataFrame=null;

        try {
            //1.服务端提供IP地址和端口
            serverSocket=new ServerSocket(9999);
            while(true){
                //2.等待客户端连接
                socket=serverSocket.accept();
                //3.读取客户端的消息
                is=socket.getInputStream();

                //管道流
                dataFrame=new ByteArrayOutputStream();
                byte[] buffer=new byte[1024];
                int len;
                while((len=is.read(buffer))!=-1){
                    dataFrame.write(buffer,0,len);
                }
                System.out.println(dataFrame.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if (dataFrame!=null){
                try {
                    dataFrame.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();
                }
            }
        }
    }
}

客户端

  1. 连接服务器Socket
  2. 发送消息
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) throws IOException {
        Socket socket=null;
        OutputStream os=null;

        try {
            //1.要知道服务端的IP地址和端口号
            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 (IOException e) {
            e.printStackTrace();
        } finally {
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

服务端输出:

你好,我是客户端!

之前没看IO流的使用,现在没搞懂IO流,明天去补IO流的课!

原文地址:https://www.cnblogs.com/gaoyao/p/13357253.html