1. 程式人生 > 實用技巧 >MySQL -- 修改root密碼

MySQL -- 修改root密碼

網路程式設計

IP地址

ip地址:inetAddress

  • 唯一定位一臺網路上的計算機

  • 127.0.0.1:本機 localhost

  • ip地址的分類

    • ipv4 四個位元組組成 0-255

    • ipv6 128位 。八個無符號整數!

      2001:1ab2:cccc:0000:0015:1e2d:1314:22bb
      
    • 公網(網際網路) 私網(區域網)

      • ABCD類地址
      • 192.168..xx.xx 專門給組織內部使用
public class Ip {
    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(inetAddress3);
            InetAddress localHost = InetAddress.getLocalHost();
            System.out.println(localHost);
            //百度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 分配和使用者和程式

      • Tomcat:8080
      • MySQL:3306
      • Oracle:1521
    • 動態埠、私有埠:49152-65535

      netstat -ano #檢視所有的埠
      netstat -ano|findstr ""  #檢視指定的埠
      tasklist|findstr ""  #檢視指定埠的程序
      

通訊協議

協議:約定,就好比我現在說的普通話才能交流

網路通訊協議:速率、傳輸位元速率、程式碼結構、傳輸控制

問題:非常的複雜?

大事化小:分層!

TCP/IP協議簇:實際上時一組協議

  • 重要:
    • TCP:使用者傳輸協議
    • UDP:使用者資料報協議
  • 出名的協議:
    • TCP
    • IP:網路互聯協議

TCP UDP對比

TCP:打電話

  • 連線、穩定
  • 三次握手 四次揮手
    • A你愁啥?B瞅你咋地?A幹一場! 最少需要三次才能保證穩定連線
    • A我要走了 B你真的要走了麼? B你真的真的要走了麼? A我真的要走了
  • 客戶端、服務端
  • 傳輸完成,釋放連線、效率低

UDP:發簡訊

  • 不連線、不穩定
  • 客戶端、服務端:沒有明確的界限
  • 不管有沒有準備好,都可以傳送
  • 導彈
  • DDOS:洪水攻擊!飽和攻擊

TCP實現聊天

客戶端

  1. 連線伺服器 Socket
  2. 傳送訊息
//客戶端
public class TcpClientDemo {
    public static void main(String[] args) {
            Socket socket = null;
            OutputStream outputStream = null;
        try {
            //1.知道伺服器的地址
            InetAddress serverIp = InetAddress.getByName("127.0.0.1");
            int port = 9999;
            //2.建立一個socket連線
            socket = new Socket(serverIp,port);
            //3.傳送訊息 IO流
            outputStream = socket.getOutputStream();
            outputStream.write("nihao".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (outputStream != null)
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(socket != null)
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

伺服器

  1. 建立服務埠 ServerSocket
  2. 等待使用者的連線 通過accept
  3. 接受使用者的訊息
//服務端
public class TcpServerDemo {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket accept = null;
        InputStream inputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        try {
            //1.有一個地址
            serverSocket = new ServerSocket(9999);
            //2.等待客戶端連線進來
            accept = serverSocket.accept();
            //3.讀取客戶端的訊息
            inputStream = accept.getInputStream();
            //管道流
            byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len=inputStream.read(buffer)) != -1){
                byteArrayOutputStream.write(buffer,0,len);
            }
            System.out.println(byteArrayOutputStream.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (byteArrayOutputStream != null)
                byteArrayOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(inputStream != null)
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(accept != null)
                accept.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (serverSocket != null)
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

TCP檔案上傳實現

伺服器端

public class TCPServer{
    public static void main(String[] args){
        ServerSocket serverSocket = null;
        Socket accept = null;//阻塞式監聽,會一直等待客戶端連線
        InputStream inputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            //建立服務
            serverSocket = new ServerSocket(9090);
            //監聽客戶端的連線
            accept = serverSocket.accept();
            //獲取輸入流
            inputStream = accept.getInputStream();
            //檔案輸出
            fileOutputStream = new FileOutputStream(new File("基礎語法\\receive.jpg"));
            byte[] buffer = new byte[1024];
            int len;
            while ((len = inputStream.read(buffer)) != -1){
                fileOutputStream.write(buffer,0,len);
            }
            //通知客戶端我接受完畢了
            OutputStream outputStream = accept.getOutputStream();
            outputStream.write("我接受完畢了,可以斷開了".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //關閉資源
            try {
                if (fileOutputStream != null)
                    fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (inputStream != null)
                    inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (accept != null)
                    accept.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (serverSocket != null)
                    serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

客戶端

public class TcpClient{
    public static void main(String[] args) {
        Socket socket = null;
        OutputStream outputStream = null;
        FileInputStream fileInputStream = null;
        InputStream inputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        try {
            //建立Socket連線
            socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
            //建立一個輸出流
            outputStream = socket.getOutputStream();
            //檔案流
            //讀取檔案
            fileInputStream = new FileInputStream(new File("基礎語法\\沙灘.jpg"));
            //寫出檔案
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fileInputStream.read(buffer)) != -1){
                outputStream.write(buffer,0,len);
            }
            //通知伺服器,我已經結束了
            socket.shutdownOutput();//我已經傳輸完了!
            //確定伺服器接受完畢,才能斷開連線
            inputStream = socket.getInputStream();
            byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer2= new byte[1024];
            int len2;
            while ((len2 = inputStream.read(buffer2)) != -1){
                byteArrayOutputStream.write(buffer2,0,len2);
            }
            System.out.println(byteArrayOutputStream.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //關閉資源
            try {
                if (byteArrayOutputStream!=null)
                byteArrayOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (inputStream!=null)
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fileInputStream!=null)
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (outputStream!=null)
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (socket!=null)
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
}

Tomcat

服務端

  • 自定義 S
  • Tomcat伺服器 S java後臺開發!

客戶端

  • 自定義 C
  • 瀏覽器 B

UDP

發簡訊:不用連結 需要知道對方的地址

傳送訊息

  • 傳送端

    //不需要連線伺服器
    public class Demo {
        public static void main(String[] args) throws Exception {
            //建立一個Socket
            DatagramSocket datagramSocket = new DatagramSocket();
            //建個包
            String msg = "nihao";
            //傳送給誰
            InetAddress localhost = InetAddress.getByName("localhost");
             int port = 9999;
             //資料  資料的長度起始  傳送給誰
            DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);
            //傳送包
            datagramSocket.send(datagramPacket);
        }
    }
    
  • 接收端

    等待客戶端的連線
    public class Demo01 {
        public static void main(String[] args) throws Exception {
            //開放埠
            DatagramSocket datagramSocket = new DatagramSocket(9999);
            //接收資料  接受包
            byte[] buffer = new byte[1024];
            DatagramPacket datagramPacket = new DatagramPacket(buffer, 0, buffer.length);
    
            //阻塞接收
            datagramSocket.receive(datagramPacket);
    
            System.out.println(datagramPacket.getAddress().getHostAddress());
            System.out.println(new String(datagramPacket.getData(),0,datagramPacket.getLength()));
    
            //關閉連線
            datagramSocket.close();
        }
    

    迴圈傳送訊息

    public class Sender01 {
        public static void main(String[] args) throws Exception {
            DatagramSocket socket = new DatagramSocket(8888);
            //準備資料  控制檯讀取 System.in
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            while (true){
                String data = reader.readLine();
                byte[] datas = data.getBytes();
                DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 6666));
                socket.send(packet);
                if (data.equals("bye")){
                    break;
                }
            }
            socket.close();
        }
    }
    

    迴圈接收訊息

    public class Receicer01 {
        public static void main(String[] args) throws Exception {
            DatagramSocket socket = new DatagramSocket(6666);
            while (true){
                //準備接受包裹
                byte[] contain= new byte[1024];
                DatagramPacket packet = new DatagramPacket(contain,0,contain.length);
                socket.receive(packet);//阻塞式接受包裹
    
                //斷開連線  bye
                byte[] data = packet.getData();
                String datas = new String(data,0,data.length);
                System.out.println(datas);
                if (datas.equals("bye")){
                    break;
                }
            }
            socket.close();
        }
    }
    

    線上諮詢:兩個人都可以是傳送方,也都可以時接收方 多執行緒實現

    傳送執行緒

    public class TalkSend implements Runnable {
        DatagramSocket socket = null;
        BufferedReader reader = null;
        private int fromPort;
        private String toIp;
        private int toPort;
        public TalkSend(int fromPort, String toIp, int toPort) {
            this.fromPort = fromPort;
            this.toIp = toIp;
            this.toPort = toPort;
            try {
                socket = new DatagramSocket(fromPort);
                reader = new BufferedReader(new InputStreamReader(System.in));
            } catch (SocketException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void run() {
            while (true){
                try {
                    String data = reader.readLine();
                    byte[] datas = data.getBytes();
                    DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress(this.toIp, this.toPort));
                    socket.send(packet);
                    if (data.equals("bye")){
                        break;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            socket.close();
        }
    }
    

    接收執行緒

    public class TalkReceive implements Runnable {
        DatagramSocket socket = null;
        private int port;
        private String msgFrom;
        public TalkReceive(int port,String msgFrom) {
            this.port = port;
            this.msgFrom = msgFrom;
            try {
                socket = new DatagramSocket(port);
            } catch (SocketException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void run() {
            while (true){
                try {
                    //準備接受包裹
                    byte[] contain= new byte[1024];
                    DatagramPacket packet = new DatagramPacket(contain,0,contain.length);
                    socket.receive(packet);//阻塞式接受包裹
                    //斷開連線  bye
                    byte[] data = packet.getData();
                    String datas = new String(data,0,data.length);
                    System.out.println(msgFrom+":"+datas);
                    if (datas.equals("bye")){
                        break;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            socket.close();
        }
    }
    

    學生端

    public class TalkStudent {
        public static void main(String[] args) {
            //開啟兩個執行緒
            new Thread(new TalkSend(7777,"localhost",9999)).start();
            new Thread(new TalkReceive(8888,"老師")).start();
        }
    }
    

    老師端

    public class TalkTeacher {
        public static void main(String[] args) {
            //開啟兩個執行緒
            new Thread(new TalkSend(5555,"localhost",8888)).start();
            new Thread(new TalkReceive(9999,"學生")).start();
        }
    }
    
    

    URL

    統一資源定位符:定位網際網路上的某一個資源

    協議://ip地址:埠/專案名/資源
    
    public class Url {
        public static void main(String[] args) throws MalformedURLException {
            URL url = new URL("http://localhost:8080/helloworld/index.jsp?username=qiu&password=123");
            System.out.println(url.getProtocol());  //協議
            System.out.println(url.getHost()); //主機  ip
            System.out.println(url.getPort());  //埠
            System.out.println(url.getPath());  //檔案
            System.out.println(url.getFile());  //全路徑
            System.out.println(url.getQuery());  //引數
        }
    }
    

    下載網路資源

    public class Url {
        public static void main(String[] args) throws Exception {
            //下載地址
            URL url = new URL("http://localhost:8080/helloworld/index.jsp?username=qiu&password=123");
            //連線到這個資源  HTTP
    
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = urlConnection.getInputStream();
            FileOutputStream fileOutputStream = new FileOutputStream("基礎語法\\abc.txt");
            byte[] buffer = new byte[1024];
            int len;
            while ((len = inputStream.read(buffer)) != -1){
                fileOutputStream.write(buffer,0,len);
            }
            fileOutputStream.close();
            inputStream.close();
            urlConnection.disconnect();
        }
    }