【227天】黑马程序员27天视频学习笔记【Day26-下】

Java填坑之路 2019-06-25

叨叨两句

被意外之事干扰了,累死了,但感觉挺好
这篇笔记还需要进一步优化。

26-07: 网络编程(UDP传输优化)

  1. 接收端Receive
package test;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Demo2_receive {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(6666); //创建码头
        DatagramPacket packet = new DatagramPacket(new byte[1024],1024);  //创建集装箱rue
        while(true) {
            socket.receive(packet);//接货,接收数据
            byte[] arr = packet.getData();
            int len = packet.getLength();
            String ip = packet.getAddress().getHostAddress();
            int port = packet.getPort();
            System.out.println(ip + ":" + port + ":" + new String(arr,0,len));
        }
    }
}
  1. 发送端Send
package test;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class Demo1_send {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        DatagramSocket socket = new DatagramSocket();//创建码头
        while(true) {
            String line = sc.nextLine();
            if("quit".equals(line)) {
                break;
            }
            DatagramPacket packet = 
                new DatagramPacket(line.getBytes(),line.getBytes().length,InetAddress.getByName("127.0.0.1"),6666);//创建集装箱,将数据装进去
            socket.send(packet);//从码头中将集装箱发出去
        }
        socket.close();//关闭码头(底层是IO流)
    }    
}

26-08:网络编程(UDP传输多线程)

  1. 发送和接收在一个窗口完成
package test;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Demo5 {
    public static void main(String[] args) {
        new Receive().start();
        new Send().start();
    }
}

class Receive extends Thread {
    public void run(){
        try {
            DatagramSocket socket = new DatagramSocket(6667);
            DatagramPacket packet = new DatagramPacket(new byte[1024],1024);
            
            while(true) {
                socket.receive(packet);
                
                byte[] arr = packet.getData();
                int len = packet.getLength();
                String ip = packet.getAddress().getHostAddress();
                int port = packet.getPort();
                System.out.println(ip + ":" + port + ":" + new String(arr,0,len));
            }
        
        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

class Send extends Thread {
    public void run() {
        try {
            Scanner sc = new Scanner(System.in);
            DatagramSocket socket = new DatagramSocket();
            
            while(true) {
                String line = sc.nextLine();
                if("quit".equals(line)) {
                    break;
                }
                DatagramPacket packet = 
                        new DatagramPacket(line.getBytes(),line.getBytes().length,InetAddress.getByName("127.0.0.1"),6667);
                socket.send(packet);    
            }
            socket.close();
        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    
    }
}

26-(09-15):UDP

UDP聊天图形化界面

UDP聊天发送功能

UDP聊天记录功能

UDP聊天清屏功能

UDP聊天震动功能

UDP聊天快捷键和代码优化

UDP聊天生成jar文件

留坑待填

26-16:网络编程(TCP协议)

  1. 客户端

    1. 创建Socket连接服务端(指定ip地址,端口号)通过ip地址找对应的服务器
    2. 调用Socket的getInputStream()和getOutputStream()方法获取和服务端相连的IO流
    3. 输入流可以读取服务端输出流写出的数据
    4. 输出流可以写出数据到服务端的输入流
  2. 服务端

    1. 创建ServerSocket(需要指定端口号)
    2. 调用ServerSocket的accept()方法接收一个客户端请求,得到一个Socket
    3. 调用Socket的getInputStream()和getOutputStream()方法获取和客户端相连的IO流
    4. 输入流可以读取客户端输出流写出的数据
    5. 输出流可以写出数据到客户端的输入流

26-17:网络编程(TCP协议代码优化)

  1. 客户端
留坑待填
  1. 服务端
留坑待填

26-18:网络编程(服务端是多线程的)

留坑待填

26-(19-20):网络编程(练习)

留坑待填

相关推荐