Java从接触到放弃(二十一)--网络编程

Day Twenty-One

网络编程

  1. 网络编程中有两个主要的问题
    • 如何准确的定位到网络上的一台或者多台主机
    • 找到主机之后如何进行通信
  2. 网络编程中的要素
    • IP和端口号
    • 网络通信协议 udp,tcp
IP

如何能获取到IP地址,在Java中有这样的一个类inetAddress,这个类是在java.net.InetAddress这个包下的,这个包下面的都是和网络相关的。在JavaAPI中对这个类有详细的介绍以及如何去使用。这个类有两个子类,Inet4Address和Inet6Address。

在这里插入图片描述

这个类里面没有构造器,只有静态方法。

在这里插入图片描述

也就是说我们new一个address,它就会把当前这个地址的IP返回出来。

package com.liuHuan.net;

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

//测试IP
public class TestInetAddress {
    public static void main(String[] args) throws UnknownHostException {
        //查询本机地址
        System.out.println(InetAddress.getByName("127.0.0.1"));
        System.out.println(InetAddress.getByName("localhost"));
        System.out.println(InetAddress.getLocalHost());

        //查询网站IP地址
        InetAddress inetAddress = InetAddress.getByName("www.csdn.com");
        System.out.println(inetAddress);

        //常用方法
        System.out.println(inetAddress.getAddress());
        System.out.println(inetAddress.getCanonicalHostName());//规范的名字
        System.out.println(inetAddress.getHostName());//域名,或者自己电脑的名字
        System.out.println(inetAddress.getHostAddress());//ip
    }
}
端口Port

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

  • 不同的进程有不同的端口号!端口号用来区分软件。

  • 端口范围 0~65535

  • 端口号分为TCP,UDP端口号,每一个分别有65535个端口号,所以总共有65535*2个。

  • 如果说我们在TCP下使用了11这个端口号,我们在UDP下也可同时使用11这个端口号。但是单个协议下,端口号不能冲突。

  • 端口分类:

    • 公有端口 0~1023。尽量不要使用,一般会被内置的进程或者服务器使用
      • HTTP:80
      • HTTPS:443
      • FTP:21
      • Telent:23
    • 程序注册端口:1024~49151,分配用户或者程序
      • Tomcat:8080
      • MySQL:3306
      • Oracle:1521
    • 动态、私有端口:49152~65535
  • 常用的DOS命令:

    netstat -ano #查看所有的端口
    netstat -ano|findstr "5900" #查看指定的端口
    tasklist|findstr "8696" #查看指定端口的进程
    
package com.liuHuan.net;

import java.net.InetSocketAddress;

public class TestInetSocketAddress {
    public static void main(String[] args) {
        InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8080);
        InetSocketAddress socketAddress2 = new InetSocketAddress("localhost", 8080);
        System.out.println(socketAddress);
        System.out.println(socketAddress2);

        System.out.println(socketAddress.getAddress());
        System.out.println(socketAddress.getHostName());//地址
        System.out.println(socketAddress.getPort());//端口号
    }
}
通信协议

协议:就是一个约定。

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

对于网络通信的协议太多了,所以这里我们就要有一个分层的概念。这里对网络通信有OSI和TCP/IP的分层协议。但是最好的,我们现在用的是TCP/IP协议簇,其中有两个比较重要的协议,TCP:用户传输协议;UDP:用户数据报协议

比较出名的协议就是TCP、IP(网络互联协议)协议

TCP实现聊天
  • 客户端

    1. 连接服务器 Socket
    2. 发送消息
    package com.liuHuan.net;
    
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.InetAddress;
    import java.net.Socket;
    
    public class TcpClient {
        public static void main(String[] args) {
            InetAddress serverIP;
            Socket socket = null;
            OutputStream outPut = null;
            try {
                //1.知道服务器的地址,端口
                serverIP = InetAddress.getByName("127.0.0.1");
                int port = 9999;
                //2.创建一个socket连接
                socket = new Socket(serverIP, port);
                //3.发送消息 IO流
                outPut = socket.getOutputStream();
                outPut.write("你好!!".getBytes());
    
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if (null != outPut) {
                    try {
                        outPut.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (null != socket) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
  • 服务器

    1. 建立服务的端口 ServerSocket
    2. 等待用户的连接 accept
    3. 接受用户消息
    package com.liuHuan.net;
    
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class TcpServer {
        public static void main(String[] args) {
            ServerSocket serverSocket = null;
            Socket socket = null;
            InputStream inPut = null;
            ByteArrayOutputStream outputStream = null;
            try {
                //1.得有一个地址
                serverSocket = new ServerSocket(9999);
                //2.等待客户端连接过来
                socket = serverSocket.accept();
                //3.读取客户端发过来的信息
                inPut = socket.getInputStream();
    
    //            管道流实现
                outputStream = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len;
                while ((len = inPut.read(buffer)) != -1) {
                    outputStream.write(buffer,0,len);
                }
                System.out.println(outputStream.toString());
    
    //            用缓冲区实现
    //            byte[] buffer = new byte[1024];
    //            int len;
    //            while ((len = inPut.read(buffer)) != -1) {
    //                System.out.println(new String(buffer, 0, len));
    //            }
    
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (inPut != null) {
                    try {
                        inPut.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (serverSocket != null) {
                    try {
                        serverSocket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
实现文件上传

服务端:

package com.liuHuan.net;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServer02 {
    public static void main(String[] args) throws Exception {
        //1.创建服务
        ServerSocket serverSocket = new ServerSocket(9000);
        //2.监听客户端的连接
        Socket socket = serverSocket.accept();
        //3.获取输入流
        InputStream inputStream = socket.getInputStream();
        //4.文件输出
        FileOutputStream fileOutputStream = new FileOutputStream("10.jpg");
        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            fileOutputStream.write(buffer,0,len);
        }

        //通知客户端,我接收完了
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("收完了,88".getBytes());

        //关闭所有资源
        outputStream.close();
        fileOutputStream.close();
        inputStream.close();
        socket.close();
        serverSocket.close();
    }
}

客户端

package com.liuHuan.net;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class TcpClient02 {
    public static void main(String[] args) throws Exception {
        //1.创建一个Socket连接
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
        //2.创建一个输出流
        OutputStream clientOutPut = socket.getOutputStream();

        //3.读取文件
        FileInputStream picInPut = new FileInputStream(new File("1.jpg"));
        //4.写出文件
        byte[] buffer = new byte[1024];
        int len;
        while ((len = picInPut.read(buffer)) != -1) {
            clientOutPut.write(buffer,0,len);
        }

        //告诉服务端,我发完了。
        socket.shutdownOutput();

        //确定服务器接收完毕,才断开连接
        InputStream inputStream = socket.getInputStream();
        //管道流
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] buffer2 = new byte[1024];
        int len2;
        while ((len2 = inputStream.read(buffer2)) != -1) {
            outputStream.write(buffer2,0,len2);
        }
        System.out.println(outputStream.toString());

        //关闭所有资源
        outputStream.close();
        inputStream.close();
        picInPut.close();
        clientOutPut.close();
        socket.close();
    }
}
UDP消息发送

可以理解为发短信一样,双方不用建立连接,只需要知道对方的地址就可以。

客户端

package com.liuHuan.net;

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

//不需要连接服务器
public class UdpClient01 {
    public static void main(String[] args) throws Exception {
        //1.建立socket连接
        DatagramSocket socket = new DatagramSocket();
        //2.建个包
        String msg = "你好,服务器!";
        //发送给谁
        InetAddress inetAddress = InetAddress.getByName("localhost");
        int port = 9090;
        DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,inetAddress,port);
        //3.发送包
        socket.send(packet);
        //4.关闭
        socket.close();
    }
}

服务端

package com.liuHuan.net;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Arrays;

//要等待客户端的连接
public class UdpServer01 {
    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().getHostName());
        System.out.println(new String(packet.getData()));
        //关闭连接
        socket.close();
    }
}
UDP聊天实现

一个人发,另一个人只能听:


发送方:

package com.liuHuan.net;

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

public class UdpSender01 {
    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();
            DatagramPacket packet = new DatagramPacket(
                    data.getBytes()
                    , 0
                    , data.getBytes().length
                    , new InetSocketAddress("localhost", 6666)
            );

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

接收方:

package com.liuHuan.net;

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

public class UdpReceive01 {
    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 receiveDate = new String(data, 0, data.length);

            System.out.println(receiveDate);

            if (receiveDate.startsWith("bye")) {
                break;
            }
        }

        socket.close();
    }
}
UDP多线程在线咨询

双方既能发送又能收到:

路人甲要和宋兵乙进行实时性的交流。


发送方法:

package com.liuHuan.net;

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

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

    private int fromPort;
    private String toIP;
    private int toPort;

    public TalkSend(int fromPort, String toIP, int toPort) throws Exception {
        this.fromPort = fromPort;
        this.toIP = toIP;
        this.toPort = toPort;
        socket = new DatagramSocket(fromPort);
        reader = new BufferedReader(new InputStreamReader(System.in));
    }

    @Override
    public void run() {
        while (true) {
            try {
                String data = reader.readLine();
                DatagramPacket packet = new DatagramPacket(
                        data.getBytes()
                        , 0
                        , data.getBytes().length
                        , new InetSocketAddress(this.toIP, this.toPort)
                );
                socket.send(packet);
                if (data.equals("bye")) {
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        socket.close();
    }
}

接收方法:

package com.liuHuan.net;

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

public class TalkReceive implements  Runnable {
    DatagramSocket socket;

    private int port;
    private String fromName;

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

    @Override
    public void run() {
        try {
            while (true) {
                //准备接收包裹
                byte[] container = new byte[1024];
                DatagramPacket packet = new DatagramPacket(container,0,container.length);
                socket.receive(packet);

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

                System.out.println(fromName + ":" + receiveDate);

                if (receiveDate.startsWith("bye")) {
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        socket.close();
    }
}

路人甲

package com.liuHuan.net;

public class TalkPeople {
    public static void main(String[] args) throws Exception {
        new Thread(new TalkSend(6666,"localhost",3333)).start();
        new Thread(new TalkReceive(8888,"路人甲")).start();
    }
}

宋兵乙

package com.liuHuan.net;

public class TalkPeople2 {
    public static void main(String[] args) throws Exception {
        new Thread(new TalkSend(5555,"localhost",8888)).start();
        new Thread(new TalkReceive(3333,"宋兵乙")).start();
    }
}
URL下载网络资源
package com.liuHuan.net;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class UrlDown {
    public static void main(String[] args) throws IOException {

        //下载地址
        URL url = new URL("");

        //连接到这个资源
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        InputStream inputStream = urlConnection.getInputStream();
        FileOutputStream outputStream = new FileOutputStream("");
        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer,0,len);
        }

        outputStream.close();
        inputStream.close();
        urlConnection.disconnect();//断开连接
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

菜的跟狗一样

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

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

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

打赏作者

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

抵扣说明:

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

余额充值