Java基础—网络编程

网络编程

概述

计算机网络:

计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。

网络编程目的:

  • 传播交流信息
  • 数据交换
  • 通信

网络通信的要素

通信双方地址

  • ip
  • 端口号
  • 192.168.16,124:5900

网络通信的协议

TCP/IP参考模型(应用层、传输层、网络层、物理层和数据链路层 )

IP

ip地址:InetAddress

  • 唯一的标识Internet上的计算机
  • 127.0.0.1 : 本机localhost
  • ip地址的分类
    1.ipv4 (4个字节组成,4个0-255)/ ipv6(16个字节(128位),写成8个无符号整数,每个整数用四个十六进制位表示)
    2.公网地址(万维网使用)和私有地址(局域网使用)
    192.168开头的就是私有地址,范围即为192.168.0.0-192.168.255.255
  • 域名:记忆IP问题
    IP(www.baidu.com、www.jd.com)
  • InetAddress类的一个对象代表一个具体的IP地址

端口

端口表示计算机上的一个程序的进程

  • 不同的进程有不同的端口号!用来区分软件!
  • 被规定0~65535
  • 单个协议下端口号不能冲突
  • 端口分类:
    1.公认端口:0-1023。被预先定义的服务通信占用(如:HTTP占用端口80,FTP占用端口21、Telnet占用端口23)
    2.注册端口:1024-49151。分配给用户进程或应用程序。(如:Tomcat占用端口8080,MySQL占用端口3306,Oracle占用端口1521等)
    3.动态/私有端口:49152~65535

通信协议

协议:约定,就好比我们现在说的普通话。

  • 网络通信协议:速率,传输的码率,代码结构,传输控制…
  • TCP/IP协议簇:实际上是一组协议
  • TCP:用户传输协议
  • UDP:用户数据报协议

TCP和UDP对比

TCP:点电话

  • 连接,稳定
  • 三次握手 四次握手
  • 客户端、服务端
  • 传输完成,释放连接,效率低

UDP:发短信

  • 不连接,不稳定
  • 客户端、服务端:没有明确的界限
  • 不管有没有准备好,都可以发给你

TCP

客户端
       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 (Exception e) {
            e.printStackTrace();
        }finally {
            if(os != null){
                try {
                    //4、资源的关闭
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

服务端
        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());
            System.out.println("收到了来自于:"+socket.getInetAddress().getHostAddress());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(baos!=null){
                try {
                    //5、关闭资源
                    baos.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();
                }
            }
        }

文件上传

客户端
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            //1、创建Socket对象,指明服务器的ip和端口号
            InetAddress inet = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inet, 9090);
            //2、获取一个输出流,用于输出数据
            os = socket.getOutputStream();
            //3、写出数据的操作
            fis = new FileInputStream(new File("01.jpg"));
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read())!=-1){
                os.write(buffer,0,len);
            }

            socket.shutdownOutput();

            //4、接受来自于服务器端的数据,并显示到控制台上
            is = socket.getInputStream();
            baos = new ByteArrayOutputStream();
            byte[] buffer1 = new byte[1024];
            int len1;
            while((len1 = is.read(buffer1))!=-1){
                baos.write(buffer1,0,len1);
            }
            System.out.println(baos.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(baos!=null){
                try {
                    //5、关闭资源
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            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();
                }
            }
        }

服务端

        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos = null;
        OutputStream os = null;
        try {
            //1、创建服务器端的ServerSocket,指明自己的端口号
            ss = new ServerSocket(9090);
            //2、调用accept()表示接受来自于客户端的socket
            socket = ss.accept();
            //3、获取输入流
            is = socket.getInputStream();
            //4、读取输入流中的数据
            fos = new FileOutputStream(new File("dog0001.jpg"));
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read())!=-1){
                fos.write(buffer,0,len);
            }
            //5、服务器端给客户端反馈
            os = socket.getOutputStream();
            os.write("你好,照片已经传输完毕,请您查收".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(os!=null){
                try {
                    //6、关闭资源
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            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();
                }
            }
        }

UDP

发短信:不用连接,需要知道对方地址

发送方
      DatagramSocket socket = null;
        try {
            //1、创建发送端
            socket = new DatagramSocket();
            //2、建立数据包
            String str = "你好,我是UDP方法的发送方";
            byte[] data = str.getBytes();
            InetAddress inet = InetAddress.getLocalHost();
            DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9091);
            //3、调用DatagramSocket的发送方法
            socket.send(packet);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //4、关闭socket
            if(socket!=null){
                socket.close();
            }
        }

接收方

       DatagramSocket socket = null;
        try {
            //1、创建接受端,指明端口
            socket = new DatagramSocket(9091);
            //2、建立数据包
            byte[] buffer = new byte[100];
            DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
            //3、调用DatagramSocket的接收方法
            socket.receive(packet);

            System.out.println(new String(packet.getData(),0,packet.getLength()));
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //4、关闭socket
            if(socket!=null){
                socket.close();
            }
        }

URL

统一资源定位符:定位资源的,定位互联网上的某一个资源
DNS 域名解析

  • 1、获取该URL的协议名
    public String getProtocol()
  • 2、获取该URL的主机名
    public String getHost()
  • 3、获取该URL的端口号
    public String getPort()
  • 4、获取该URL的文件路径
    public String getPath()
  • 5、获取该URL的文件名
    public String getFile()
  • 6、获取该URL的查询名
    public String getQuery()
      HttpURLConnection urlConnection = null;
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            URL url = new URL("http://localhost:8080/qinjiang/SecurityFile.txt);
            urlConnection = (HttpURLConnection)url.openConnection();

            urlConnection.connect();

            is = urlConnection.getInputStream();
            fos = new FileOutputStream("SecurityFile.txt");

            byte[] buffer = new byte[1024];
            int len;
            while((len = is.read())!=-1){
                fos.write(buffer,0,len);
            }
        } catch (Exception 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(urlConnection!=null){
                urlConnection.disconnect();
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值