计算机网络

127.0.0.1

IP :互联网协议地址 ipv4和ipv6  设备在网络中的唯一表示

 

DNS把域名解析为了IP

IPv4 4字节 点分十进制

IPv6  16字节  八组  冒分十六进制  去头0,0位压缩

端口号: 找到设备中软件的标识    两个字节表示的整数  0~65535

协议    :计算机网络中,连接和通信的规则被称为计算机网络通信

InetAddress 类使用

  //通过主机名获取主机对象
        InetAddress inetAddress= InetAddress.getByName("DESKTOP-4MKNLPK");
        System.out.println("主机名为"+inetAddress.getHostName());
        System.out.println("IP为"+inetAddress.getHostAddress());

  

 

允许部分数据丢失,使用UDP(用户数据报)协议    无连接 

发送数据 (单播)

 

class Client {
    public static void main(String[] args) throws IOException {

        Scanner scanner =new Scanner(System.in);
        //找码头   数据报 套接字
        DatagramSocket datagramSocket=new DatagramSocket();
        //数据报 封装
        String string=scanner.next();
        byte[] bytes=string.getBytes();
        InetAddress inetAddress=InetAddress.getByName("127.0.0.1");
        int port=10086;
        DatagramPacket datagramPacket=new DatagramPacket(bytes,bytes.length,inetAddress,port);
        //发送数据
        datagramSocket.send(datagramPacket);
        System.out.println("发送成功");
        //释放资源
        datagramSocket.close();
    }
}

接收数据

public class Server {
    public static void main(String[] args) throws IOException {
        //找码头   数据报套接字
        DatagramSocket datagramSocket=new DatagramSocket(10086);
        //装数据报箱子
        byte[] bytes=new byte[1204];
        DatagramPacket datagramPacket=new DatagramPacket(bytes,bytes.length);
        //接受数据
        System.out.println("接受数据");
        datagramSocket.receive(datagramPacket);
        System.out.println("接收成功");
        //获取箱子中的数据
        String string=new String(datagramPacket.getData());
        System.out.println(string);
        //释放资源
        datagramSocket.close();
    }
}

 

UDP(组播)

MulticastSocket ms=new MulticastSocket(port)//接收端

//把当前计算机绑定一个人组播地址

//组播地址 224.0.0.1~239.255.255.255  组播的预留地址为 224.0.0.1~224.0.0.255

发送数据

class Client {
    public static void main(String[] args) throws IOException {
        //套接字
        Scanner scanner =new Scanner(System.in);
        DatagramSocket datagramSocket=new DatagramSocket();
        //装 数据报的箱子
        String string=scanner.next();
        byte[] bytes=string.getBytes();
        InetAddress inetAddress=InetAddress.getByName("224.0.0.15");
        int port=14752;
        DatagramPacket datagramPacket=new DatagramPacket(bytes,bytes.length,inetAddress,port);
        //发送数据
        datagramSocket.send(datagramPacket);
        System.out.println("发送成功");
        //释放资源
        datagramSocket.close();
    }
}

接收数据

public class Server {
    public static void main(String[] args) throws IOException {
        //创建 广播套接字对象
        MulticastSocket multicastSocket=new MulticastSocket(14752);
        //创建数据报报对象
        byte[] arr=new byte[1024];
        DatagramPacket datagramPacket=new DatagramPacket(arr,arr.length);
        //加入组播组
        multicastSocket.joinGroup(InetAddress.getByName("224.0.0.15"));
        multicastSocket.receive(datagramPacket);
        String s=new String(datagramPacket.getData());
        System.out.println(s);
        //释放资源
        multicastSocket.close();
    }
}

  

UDP (广播)

广播 InetAddressdd255.255.255.255

 

 2 不允许任何数据丢失,是同TCP(传输控制协议)协议    面向连接

发送端

/*
//套接字  两台机器间通信的端点
Scoket sk=new Socket(host,port);
//获取IO流开始写写数据
OutputStream os=socket.getOutputStream();
//将字节输出流转换为字符输出流
OutputStreamWriter osw=new OutputStreamWriter(os);
BufferedWriter bw=new BufferedWriter(osw);
 */
public class SocketClient {
    public static void main(String[] args) throws IOException {
        //创建套接字对象
        Socket socket=new Socket("192.168.11.61",10000);
        //创建字节输出流对象
        OutputStream outputStream=socket.getOutputStream();
        //字节输出流转换为字符输出流
        OutputStreamWriter outputStreamWriter=new OutputStreamWriter(outputStream);
        BufferedWriter bufferedWriter=new BufferedWriter(outputStreamWriter);
        bufferedWriter.write("带骨头的狗");
        bufferedWriter.newLine();
        bufferedWriter.close();
        //释放资源
        socket.close();

    }
} 

接收数据

//客户端连接服务器三次握手
//在最后一步断开链接,通过四次挥手协议保证连接终止
public class Server {
    public static void main(String[] args) throws IOException {
        //创建服务器端的套接字对象
        ServerSocket socket=new ServerSocket(10000);
        //监听客户端连接 阻塞方法
        Socket accept=socket.accept();
        //创建字节输入流对象,接受
        InputStream inputStream=accept.getInputStream();
        //字节输入流转换成字符输入流
        InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
        BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
          //readLine阻塞方法,传输的数据报中没有结束符标记
        System.out.println(bufferedReader.readLine());
        //在关IO流的时候,多了一个往服务器理写结束标记的动作
        bufferedReader.close();
        socket.close();
    }
}

  三次握手 粗浅理解,不是计算机网络

 

   四次挥手  粗浅理解

  

 

 

 

数据交互小练习

客户端

public class Client01 {
    public static void main(String[] args) throws IOException {
        //创建套接字对象
        Socket socket=new Socket("127.0.0.1",10086);
        //创建字节输出流输出到套接字对象中
        OutputStream outputStream=socket.getOutputStream();
        //将字节输出流转换为字符输出流
        OutputStreamWriter outputStreamWriter=new OutputStreamWriter(outputStream);
        BufferedWriter bufferedWriter=new BufferedWriter(outputStreamWriter);
        bufferedWriter.write("大白鹅");
        bufferedWriter.newLine();
        bufferedWriter.flush();
        socket.shutdownOutput();

      //接受服务器的回复
        InputStream inputStream=socket.getInputStream();
        //字节输入流转换为字符数入流
        InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
        BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
        System.out.println(bufferedReader.readLine());


        bufferedWriter.close();
        bufferedReader.close();
        socket.close();

    }
}

  服务器端

public class Server {
    public static void main(String[] args) throws IOException {
        //创建服务器端 套接字对象
      ServerSocket serverSocket=new ServerSocket(10086);
      // 创建连接 阻塞任务
      Socket accept=serverSocket.accept();
      //创建字节输入流
        InputStream inputStream=accept.getInputStream();
        //字节输入流转换为字符数入流
        InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
        BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
        System.out.println(bufferedReader.readLine());


        //回复客户端
        OutputStream outputStream=accept.getOutputStream();
        //将字节输出流转换为字符输出流
        OutputStreamWriter outputStreamWriter=new OutputStreamWriter(outputStream);
        BufferedWriter bufferedWriter=new BufferedWriter(outputStreamWriter);
        bufferedWriter.write("难道你就是传说中的大白鹅");
        bufferedWriter.close();
        bufferedReader.close();

        accept.close();

    }
}

文件里的数据交互小练习

  优化 服务端

            文件名随机且唯一(有很小小小的几率重复) UUID

      reandomUUID()

       toString().replace()

   改为多线程

   加线程池

 

类 InetAddress   

ipconfig 查看本机IP地址

私有构造  成员方法

public class MyInetAddress {
    public static void main(String[] args) throws UnknownHostException {
        //通过ip获取主机对象
        InetAddress inetAddress= InetAddress.getByName("192.168.56.1");
        System.out.println("主机名为"+inetAddress.getHostName());
        System.out.println("IP为"+inetAddress.getHostAddress());
        /*byte[] arr="192.168.56.1".getBytes();
        InetAddress inetAddress=InetAddress.getByAddress(arr);
        System.out.println(inetAddress.getHostName());*/

    }
}

  

posted @ 2021-10-22 12:09  互联.王  阅读(105)  评论(0编辑  收藏  举报