JavaSE---网络编程

1、网络编程概述

在这里插入图片描述

1.1、网络基础

在这里插入图片描述

2、网络通信要素概述(通信双方地址+一定的规则)

在这里插入图片描述

2.1、如何实现网络中的主机相互通信:IP+端口号

2.1.1、IP地址的理解

在这里插入图片描述

2.1.2、InetAddress类的实例化

public class InetAddressTest {
    //如何实例化InetAddress:两个方法:getByName(String host)、getLoalHost()
    public static void main(String[] args) {
        try {
            InetAddress inet1 = InetAddress.getByName("192.168.0.154");
            System.out.println(inet1);
            System.out.println("==============================");
            InetAddress inet2 = InetAddress.getByName("www.baidu.com");
            System.out.println(inet2);
            System.out.println("==============================");
            //获取本机ip
            InetAddress localhost = InetAddress.getLocalHost();
            System.out.println(localhost);
            System.out.println("==============================");
            System.out.println(inet2.getHostName());
            System.out.println(inet2.getHostAddress());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述

2.1.3、端口号

在这里插入图片描述

2.2、网络通信协议

在这里插入图片描述

2.2.1、网络通信协议

在这里插入图片描述

2.2.2、TCP/IP协议簇

在这里插入图片描述

2.2.3、TCP和UDP

在这里插入图片描述
在这里插入图片描述

2.2.4、建立链接时的三次握手

在这里插入图片描述

2.2.5、释放连接时的四次挥手

在这里插入图片描述
在这里插入图片描述

3、TCP网络编程例题

先启动服务端,在启动客户端

3.1、客户端发送内容给服务端,服务端将内容打印在控制台上

public class TcpTest {
    //客户端
    @Test
    public void client(){
        Socket socket = null;
        OutputStream os = null;
        try {
            //1、创建Socket对象,指明服务器的ip和端口号
            InetAddress inet = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inet,8899);
            //2、获取一个输出流,用于输出数据
            os = socket.getOutputStream();
            //3、写书数据
            os.write("你好,我是客户端".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4、资源关闭
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void server(){
        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            //1、创建ServerSocket对象,指明自己的端口号
            ss = new ServerSocket(8899);
            //2、调用accept()表示接收来自于客户端的socket
            socket = ss.accept();
            //3获取输入路
            is = socket.getInputStream();
            //4、读取输入流中的数据
            baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[5];
            int len;
            while((len = is.read(buffer))!=-1){
                baos.write(buffer,0,len);
            }
            System.out.println(baos.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        //5、关闭资源
            if(baos!=null){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(baos!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(baos!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(baos!=null){
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

3.2、客户端发送文件给服务端,服务端将文件保存到本地

public class TcpTest02 {
    //客户端
    @Test
    public void client(){
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
            os = socket.getOutputStream();
            fis = new FileInputStream(new File("bird.png"));
            byte[] buffer = new byte[20];
            int len;
            while((len = fis.read(buffer))!=-1){
                os.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //服务端
    @Test
    public void server(){
        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            ss = new ServerSocket(9090);
            socket = ss.accept();
            is = socket.getInputStream();
            fos = new FileOutputStream(new File("bird1.png"));

            byte[] buffer  = new byte[20];
            int len;
            while ((len = is.read(buffer))!=-1){
                fos.write(buffer,0,len);
            }
            System.out.println("接收成功!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(ss!=null){
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

在这里插入图片描述

3.3、客户端发送文件给服务端,服务端将文件保存到本地。并返回发送成功给客户端。并关闭相应连接

public class TcpTest03 {
    //客户端
    @Test
    public void client(){
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        ByteArrayOutputStream baos = null;
        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
            os = socket.getOutputStream();
            fis = new FileInputStream(new File("bird.png"));
            byte[] buffer = new byte[20];
            int len;
            while((len = fis.read(buffer))!=-1){
                os.write(buffer,0,len);
            }
            //图片传完,关闭图片的传输
            socket.shutdownOutput();
            //5、接收来自于服务端的数据,打印倒控制台上
            InputStream is = socket.getInputStream();
            baos = new ByteArrayOutputStream();
            byte[] buffer1 = new byte[20];
            int len1;
            while((len1 = is.read(buffer1))!=-1){
                baos.write(buffer1,0,len1);
            }
            System.out.println(baos.toString());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(baos!=null){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //服务端
    @Test
    public void server(){
        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos = null;
        OutputStream os = null;
        try {
            ss = new ServerSocket(9090);
            socket = ss.accept();
            is = socket.getInputStream();
            fos = new FileOutputStream(new File("bird2.png"));

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

            os = socket.getOutputStream();
            os.write("你好,客户端,照片我已接收(---来自于服务端)".getBytes());


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(ss!=null){
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

在这里插入图片描述

4、UDP网络通信

在这里插入图片描述
UDP通信特点:发送端只管发送,不管接收端是否接收成功。如果发送端发送出去数据,但接收端没有接收到数据,那么发送端也不会报错。

public class UDPTest {
   //发送端
    @Test
    public void sender() throws IOException {
        DatagramSocket socket = new DatagramSocket();

        String str = "我是UDP方式发送的导弹";

        byte[] data = str.getBytes();
        InetAddress inet = InetAddress.getLocalHost();
        DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9090);
        
        socket.send(packet);

        socket.close();
    }

    //接收端
    @Test
    public void receiver() throws IOException {
        DatagramSocket socket = new DatagramSocket(9090);

        byte[] buffer = new byte[100];
        DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);

        socket.receive(packet);

        System.out.println(new String(packet.getData(),0,packet.getLength()));

        socket.close();
    }
}

5、URL类的理解与实例化

5.1、URL类

在这里插入图片描述

5.2、URL类构造器

在这里插入图片描述

5.3、URL类常用方法

在这里插入图片描述

/**
 * URL网络编程
 * 1、URL:统一资源定位符、,对应着互联网的而谋一资源地址
 * 2、格式:
 *      http://localhost:8080/example/beauty.jpg?username=tom
 *      协议    主机名   端口号   资源地址           参数列表
 */
public class URLTest {
    public static void main(String[] args) {
        try {
            URL url = new URL("http://localhost:8080/example/beauty.jpg?username=tom");

            System.out.println(url.getProtocol());//获取该URL的协议名
            System.out.println(url.getHost());//获取该URL的主机名
            System.out.println(url.getPort());//获取该URL的端口号
            System.out.println(url.getPath());//获取该URL的文件路径
            System.out.println(url.getFile());//获取该URL的文件名
            System.out.println(url.getQuery());//获取该URL的查询名
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述

5.4、URL编程实现从Tomcat服务器上拿资源

前提:启动本机tomcat服务器,在example文件夹下有bird.png文件

public class URLTest2 {
    public static void main(String[] args){
        HttpURLConnection urlConnection = null;
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            URL url = new URL("http://localhost:8080/example/bird.png");
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.connect();
            is = urlConnection.getInputStream();
            fos = new FileOutputStream("bird2.png");

            byte[] buffer = new byte[1024];
            int len;
            while((len =is.read())!=-1){
                fos.write(buffer,0,len);
            }
            System.out.println("下载完成");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(is!=null){
                //关闭资源
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(urlConnection!=null){
                //关闭资源
                urlConnection.disconnect();
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值