栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java中有关网络编程的相关操作简述

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java中有关网络编程的相关操作简述

网络编程

概述InetAddressTCPUDPURL

概述

一.网络通信需要解决的俩个问题
1.如何定位网络上的一台主机或是多台主机
IP和端口号
2.找到主机后如何进行高效的传输
TCP/IP参考模型(应用层,传输层,网络层,物理+数据链路层)

二.网络通信俩个要素
1.IP的理解
(1)IP唯一表示因特网内的计算机
(2)在java中使用InetAddress类代表ip
(3)IP分类:IPv4和IPv6;网维网和局域网
(4)本地回路地址:127.0.0.1 对应localhost
(5)实例化InetAddress俩个方法;getByname(String)和getlocalHost()
常用方法:getHostName,getHostAress
端口号的理解
(1)正在计算机上运行的进程
要求:不同进程有不同的端口号
范围:0~65536
(2)端口号和ip地址组成一个网络套接字:Socket
2.网络协议


3.三次握手和四次挥手

InetAddress
	    InetAddress inet2 = InetAddress.getLocalHost();//实例化
        System.out.println(inet2);
        System.out.println(inet2.getHostAddress());     //获取地址
        System.out.println(inet2.getHostName());        // 获取域名
        InetAddress inet1 = InetAddress.getByName(inet2.getHostAddress());     //实例化
        System.out.println(inet1);
TCP

用户端

InetAddress inet = InetAddress.getLocalHost();
        InetAddress inet1 = InetAddress.getByName(inet.getHostAddress());
        Socket s = new Socket(inet1,8848);
        OutputStream op = s.getOutputStream();
        op.write("你好".getBytes());
        op.close();
        s.close();

服务器端

ServerSocket ss =new ServerSocket(8848);
        Socket s = ss.accept();
        InputStream is = s.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        char[] a = new char[5];
        int len;
        while((len=isr.read(a))!=-1)
        {
            String s1 = new String(a,0,len);
            System.out.println(s1);
        }
        isr.close();
        ss.close();
        s.close();

例题:服务器端回复
用户端

Socket s = new Socket(InetAddress.getByName("10.8.26.1"),8848);
        OutputStream os = s.getOutputStream();
        FileInputStream fis = new FileInputStream(new File("D:\Java\src\WLBC\市场痛点2.pptx"));
        byte[] b = new byte[20];
        int len;
        while((len = fis.read(b))!=-1)
        {
            os.write(b,0,len);
        }
        s.shutdownOutput();     //声明文件已经传输完成
//--------------------------------------接收来自服务器的信息--------------------------------------
        InputStream is = s.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        char[] b1 = new char[20];
        int len1;
        while((len1 = isr.read(b1))!=-1)
        {
            String b2 = new String(b1,0,len1);
            System.out.println(b2);
        }

        os.close();
        fis.close();
        s.close();

服务器端

ServerSocket ss = new ServerSocket(8848);
        Socket s = ss.accept();
        InputStream is = s.getInputStream();
        FileOutputStream fos = new FileOutputStream(new File("D:\Java\src\WLBC\操作.pptx"));
        byte[] b = new byte[20];
        int len;
        while((len = is.read(b))!=-1)
        {
            fos.write(b,0,len);
        }
        //----------------------服务器返回给客户端信息-----------------------------
        OutputStream os = s.getOutputStream();
        os.write("已接收到文件".getBytes());

        fos.close();
        s.close();
        ss.close();
        is.close();
UDP

发送端:

DatagramSocket ds = new DatagramSocket();
        String s = "我是弹幕";
        byte[] b = s.getBytes();
        InetAddress inetAddress = InetAddress.getLocalHost();
        DatagramPacket dp = new DatagramPacket(b,0,b.length,inetAddress,8848);
        ds.send(dp);
        ds.close();

接收端:

DatagramSocket ds = new DatagramSocket(8848);
        byte[] b = new byte[100];
        DatagramPacket datagramPacket = new DatagramPacket(b,0,b.length);
        ds.receive(datagramPacket);
        System.out.println(new String(datagramPacket.getData(),0,datagramPacket.getLength()));
        ds.close();
URL
    URL url = new URL("");          //  创建种子链接
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();     //  创建种子链接
        httpURLConnection.connect();
        InputStream is = httpURLConnection.getInputStream();
        FileOutputStream fos = new FileOutputStream(new File(""));
        byte[] b = new byte[20];
        int  len;
        while((len = is.read(b))!=-1)
        {
            fos.write(b,0,  len);
        }
        is.close();
        fos.close();
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/750643.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号