1.InetAddress类

package com.ydlclass.sorketTest;

import org.junit.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

public class SorketTest {
    @Test
    public void inetAddressTest() throws UnknownHostException {
        InetAddress address = InetAddress.getLoopbackAddress();//获取环回地址
        System.out.println(address);//有自己的tostring方法

        InetAddress[] allByName = InetAddress.getAllByName("www.baidu.com");//通过域名或者主机名获得
        System.out.println(Arrays.toString(allByName));//一个域名可能存在多个ip地址,dns负载均衡,提供更大的并发。

        InetAddress[] localhosts = InetAddress.getAllByName("localhost");
        System.out.println(Arrays.toString(localhosts));//能拿到两个地址,分别为ipv4的地址,以及一个ipv6的地址(虚拟网卡)

        InetAddress[] allByName1 = InetAddress.getAllByName("DESKTOP-OGQ05FQ");
        System.out.println(Arrays.toString(allByName1));//通过主机名,获得多个实际的网卡
        
        byte[] bytes = {127,0,0,1};
        InetAddress byAddress = InetAddress.getByAddress(bytes);
        System.out.println(byAddress);//通过byte数组获


        /**
         * localhost/127.0.0.1
         * [www.baidu.com/
         * [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1]
         * /127.0.0.1
         */


    }
}

2.URL类

package com.ydlclass.sorketTest;

import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;
import java.util.Arrays;

public class SorketTest {
    @Test
    public void urlTest() throws IOException {
        //URL统一资源定位符,是一种定位资源的字符串;这个字符串有几个部分组成:协议://主机:端口号/path?query@fragement
        //https默认端口为443,http默认端口为80,可以不写;
        //或者file:///D:/a.txt
        URL url = new URL("https://dldir1.qq.com/qqfile/qq/PCQQ9.6.1/QQ9.6.1.28732.exe");
        String host = url.getHost();
        System.out.println(host);//获取文件的主机名

        String path = url.getPath();
        System.out.println(path);//获取文件所在的路径

        System.out.println(url.getProtocol());//输出协议

        String file = url.getFile();
        System.out.println(file);

        URLConnection urlConnection = url.openConnection();//使用连接下载文件,会抛出IOException
        InputStream inputStream = urlConnection.getInputStream();
        OutputStream outputStream = new FileOutputStream("D:\\qq\\qq.exe");
        byte[] buffer = new byte[10*1024*1024];
        int len;
        while ((len = inputStream.read(buffer)) != -1){
            outputStream.write(buffer,0,len);
        }

    }

    @Test
    public void urlTest2() throws IOException {
        //使用file协议,copy一份文件放在指定的目录中;
        URL url = new URL("file:///D:/qq/qq.exe");
        URLConnection urlConnection = url.openConnection();
        InputStream inputStream = urlConnection.getInputStream();
        OutputStream outputStream = new FileOutputStream("D:\\qq\\qqcopy.exe");
        byte[] buffer = new byte[10*1024*1024];
        int len;
        while ((len = inputStream.read(buffer)) != -1){
            outputStream.write(buffer);
        }
    }
}

3.ServerSocket与Socket类的使用,以及使用Socket实现一个简单的tcp

package com.ydlclass.sorketTest;

import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;
import java.util.Arrays;

public class SorketTest {
   
    @Test
    public void serverSocketeTest() throws IOException {
        //socket编程的几个步骤:服务器端调用socket()方法建立socket——>
        // bind()方法绑定在一个端口上->listen()方法监听->调用accept()方法阻塞,等待客户端的连接;

        //客户端: 调用socket()方法建立一个套接字->调用connect()方法连接->一旦连接成功,
        // 客户端和服务器端就可以相互之间发送接收数据了


        //步骤:创建serverSorket()
        ServerSocket serverSocket = new ServerSocket();
        //绑定在某一个端口上
        serverSocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(),9999));//
        //服务端可以需要传入一个前面的地址,但是客户端使用socket进行连接时需要制定连接哪个服务器的地址
        //bind方法中是需要一个InetSockerAddress对象
        Socket socket = serverSocket.accept();//accept()方法会阻塞在这里;

        InputStream inputStream = socket.getInputStream();
        OutputStream outputStream = socket.getOutputStream();
        int len;
        byte[] buffer = new byte[1024*1024];
        while ((len = inputStream.read(buffer)) != -1){
            System.out.println(new String(buffer,0,len));//服务器端使用输入流接住客户端的发送出来的消息
        }
        outputStream.close();
        inputStream.close();

    }

    @Test
    public void clientTest() throws IOException {
        //创建socket
        Socket client = new Socket();
        //使用socket进行连接
        client.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(),9999));
        //服务器可能会存在一个独立的ip地址

        //客户端简单的发送消息至服务端
        InputStream inputStream = client.getInputStream();
        OutputStream outputStream = client.getOutputStream();
        outputStream.write("hello server!".getBytes());
        outputStream.close();
        inputStream.close();
    }
}

4.使用socket实现一个udp连接

package com.ydlclass.sorketTest;

import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;
import java.util.Arrays;

public class SorketTest {
   
    @Test
    public void serverUdpTest() throws IOException {

        //socket实现udp

        //创建DatagramSocket
        DatagramSocket udpServer = new DatagramSocket(8080);
        byte[] buffer = new byte[1024];

        //构建数据报
        DatagramPacket p = new DatagramPacket(buffer, 0, buffer.length);
        //while (true){
            udpServer.receive(p);//receive是一个阻塞的方法

            System.out.println(new String(buffer, 0, p.getLength()));
        //}
        //udpServer.close();//关闭socket
    }
    @Test
    public void clientUdpTest() throws IOException {
        //创建一个DatagramSocket
        DatagramSocket udpClient = new DatagramSocket();
        String data = "hell udp";
        //数据包,尝试向本机的8080端口发送数据包
        DatagramPacket p = new DatagramPacket(data.getBytes(),0,data.length(),
                InetAddress.getByName("localhost"),8080);
        udpClient.send(p);

        udpClient.close();//关闭socket

        //使用一个简单的循环实现一个轰炸:


        //    DatagramSocket udpClient = new DatagramSocket();
        //    String data = "hell udp";
        //    //数据包,尝试向本机的8080端口发送数据包
        //while (true){
        //    DatagramPacket p = new DatagramPacket(data.getBytes(),0,data.length(),
        //            InetAddress.getByName("localhost"),8080);
        //    udpClient.send(p);
        //}

    }
}