[Java基础] 网络编程

一:TCP/IP模型

  OSI参考模型为七个层面:应用层,表示层,会话层,传输层,网络层,数据链路层,物理层;

  TCP/IP模型将这七个层面简化成四个层面:前三个层面简化成了:应用层,最后两个层面简化成了:网络接口层;

二:TCP/UDP

  TCP协议:是一种面向连接,可靠的,基于字节流的传输层通讯协议,数据大小无限制,建立连接和断开连接需要三次握手四次挥手,是一种长连接

  UDP协议:是一种无连接的传输层协议,提供面向事务的简单不可靠信息传输服务,每个包的大小为64KB

三:IP/PORT

  IP是一个分配给互联网设备的唯一标识,port是一部设备的端口,一般来讲8000以内的端口都被设备的硬件软件服务给占领了

  而一组IP和port能确定一个唯一的连接,IP确定设备,port确定数据的传输端口

四:Socket/ServerSocket

  Client类

  

/**
 * 
 */
package SocketDemo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @description:
 * @author Lzzy
 * @date 2020年8月12日
 *
 */
public class Client {
    public static void main(String[] args) {
        try {

            Socket s = new Socket("127.0.0.1", 8808);
            // 写的run() 当写入exit时向对方写入一个exit告知要结束交流
            Runnable write = () -> {

                while (true) {
                    OutputStream os = null;
                    try {
                        os = s.getOutputStream();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    Scanner input = new Scanner(System.in);
                    String str = input.nextLine();

                    if (str.equals("exit")) {
                        try {

                            os.write("exit".getBytes());
                            os.flush();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        input.close();
                        break;

                    } else {

                        try {
                            os.write(str.getBytes());
                            os.flush();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        // TODO Auto-generated catch block

                    }

                }
            };
            // 读的run()方法,当读到了exit后结束此方法
            Runnable read = () -> {
                while (true) {
                    InputStream is = null;
                    try {
                        is = s.getInputStream();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    byte c[] = new byte[1024];
                    try {

                        is.read(c);
                        if (!new String(c).trim().equals("exit")) {
                            System.out.println(new String(c));
                        } else {

                            break;
                        }

                    } catch (IOException e) {
                        // TODO Auto-generated catch block

                    }

                }
            };
            ExecutorService es = Executors.newFixedThreadPool(2);
            es.submit(read);
            es.submit(write);

            es.shutdown();
            while (es.isTerminated()) {

                s.close();

            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

  Server类

  

/**
 * 
 */
package SocketDemo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @description:
 * @author Lzzy
 * @date 2020年8月12日
 *
 */
public class Server {

    public static void main(String[] args) {
        try {

            ServerSocket ss = new ServerSocket(8808);
            Socket s = ss.accept();
            System.out.println("一个连接进入...");
            // 写的run() 当写入exit时向对方写入一个exit告知要结束交流
            Runnable write = () -> {
                while (true) {
                    OutputStream os = null;
                    try {
                        os = s.getOutputStream();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    Scanner input = new Scanner(System.in);
                    String str = input.nextLine();
                    if (str.equals("exit")) {

                        input.close();
                        try {
                            os.write("exit".getBytes());
                            os.flush();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        break;

                    } else {
                        try {

                            os.write(str.getBytes());
                            os.flush();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                        }
                    }
                }
            };
            // 读的run()方法,当读到了exit后结束此方法
            Runnable read = () -> {
                while (true) {

                    byte c[] = new byte[1024];
                    try {
                        InputStream is = s.getInputStream();

                        is.read(c);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block

                    }
                    if (!new String(c).trim().equals("exit")) {
                        System.out.println(new String(c));
                    } else {

                        break;
                    }
                }
            };
            ExecutorService es = Executors.newFixedThreadPool(2);
            es.submit(read);
            es.submit(write);

            es.shutdown();
            while (es.isTerminated()) {

                s.close();
                ss.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

 

上一篇:6.springboot----------JSR303校验


下一篇:SpringBoot自定义JSR303校验注解