Java基础__05.网络编程

通信协议

  即约定网络通信时的一些内容。

TCP和UDP对比

TCP:类比打电话

  • 连接稳定
  • 三次握手、四次挥手
  • 客户端、服务端
  • 传输完成、释放连接,效率低

UDP:类比发短信

  • 不连接、不稳定;
  • 客户端、服务端,没有明确的界限;
  • 不管有没有准备好,都可以进行

TCP

  客户端

    1、连接服务器,通过Socket;

    2、发送消息

    代码示例:

public static void main(String[] args) {
        // 1、要知道服务器的IP地址,端口号
        InetAddress serverIp = null;
        Socket socket = null;
        OutputStream os = null;
        try {
            serverIp = InetAddress.getByName("127.0.0.1");
            int port = 9988;
            // 2、创建一个Socket连接
            socket = new Socket(serverIp,port);
            // 3、发送消息 IO流知识
            os = socket.getOutputStream();
            os.write("welcome to China".getBytes());  // 转为字节
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

 

  服务端

    1、建立服务的端口:通过ServerSocket;

    2、等待用户的连接,accept方法获取客户端的Socket;

    3、接收用户的消息。

    代码示例:

public static void main(String[] args) {
        ByteArrayOutputStream baos = null;
        InputStream inputStream = null;
        Socket accept = null;
        ServerSocket serverSocket = null;
        try {
            // 1、我需要一个地址
            serverSocket = new ServerSocket(9988);
            // 2、等待客户端连接
            accept = serverSocket.accept();
            // 3、读取客户端的消息
            inputStream = accept.getInputStream();
            // 管道流的形式处理
            baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while((len = inputStream.read(buffer)) != -1){
                baos.write(buffer,0,len);
            }
            System.out.println(baos.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                accept.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

   除了简单的信息传递,也可以进行文件的传输,直接上代码,示例如下:

  客户端

  public static void main(String[] args) throws Exception{
        // 1、创建一个Socket连接
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9988);
        // 2、创建一个输出流
        OutputStream os = socket.getOutputStream();
        // 3、读取文件
        FileInputStream fis = new FileInputStream(new File("F:/WeChatFiles/WeChat Files/All Users/abc.jpg"));
        //4、写出文件
        byte[] buffer = new byte[1024];
        int len;
        while((len = fis.read(buffer)) != -1){
            os.write(buffer,0,len);
        }
        // 通知服务器,我已经发送完毕了
        socket.shutdownOutput();
        // 确定服务器接收完毕了,才能断开
        InputStream ins = socket.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] b1 = new byte[1024];
        int len2;
        while((len2 = ins.read(b1)) != -1){
            baos.write(b1,0,len2);
        }
        System.out.println(baos.toString());
        // 5、关闭资源
        baos.close();
        fis.close();
        os.close();
        socket.close();
    }

  服务端

  public static void main(String[] args) throws Exception{
        // 1、创建服务
        ServerSocket serverSocket = new ServerSocket(9988);
        // 2、监听客户端的连接
        Socket accept = serverSocket.accept();
        // 3、获取输入流
        InputStream is = accept.getInputStream();
        // 4、文件输出
        FileOutputStream fos = new FileOutputStream(new File("F:/abc.jpg"));
        byte[] buffer = new byte[1024];
        int len;
        while((len = is.read(buffer)) != -1){
            fos.write(buffer,0,len);
        }
        // 告诉客户端,我接收完毕了
        OutputStream os = accept.getOutputStream();
        os.write("我接收完毕了,你可以关了".getBytes());

        // 5、关闭资源
        os.close();
        fos.close();
        is.close();
        accept.close();
        serverSocket.close();
    }

  这样即可实现文件的传输。

UDP

  ...

 

URL

  即:统一资源定位符,用以定位某一资源的位置。

  格式:协议://ip:端口/项目/资源

 

posted on 2021-01-11 23:48  Miracle_Jerry  阅读(63)  评论(0)    收藏  举报