java-网络编程

如何实现网络通信:网络编程三要素

  1. IP
  2. 端口号
  3. 网络通信协议
  • OSI参考模型
  • TCP/IP

网络分层

  • OSI网络通信协议模型,是一个参考模型,而TCP/IP协议是事实上的标准
  • TCP/IP是一个协议族,按照层次划分为四层:应用层,传输层,互连网络层,接口层(物理+数据链路层)
TCP/IP参考模型TCP/IP协议栈
应用层HTTP、FTP、DNS、Telnet
传输层TCP、UDP
网络层IP、ICMP、ARP…
物理+数据链路层Ethernet、ATM、Frame Relay

IP地址:InetAddress

  • IPV4:4个字节组成,4个0-255;
  • IPV6:128位(16个字节),写成8个无符号整数,每个整数用四个十六进制位表示,
    数之间用冒号(:)分开,如:3ffe:3201:1401:1280:c8ff:fe4d:db39:1984
  • 本地回环地址(hostAddress):127.0.0.1 主机名(hostName):localhost

InetAddress类

  • 此类表示Internet协议(IP)地址,用于封装计算机的IP地址和DNS(没有端口信息)
  • static InetAddress getByName(String host):确定主机名称的IP地址
  • static InetAddress getLocalHost(String host):获取本机的IP
  • String getHostName():获取此IP地址的主机名
  • String getHostAddress():返回文本显示中的IP地址字符串
public class InetAddressTest {
    public static void main(String[] args) {
        try {
            //确定主机名称的IP地址
            InetAddress inetAddress = InetAddress.getByName("192.168.10.123");
            InetAddress inetAddress1 = InetAddress.getByName("baidu.com");
            System.out.println(inetAddress1);
            // 获取此IP地址的主机名
            System.out.println(inetAddress1.getHostName());
            //返回文本显示中的IP地址字符串
            System.out.println(inetAddress1.getHostAddress());
            //获取本机的IP
            InetAddress inetAddress2 = InetAddress.getLocalHost();
            System.out.println(inetAddress2);
        }  catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
    }
}

端口号

  • 标识正在计算机上运行的进程(程序)
  • 不同的进程有不同的端口号
  • 一个 16 位的整数 0~65535
  • 端口号与IP地址的组合得出一个网络套接字:Socket

协议

TCP协议

  • 建立TCP连接
  • 可靠的:采用三次握手的方式,点对点通信
  • 两个应用进程:客户端、服务端
  • 在连接中可进行大数据量的传输
  • 传输完毕,需要释放连接,效率低
TCP协议重要字段
  • 序号(sequence number):seq序号,占32位,用来标识从TCP源端向目的端发送的字节流
  • 确认号(acknowledgement number):ack序号,占32位,只有ACK标志位为1时,确认序号字段才有效,ack=seq+1。
  • 标志位(Flags):

URG:紧急指针(urgent pointer)有效。
ACK:确认序号标志;1:表示确认号有效; 0:表示报文中不含确认信息,忽略确认号字段.
PSH:接收方应该尽快将这个报文交给应用层。
RST:重置连接。
SYN:同步序号,用于发起一个新连接。
FIN:finish标志,用于释放连接,为1时表示关闭本方数据流。

  • 在连接请求中,SYN=1和ACK=0表示该数据段没有使用捎带的确认域,而连接应答捎带一个确认,即SYN=1和ACK=1。
三次握手
  1. 客户端给服务端发送一个SYN报文,并指明客户端的初始化序列号:(SYN=1,seq=x);
  • 客户端状态: SYN_SENT
  1. 服务端收到客户端报文,以自己的SYN报文作为应答,并指定服务端初始化序列号(SYN=1,seq=y);同时用x+1作为确认号ack发送给客户端,确认收到了客户端的SYN(ACK=1,ack=x+1);
  • 服务端状态:SYN_RCVD
  1. 客户端收到SYN报文后,指定客户端序列号(seq=x+1)(初始为seq=x,第二个报文+1),会用y+1作为值作为确认号ack发给服务器,确认收到了服务端的SYN(ACK=1,ack=y+1),服务器与客户端建立连接。
  • 客户端状态:ESTABLISHED
  • 服务端状态:ESTABLISHED

请添加图片描述

四次挥手
  1. 客户端发送一个FIN报文,指定序列号(FIN=1,seq=u);并主动关闭TCP连接
    客户端状态:FIN_WAIT1
  2. 服务端收到客户端FIN报文后,指定序列号v(seq=v),并发送一个确认报文段(ACK=1,seq=u+1),确认收到客户端的关闭连接请求;
    服务端状态:CLOSE_WAIT
  3. 服务端给客服发送一个FIN报文,指定序列号w(FIN=1,seq=w),并确认收到了客户端的关闭连接请求(ACK=1,seq=u+1)
    服务端状态:LAST_ACK
  4. 客户端收到服务端的FIN报文,指定序列号u+1(seq=u+1),并确认收到了服务端的报文段(ACK=1,ack=w+1)

请添加图片描述

UDP协议

  • 无连接通信协议
  • 不可靠的:发送方直接发送,接收方也不确认
  • 通信结束时,不需要释放资源,消耗资源少,通信效率高,用于音频、视频和普通数据的传输

Socket

  1. 网络上具有唯一标识的IP地址和端口号组合在一起才能构成唯一能识别的标
    识符套接字
  2. Socket的分类:
  • 流套接字(stream socket):使用TCP提供可依赖的字节流服务
  • 数据报套接字(datagram socket):使用UDP提供“尽力而为”的数据报服务

URL

  • 表示统一资源定位符,指向万维网上的“资源”的指针。用于区分、定位资源
  • 一个标准的URL必须包括:protocol(方案或协议)、host(主机)、port(端口)、path(路径)、parameter( 查询参数)、anchor(锚点)

TCP网络编程

  • 客户端发送信息给服务端,服务端将数据显示在控制台上
public class TCPClient {
    public static void main(String[] args) {
        Socket socket = null;
        OutputStream os = null;
        try {
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inetAddress, 8900);
            os = socket.getOutputStream();
            os.write(1234);
            os.write("您好,我是梅尝酥!".getBytes());

        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}

public class TCPServer {
    public static void main(String[] args) {
        ServerSocket ss = null;
        Socket socket=null;
        InputStream is = null;
        ByteArrayOutputStream baos=null;
        try {
           ss= new ServerSocket(8900);
            socket = ss.accept();
            is = socket.getInputStream();
            baos = new ByteArrayOutputStream();
            byte[] buffer=new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1){
                baos.write(buffer,0,len);
            }
            System.out.println(baos.toString());

        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if(ss != null){
                try {
                    ss.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(ss != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(ss != null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(ss != null){
                try {
                    baos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}

  • 从客户端发送文件给服务端,服务端保存到本地。并返回内容给客户端。
public class TCPClient1 {
    public static void main(String[] args) {

        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inetAddress, 9091);
            os = socket.getOutputStream();
            fis= new FileInputStream(new File("day03\\hadeng.png"));
            byte[] buffer=new byte[1024];
            int len;
            while ((len = fis.read(buffer)) != -1){
                os.write(buffer,0,len);
            }
            //关闭数据的输出
            socket.shutdownOutput();

            is = socket.getInputStream();
            baos = new ByteArrayOutputStream();
            int len1;
            byte[] buffer1=new byte[1024];
            while ((len1 = is.read(buffer1)) != -1){
                baos.write(buffer1,0,len1);
            }
            System.out.println(baos);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            
        }
    }
}

public class TCPServer1 {
    public static void main(String[] args) {
        ServerSocket ss = null;
        Socket socket = null;
        FileOutputStream fos=null;
        InputStream is=null;
        OutputStream os = null;
        try {
            ss = new ServerSocket(9091);
            socket = ss.accept();
            is = socket.getInputStream();
            fos=new FileOutputStream(new File("day03\\lily.png"));
            byte[] buff=new byte[20];
            int len;
            while ((len = is.read(buff)) != -1){
                fos.write(buff,0,len);
            }

            os = socket.getOutputStream();
            os.write("已收到,谢谢".getBytes());

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

    }
}

UDP网络编程

public class UDPSender {
    public static void main(String[] args) {
        DatagramSocket socket = null;
        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            socket = new DatagramSocket();
            String str="这是一个图片";
            byte[] data=str.getBytes();
            DatagramPacket packet = new DatagramPacket(data, 0, data.length, inetAddress, 9100);
            socket.send(packet);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if(socket != null){
                socket.close();
            }
        }
    }
}


public class UDPReceiver {
    public static void main(String[] args) {
        DatagramSocket socket = null;
        try {
            socket = new DatagramSocket(9100);
            byte[] buffer=new byte[1024];
            DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
            socket.receive(packet);
            byte[] data = packet.getData();
            String dataStr = new String(data, 0, packet.getLength());
            System.out.println(dataStr);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

URL网络编程

URL常用的方法

  • getProtocol():URL的协议名
  • getHost():URL的主机名
  • getPort():URL的端口号
  • getFile():URL的文件路径
  • getQuery():URL的查询名
public class URLTest1 {
    public static void main(String[] args) {
        try {
            URL url = new URL("http://localhost:8080/refine/hadeng.png");
            System.out.println("URL的协议名:\t"+url.getProtocol());
            System.out.println("URL的主机名:\t"+url.getHost());
            System.out.println("URL的端口号:\t"+url.getPort());
            System.out.println("URL的文件路径:\t"+url.getPath());
            System.out.println("URL的文件名:\t"+url.getFile());
            System.out.println("URL的查询名:\t"+url.getQuery());
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }
}
/*
URL的协议名:	http
URL的主机名:	localhost
URL的端口号:	8080
URL的文件路径:	/refine/hadeng.png
URL的文件名:	/refine/hadeng.png
URL的查询名:	null
*/
  • 从服务器下载图片,并保存到本地。
public class URLTest {
    public static void main(String[] args) {
        HttpURLConnection urlConnection = null;
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            URL url = new URL("http://localhost:8080/refine/hadeng.png");
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.connect();
            is = urlConnection.getInputStream();
            fos = new FileOutputStream(new File("day03\\jim.png"));
            byte[] buff = new byte[1024];
            int len;
            while ((len = is.read(buff)) != -1){
                fos.write(buff,0,len);
            }
            System.out.println("Download successfully!");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(urlConnection != null){
                urlConnection.disconnect();
            }
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梅尝酥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值