Java之网络编程

什么是计算机网络

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


网络通讯的两个要素

通讯双方的网络地址

1.ip

2.端口号

通过ip+端口号就可以精准定位一台计算机

规则:网络通讯协议

TCP/IP参考模型

Java之网络编程_第1张图片

1.网络编程中有两个主要的问题

        一、如何精准定位到网络上的一台或多台主机

        二、找到主机之后如何进行通信

2.网络编程中的要素

        一、IP和端口号

        二、网络通信协议

3.万物皆对象,创建类表示Ip和端口号


IP地址

ip地址(InetAddress)

1.唯一定位一台网络上的计算机

2.127.0.0.1:本机localhost

3.IP地址的分类

        一、ipv4 / ipv6

                IPV4 127.0.071,4个字节组成

        二、公网-私网

InetAddress类,互联网协议(ip)对象

创建对象:InetAddress 引用变量  =  InetAddress.getByName("ip地址");

获取本机地址的三种方式:

1.InetAddress.getByName("127.0.0.1")

2.InetAddress.getByName("localhost")

3.InetAddress.getLocalHost()

查询网站IP地址

InetAddress.getByName("域名")

三种常用方法:

1.InetAddress变量.getCanonicalHostName()   获取对象的规范地址(IP)

2.InetAddress变量.getHostAddress()        获取变量的ip地址

3.InetAddress变量.getHostName()        获取变量域名

例子:

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

//测试ip
public class Demo {
    public static void main(String[] args) {
        try {
            //查询本机地址
            InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
            System.out.println(inetAddress1);
            InetAddress inetAddress3 = InetAddress.getByName("localhost");
            System.out.println(inetAddress3);
            InetAddress inetAddress4 = InetAddress.getLocalHost();
            System.out.println(inetAddress4);

            //查询网站Ip地址
            InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
            System.out.println(inetAddress2);

            //常用方法
            System.out.println(inetAddress2.getAddress());
            //规范的名字
            System.out.println(inetAddress2.getCanonicalHostName());
            //ip
            System.out.println(inetAddress2.getHostAddress());
            //域名
            System.out.println(inetAddress2.getHostName());

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

端口Port

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

1.不同的进程有不同的端口号,用来区分软件

2.端口号被规定范围0~65535

3.TCP,UDP:两个范围都是65535        单个协议下,端口号不能冲突,即TCP协议下端口号为80,UDP协议下端口号也能为80,但是相同协议下端口号不能相同

4.端口分类:

        一、共有端口0~1023

                常用协议访问网站端口号

                ·HTTP:80

                ·HTTPS:443

                ·FTP:21

                ·Telent:23

        ·程序注册端口:1024~49151,分配给用户或程序

                ·Tomcat:8080

                ·MySQL:3306

                ·Oracle:1521

        动态、私有端口:49152~65535不要使用

netstat -ano        #查看所有端口

netstat -ano|finder "端口值"   #查看指定端口

tasklist|findstr "端口值"  #查看指定端口的进程

以上命令在CMD控制台中使用

ctrl+shift+ESC  打开任务管理器

获取端口值的方法:引用变量.getPort()

InetSocketAddress定义协议对象

InetSocketAddress  引用变量  =  new  InetSocketAddress("hostname",port);


通信协议

协议:约定,如同普通话

网络通信协议:速率、传输码率、代码结构、传输控制.....

TCP/IP协议簇:实际上是一组协议

重要:

        1.TCP:用户传输协议(可以理解为打电话)

        一、连接、稳定

        二、分为客户端、服务端

        三、传输完成就释放连接,效率低

        四、三次握手(建立连接)四次挥手(断开连接)

至少需要三次交互,才能保证稳定连接

A:你在吗?

B:我在

A:连接!

挥手

A:我要走了

B:你要走了吗?

B:你真的要走了吗?

A: 我真的要走了

        2.UDP:用户数据报协议 (可以理解为发短信)

        一、不连接、不稳定

        二、客户端和服务端没有明确界限 

        三、不管是否对方准备好接收,都可以发给对方


TCP实现聊天

首先要做好客户端与服务端

客户端

1.连接服务器(Socket)

2.发送消息至服务器

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

//客户端
public class TcpClientDemo01 {
    public static void main(String[] args) {
        Socket socket = null;
        OutputStream os = null;
        try {
            //1.要知道服务器的地址
            InetAddress serverIP = InetAddress.getByName("127.0.0.1");
            int port = 9999;
            //2.创建一个socket连接
            socket = new Socket(serverIP,port);
            //3.发送消息 IO流
            os = socket.getOutputStream();

            os.write("hello!How are you?".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                os.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                socket.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

服务器

1.建立服务端口(ServerSocket)

2.等待用户连接(accept)

3.接收用户的消息

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

//服务端
public class TcpServerDemo01 {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        ByteArrayOutputStream baos = null;
        InputStream is = null;
        try {
            //1.得有一个地址
            serverSocket = new ServerSocket(9999);
            //2.等待客户端连接过来
            socket = serverSocket.accept();
            //3.读取客户端的消息
            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 (baos!=null){
                try {
                    baos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}

TCP实现文件上传

接收端:
 

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

//还是要等待客户端的连接
public class UdpService {
    public static void main(String[] args) throws Exception{
        //开放端
        DatagramSocket socket = new DatagramSocket(9090);
        //接收数据包
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);

        socket.receive(packet);  //阻塞接收

        System.out.println(packet.getAddress().getHostAddress());
        System.out.println(new String(packet.getData(),0,packet.getLength()));

        //关闭连接
        socket.close();
    }
}

发送端:

import java.net.*;

//UDP不需要连接服务器
public class UdpClient {
    public static void main(String[] args) throws Exception{
        //1.建立一个Socket
        DatagramSocket socket = new DatagramSocket();
        //2.建个包
        String msg = "hello,Service!";
        InetAddress localhost = InetAddress.getByName("localhost");
        int port =9090;
        //参数:数据,数据的起始位置,何处结束,要发送给谁,端口号
        DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);
        //3.发送包
        socket.send(packet);

        //4.关闭流
        socket.close();
    }
}

UDP实现消息发送

发送端:

import java.net.*;

//UDP不需要连接服务器
public class UdpClient {
    public static void main(String[] args) throws Exception{
        //1.建立一个Socket
        DatagramSocket socket = new DatagramSocket();
        //2.建个包
        String msg = "hello,Service!";
        InetAddress localhost = InetAddress.getByName("localhost");
        int port =9090;
        //参数:数据,数据的起始位置,何处结束,要发送给谁,端口号
        DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);
        //3.发送包
        socket.send(packet);

        //4.关闭流
        socket.close();
    }
}

接收端:

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

//还是要等待客户端的连接
public class UdpService {
    public static void main(String[] args) throws Exception{
        //开放端
        DatagramSocket socket = new DatagramSocket(9090);
        //接收数据包
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);

        socket.receive(packet);  //阻塞接收

        System.out.println(packet.getAddress().getHostAddress());
        System.out.println(new String(packet.getData(),0,packet.getLength()));

        //关闭连接
        socket.close();
    }
}

UDP实现聊天

DatagramSocket        使用的是UDP连接,不需要连接就可以发送数据给指定服务器

DatagramPacket        用于打包需要发送的数据,它有两个构造函数

1.发送数据构造函数

DatagramPacket(byte[]  buffer,int  length,InetAddress  address,int  port)

把长度为length的数据包buffer发送到address主机的port端口

2.接收数据构造函数

DatagramPacket(byte[]  buffer,int  length)

接收长度为length的数据储存到buffer中

getBytes()

1.无参,将字符串转换为字节数组,返回一个byte数组,其中每个字节都表示字符串中相应位置的字符

2.有参,传入一个字符编码方式charseName作为参数,将字符传转换为字节数组

BufferedReader类:是java io中的一个类,它是一个带缓冲区的字符输入流,用于从字符输入流中读取字符。主要作用是读取文本文件中的字符数据,可以读取文件中的每一行数据

成员方法:

1.readLine()        从缓存中读取一行文本,并返回String类型的结果

2.read()               从缓存中读取一个字符,并返回读取字符的ASCII码值,如果已经到达流的末尾,则返回-1

3.read(char[]  cbuf,int  off,int  len)        从缓存中读取字符,并将字符复制到指定字符数组cbuf中,偏移量为off,长度为len

4.close()              关闭字符流

构造方法:

1.BufferedReader(Reader  in)        创建一个缓冲字符输入流对象,并选择指定字符输入流对象in作为其实际数据源

2.BufferedReader(Reader  in,int  size)        创建一个缓冲字符输入流对象,并选择字符输入流对象in作为其实际数据源,同时指定缓冲区大小size

InputStreamReader:将一个字节的输入流转换为字符的输入流

OutputStreamWriter :将一个字节的输出流转换为字符的输出流

String类用法

String(char[]  value,int  index,int  count)

把字符数组中的一部分数据封装成字符串对象,从下表index~count 

发送端:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

public class UdpSender {
    public static void main(String[] args) throws Exception{
        DatagramSocket socket = new DatagramSocket(8888);

        //准备数据:控制台读取System.in
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        while (true){
            String data = reader.readLine();
            byte[] datas = data.getBytes();
            DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress("localhost",6666));

            socket.send(packet);
            if (data.equals("bye")){
                break;
            }
        }

        socket.close();
    }
}

接收端:

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

public class UdpReceiver {
    public static void main(String[] args) throws Exception{
        DatagramSocket socket = new DatagramSocket(6666);

       while (true){
           //准备接收包裹
           byte[] container = new byte[1024];
           DatagramPacket packet = new DatagramPacket(container,0,container.length);
           socket.receive(packet); //阻塞式接收包裹

           //断开连接
           byte[] data = packet.getData();
           String receiveData = new String(data,0,packet.getLength());

           System.out.println(receiveData);

           if (receiveData.equals("bye")){
               break;
           }

       }
       socket.close();
    }
}

UPD多线程在线咨询

接收端1:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class TalkReceive implements Runnable{
    DatagramSocket socket = null;
    private int port;
    private String msgFrom;

    public TalkReceive(int port,String msgFrom) {
        this.port = port;
        this.msgFrom = msgFrom;
        try {
            socket = new DatagramSocket(port);
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run(){

        while (true){
            try {
                //准备接收包裹
                byte[] container = new byte[1024];
                DatagramPacket packet = new DatagramPacket(container,0,container.length);
                socket.receive(packet); //阻塞式接收包裹

                //断开连接
                byte[] data = packet.getData();
                String receiveData = new String(data,0,packet.getLength());

                System.out.println(msgFrom+":"+receiveData);

                if (receiveData.equals("bye")){
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        socket.close();
    }
}

接收端2:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

public class TalkSend implements Runnable{
    DatagramSocket socket = null;
    BufferedReader reader = null;

    int fromPort;
    private String toIP;
    private int toPort;

    public TalkSend(int fromPort, String toIP, int toPort) {
        this.fromPort = fromPort;
        this.toIP = toIP;
        this.toPort = toPort;

        try {
            socket = new DatagramSocket(fromPort);
            reader = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (true){
            try {
                String data = reader.readLine();
                byte[] datas = data.getBytes();
                DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress(this.toIP,this.toPort));

                socket.send(packet);
                if (data.equals("bye")){
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        socket.close();
    }
}

对象1:

public class TalkStudent {
    public static void main(String[] args) {
        //开启两个线程
        new Thread(new TalkSend(7777,"localhost",9999)).start();
        new Thread(new TalkReceive(8888,"老师")).start();
    }
}

对象2:

public class TalkTeacher {
    public static void main(String[] args) {
        //开启两个线程
        new Thread(new TalkSend(5555,"localhost",8888)).start();
        new Thread(new TalkReceive(9999,"学生")).start();
    }
}

URL下载网络资源

统一资源定位符:定位资源的,定位互联网上的某一个资源

DNS域名解析:www.baidu.com转化为IP   xxx.x..x..x

组成:    协议://ip地址:端口/项目名/资源

Java之网络编程_第2张图片

连接URL:

HttpURLConnection  引用变量 = (HttpURLConnection) url.openConnection() 

你可能感兴趣的:(网络)