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

本文概述了网络通信的基础,包括IP地址和端口号的概念、InetAddress类的应用、TCP三次握手和四次挥手,以及如何使用TCP和UDP进行数据传输。还介绍了如何通过URL实现HTTP连接。

概述

一.网络通信需要解决的俩个问题
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();
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值