Java学习5.15

网络编程

  1. 地址
 //查询本机地址
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
            System.out.println(inetAddress);
            InetAddress localname = InetAddress.getByName("localhost");
            System.out.println(localname);
            InetAddress localHost = InetAddress.getLocalHost();
            System.out.println(localHost);

            //常用方法
            //System.out.println(inetAddress.getAddress());
            System.out.println(inetAddress.getCanonicalHostName());//规范的名字
            System.out.println(inetAddress.getHostAddress());//ip
            System.out.println(inetAddress.getHostName());//自己电脑的名字
  1. 端口
InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8080);
  1. TCP(三次握手)
public class PtCelent01 {
    	//客户端
    public static void main(String[] args) throws Exception {
        //1.创建一个地址端口
        InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
        Socket socket = new Socket(inetAddress,9000);

        //2.创建一个输出流
        OutputStream os = socket.getOutputStream();

        //3.读取文件
        FileInputStream fis = new FileInputStream(new File("1.png"));
        //写出文件
        byte[] buffer = new byte[1024];
        int len;
        while((len=fis.read())!=-1){
            os.write(buffer,0,len);
        }
        //告诉服务器已经传输完了
        socket.shutdownOutput();
        //接受服务器的反馈
        InputStream is = socket.getInputStream();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buffer1 = new byte[1024];
        int len1;
        while ((len1=is.read(buffer1))!=-1){
            bos.write(buffer1,0,len1);
        }

        System.out.println(bos.toString());

        //5.关闭资源
        bos.close();
        is.close();
        fis.close();
        os.close();
        socket.close();
    }

}
public class PtSever01 {
    	//服务器端
    public static void main(String[] args) throws Exception {
        //1.创建服务器和机器连接的端口
        ServerSocket serverSocket = new ServerSocket(9000);
        //2.监听客户端的连接
        Socket socket = serverSocket.accept();

        //3.获取输入流
        InputStream is = socket.getInputStream();

        //4.把输入的文件输出
        FileOutputStream fos = new FileOutputStream(new File("1233221331.png"));

        byte[] buffer = new byte[1024];
        int len;
        while ((len=is.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }

        //告诉客户端,已经收到
        OutputStream os = socket.getOutputStream();
        os.write("我好了".getBytes());

        os.close();
        fos.close();
        is.close();
        socket.close();
    }
}

抛出异常只是一种偷懒做法,实际上每一个close都应该try/catch

4.UDP(发送不需要接收方同意)

//不需要连接服务器
public class UDPClient01 {
    public static void main(String[] args) throws Exception {
        //1.建立一个socket
        DatagramSocket datagramSocket = new DatagramSocket();

        //2.建立个包
        String msg = "你好服务器";

        //发送给谁
        InetAddress localhost = InetAddress.getByName("localhost");
        int potrt = 9090;

        DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, potrt);

        //3.发送个包
        datagramSocket.send(datagramPacket);

        //关闭流
        datagramSocket.close();
    }
}
public class UDPServerDemo01 {
    public static void main(String[] args) throws IOException {
        //开放端口
        DatagramSocket socket = new DatagramSocket(9090);
        //接受数据包
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);

        socket.receive(packet);

        System.out.println(packet.getAddress().getHostAddress());
        System.out.println(new String(packet.getData(),0,packet.getLength()));
        socket.close();
    }
}

学习内容来自:狂神说Java bilibili的一位宝藏up主

posted @ 2020-05-26 15:41  麻雀麻雀  阅读(126)  评论(0)    收藏  举报