java网络编程

IP 概念:用于唯一标识主机 查看IP方式:cmd->ipconfig 表达形式:点分十进制 xx.xx.xx.xx 每一个十进制的范围:0~255 ip地址的组成=网络地址+主机地址(3+1) ivv4 四个字节 ipv6 十六个字节

域名 概念:将ip地址映射成域名 HTTP协议 好处:方便记忆

端口号 概念:用于标识祭祀安吉上某个特定的网络程序

InetAddress

 public void InetAddress_() throws UnknownHostException {
        InetAddress localHost = InetAddress.getLocalHost();//LAPTOP-OLAJRR6K/192.168.71.127 设备名称/id地址

        //根据主机名或域名 返回ip
        InetAddress byName = InetAddress.getByName("LAPTOP-OLAJRR6K");//LAPTOP-OLAJRR6K/192.168.71.127 通过设备


        System.out.println(InetAddress.getByAddress(localHost.getAddress()));

        System.out.println(byName.getHostName());
        System.out.println(byName.getHostAddress());
        System.out.println(byName);
    }

Socket

public class SocketTCP01Server {
    public static void main(String[] args) throws IOException {
        //1.在本机的 9999 端口监听 等待连接  要求该端口不被占用 尽量不要选用1024之前作为端口号 存在端口占用现象
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("监听中 等待连接..");
        //2.等待被链接 如果未连接 处于阻塞状态 如果有客户端连接 则返回一个socket对象
        Socket socket = serverSocket.accept();

        System.out.println(socket.getClass());

        //3 通过socket.getInputStream()读取存入到socket当中的数据
        InputStream inputStream = socket.getInputStream();
        byte[] buf = new byte[1024];
        int writeLen;
        while((writeLen = inputStream.read(buf)) != -1){
            System.out.println(new String(buf,0,writeLen));
        }

        inputStream.close();
        socket.close();
        serverSocket.close();
    }
}

public class SocketTCP01Cilent {
    public static void main(String[] args) throws IOException {
        //1.连接服务器
        Socket socket = new Socket(InetAddress.getLocalHost(),9999);
        //2.生成Socket 使用流写入数据到socket对象中
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("hello world".getBytes());

        outputStream.close();
        socket.close();
    }
}

网络间的文件传输 服务端代码

public class SocketTCP03Server {
    public static void main(String[] args) throws IOException {
        //1.在本机的 9999 端口监听 等待连接  要求该端口不被占用 尽量不要选用1024之前作为端口号 存在端口占用现象
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("监听中 等待连接..");
        //2.等待被链接 如果未连接 处于阻塞状态 如果有客户端连接 则返回一个socket对象
        Socket socket = serverSocket.accept();
        System.out.println("连接成功");
        //3 获取socket对应的输入流
        BufferedInputStream bufferedInputStream = new BufferedInputStream(socket.getInputStream());
        //4.将输入流当中的数据以字节方式取出
        StreamUtils streamUtils = new StreamUtils();
        byte[] bytes = streamUtils.streamToByteArray(bufferedInputStream);
        //5.创建输出流 将字节输出到对应磁盘
        String outPath = "d:\\default.png";
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(outPath));
        bufferedOutputStream.write(bytes);
        //6.返回消息给客户端
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("传输成功".getBytes());
        socket.shutdownOutput();//标记传输结束 不用等待


        bufferedOutputStream.close();
        bufferedInputStream.close();
        socket.close();
        serverSocket.close();
    }
}

客户端

public class SocketTCP03Cilent {
    public static void main(String[] args) throws IOException {
        //1.连接服务器
        Socket socket = new Socket(InetAddress.getLocalHost(),9999);
        //2.创建获取图片的输入流
        String filePath = "C:\\Users\\sdas\\Pictures\\default.png";
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(filePath));
        //3.将图片转化为字节
        StreamUtils streamUtils = new StreamUtils();
        byte[] bytes = streamUtils.streamToByteArray(bufferedInputStream);
        //4.将字节放到socket当中
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(bytes);
        socket.shutdownOutput();//表明传输结束 不用等待
        //5.接收服务器端的回应
        InputStream inputStream = socket.getInputStream();
        byte[] buff = new byte[1024];
        int realLen;
        while((realLen = inputStream.read(buff)) != -1){
            System.out.println(new String(buff,0,realLen));
        }

        bufferedInputStream.close();
        outputStream.close();
        socket.close();
    }
}

将输入流中的内容转换为字节的工具类

public class StreamUtils {
    public static byte[] streamToByteArray(InputStream inputStream) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int readLen;
        while((readLen = inputStream.read(buf)) != -1){
            byteArrayOutputStream.write(buf,0,readLen);
        }
        byte[] array = byteArrayOutputStream.toByteArray();
        byteArrayOutputStream.close();
        return array;
    }
}

netstat netstat -an 可以查看当前主机网络情况 包括端口监听和网络连接情况 netstat -an|more 可以分页显示

当客户端连接上服务器端时 客户端会随机生成一个端口和服务器端


本文章使用limfx的vscode插件快速发布