Java网络编程(一)

网络编程概述

计算机网络: 计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。
网络编程的概述: 网络编程最主要的工作就是在发送端把信息通过规定好的协议进行组装包,在接收端按照规定好的协议把包进行解析,从而提取出对应的信息,达到通信的目的。中间最主要的就是数据包的组装,数据包的过滤,数据包的捕获,数据包的分析,当然最后再做一些处理。

网络通信的要素

如何实现网络通信?
1.获得通信的地址
地址分为两个IP和端口号
2.规则:网络通信的协议TCP/UDP
在这里插入图片描述
IP: ipv4/ipv6

地址的格式为:IP地址=网络地址+主机地址 或 IP地址=主机地址+子网地址+主机地址。

IP地址根据网络ID的不同分为5种类型: A类地址、B类地址、C类地址、D类地址和E类地址。
1. A类IP地址

一个A类IP地址由1字节的网络地址和3字节主机地址组成,网络地址的最高位必须是“0”, 地址范围从1.0.0.0 到126.0.0.0。可用的A类网络有126个,每个网络能容纳1亿多个主机。

2. B类IP地址

一个B类IP地址由2个字节的网络地址和2个字节的主机地址组成,网络地址的最高位必须是“10”,地址范围从128.0.0.0到191.255.255.255。可用的B类网络有16382个,每个网络能容纳6万多个主机 。

3. C类IP地址

一个C类IP地址由3字节的网络地址和1字节的主机地址组成,网络地址的最高位必须是“110”。范围从192.0.0.0到223.255.255.255。C类网络可达209万余个,每个网络能容纳254个主机。

4. D类地址用于多点广播(Multicast)。

D类IP地址第一个字节以“lll0”开始,它是一个专门保留的地址。它并不指向特定的网络,目前这一类地址被用在多点广播(Multicast)中。多点广播地址用来一次寻址一组计算机,它标识共享同一协议的一组计算机。

5. E类IP地址

以“llll0”开始,为将来使用保留。

全零(“0.0.0.0”)地址对应于当前主机。全“1”的IP地址(“255.255.255.255”)是当前子网的广播地址。

用代码实现ip地址的查找

import java.net.InetAddress;
import java.net.UnknownHostException;

public class TestInetAddress {
    public static void main(String[] args) {
        try {
            //获得本机地址
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");

            System.out.println(inetAddress);
            //获得百度地址
            InetAddress inetAddress1 = InetAddress.getByName("www.baidu.com");
            System.out.println(inetAddress1);
            //获得规范的名字
            System.out.println(inetAddress1.getCanonicalHostName());
            //获得Ip
            System.out.println(inetAddress1.getHostAddress());
            //获得域名
            System.out.println(inetAddress1.getHostName());

            InetAddress localhost = inetAddress.getByName("localhost");
            System.out.println(localhost);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

    }
}

端口

协议端口的概述: 如果把IP地址比作一间房子 ,端口就是出入这间房子的门。真正的房子只有几个门,但是一个IP地址的端口可以有65536(即:2^16)个之多!端口是通过端口号来标记的,端口号只有整数,范围是从0 到65535(2^16-1)。

端口的分类: 根据提供服务类型的不同,端口分为两种,一种是TCP端口,一种是UDP端口。计算机之间相互通信的时候,分为两种方式:一种是发送信息以后,可以确认信息是否到达,也就是有应答的方式,这种方式大多采用TCP协议;一种是发送以后就不管了,不去确认信息是否到达,这种方式大多采用UDP协议。对应这两种协议的服务提供的端口,也就分为TCP端口和UDP端口。

端口类型:

  • 公有端口 0~1023
    • HTTP : 80
    • HTTPS : 443
    • FTP : 21
    • Telent : 23
  • 程序注册端口:1024~49151, 分配用户或者程序
    • Tomcat : 8080
    • MySQL : 3306
    • Oracle :1521
  • 动态、私有:49152~ 65535
import java.net.InetSocketAddress;

public class TestInetsockAddress {
    public static void main(String[] args) {

        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8080);
        InetSocketAddress inetSocketAddress1 = new InetSocketAddress("localhost", 8080);

        System.out.println(inetSocketAddress);
        System.out.println(inetSocketAddress1);

        System.out.println(inetSocketAddress.getAddress());
        //获得ip地址
        System.out.println(inetSocketAddress.getHostName());
        //获得端口号
        System.out.println(inetSocketAddress.getPort());
    }
}

通信协议:

TCP:

客户端:

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClientDemo {
    public static void main(String[] args) {
        Socket socket=null;
        OutputStream out=null;
        try {
            InetAddress address = InetAddress.getByName("127.0.0.1");

            int port=6666;
             socket = new Socket(address, port);
             out = socket.getOutputStream();
            out.write("hello,服务端".getBytes());



        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

服务端:

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;


public class TcpServerDemo {
    public static void main(String[] args) {
        ServerSocket serverSocket=null;
        Socket socket=null;
        InputStream in=null;
        ByteArrayOutputStream bos=null;

        try {
             serverSocket = new ServerSocket(6666);
            while (true){
                socket = serverSocket.accept();
            in = socket.getInputStream();
            bos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len ;
            while ((len = in.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
            System.out.println(bos.toString());
        }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(bos!=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

UDP:

客户端:

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;


public class UdpClientDemo {
    public static void main(String[] args) {
        try {
            //建立连接
            DatagramSocket socket = new DatagramSocket();
            //
            int port=6699;
            InetAddress localhost = InetAddress.getByName("localhost");
            String msg="你好啊,服务端";
            //要发送的数据,数据长度,发送给谁,端口号
            DatagramPacket packet = new DatagramPacket(msg.getBytes(),msg.getBytes().length,localhost,port);
            //发送文件
            socket.send(packet);


        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}

服务端:

import java.net.DatagramPacket;
import java.net.DatagramSocket;


public class UdpServerDemo {
    public static void main(String[] args) {
        try {
            //建立连接
            DatagramSocket socket = new DatagramSocket(6699);
            //
            byte[] buffer = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
            socket.receive(packet);
            byte[] data = packet.getData();
            System.out.println(new String(data,0,data.length));


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值