IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> Java筑基33-网络编程01-TCP网络编程&Socket -> 正文阅读

[网络协议]Java筑基33-网络编程01-TCP网络编程&Socket

目录

一、网络相关概念

二、IP地址

三、域名和端口

四、网络协议

五、 tcp和udp

六、Java_InetAddress

七、Socket

八、tcp字节流编程

1.应用案例01(单向通信)

2.应用案例02(字节流双向通信)

3.应用案例03(字符流双向通信)

4.应用案例04(网络上传文件)


一、网络相关概念

二、IP地址

0c0830819f18985208a3a4a832f287b1.png

三、域名和端口

bc34c3d4531e8dd65523422a9bf4d588.png

四、网络协议

6714720e28ba49b485fa6b457b8b9a6a.png

2ca8fd9e9849b2c80abcf02972588061.png

五、 tcp和udp

e596b5eb266b5303b9070aa1a1c784a1.png

六、Java_InetAddress

package com.feiyang.basic16_net;

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

/**
 * @author:飞扬
 * @公众hao:程序员飞扬
 * @description:InetAddress类API
 */
public class InetAddress_ {
    public static void main(String[] args) throws UnknownHostException {

        //获取本机的InetAddress对象
        InetAddress localHost = InetAddress.getLocalHost();
        System.out.println("本机地址=" + localHost);//LAPTOP-VLF7OM29/192.168.191.1

        //根据主机名获取InetAddress对象
        InetAddress host1 = InetAddress.getByName("LAPTOP-VLF7OM29");
        System.out.println("host1=" + host1);//LAPTOP-VLF7OM29/192.168.0.102

        //根据域名返回InetAddress
        InetAddress host2 = InetAddress.getByName("www.baidu.com");
        System.out.println("host2=" + host2);//www.baidu.com/180.101.49.11

        //根据InetAddress对象获取主机地址(即IP)
        String hostAddress = host2.getHostAddress();
        System.out.println("host2对应IP=" + hostAddress);

        String hostName = host2.getHostName();
        System.out.println("host2对应主机名/域名=" + hostName);
        
    }
}

打印输出:

本机地址=LAPTOP-VLF7OM29/192.168.0.102

host1=LAPTOP-VLF7OM29/192.168.0.102

host2=www.baidu.com/180.101.49.12

host2对应IP=180.101.49.12

host2对应主机名/域名=www.baidu.com

七、Socket

383ba9429e2d91f8648b8deabdb9f619.png

八、tcp字节流编程

1.应用案例01(单向通信)

思路分析:

package com.feiyang.basic16_net;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author:飞扬
 * @公众hao:程序员飞扬
 * @description: socke通信,服务端
 */
public class SocketTCP01Server {
    public static void main(String[] args) throws IOException {
        //1.在本机9999端口监听
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("服务端在9999端口监听...");

        //2.当没有连接时,程序会阻塞,等待连接,如果有客户端连接,则会返回socket对象,程序继续
        Socket socket = serverSocket.accept();
        System.out.println("服务端socket=" + socket.getClass());

        //3.读取数据
        InputStream inputStream = socket.getInputStream();

        /*byte[] buf = new byte[1024];
        int readLen = 0;

        while((readLen=inputStream.read()) != -1){
            System.out.println(new String(buf,0,readLen));
        }*/

        //方式2:如果读取乱码,使用转换流读取
        InputStreamReader isr = new InputStreamReader(inputStream, "utf-8");
        BufferedReader bufferedReader = new BufferedReader(isr);
        String s = bufferedReader.readLine();
        System.out.println("服务端接收=" + s);

        //4.关闭流
        inputStream.close();
        socket.close();
        serverSocket.close();

    }
}
package com.feiyang.basic16_net;

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

/**
 * @author:飞扬
 * @公众hao:程序员飞扬
 * @description: socke通信,客户端
 */
public class SocketTCP01Client {
    public static void main(String[] args) throws IOException {
        //1.连接服务端(IP,端口)
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        System.out.println("客户端socket=" + socket.getClass());

        //2.连接上之后会返回一个socket对象,得到和socket关联的输出流对象
        OutputStream outputStream = socket.getOutputStream();

        //3.写数据
        outputStream.write("hello,server".getBytes());
        System.out.println("客户端写数据。。。");

        //4.关闭io流,关闭socket连接
        outputStream.close();
        socket.close();
        System.out.println("客户端退出。");
    }
}

运行结果:

2.应用案例02(字节流双向通信)

2b84c64c5ca1159c411f5458454d5dda.png

package com.feiyang.basic16_net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author:飞扬
 * @公众hao:程序员飞扬
 * @description: socke双向通信,服务端
 */
public class SocketTCP02Server {
    public static void main(String[] args) throws IOException {
        //1.在本机9999端口监听
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("服务端在9999端口监听...");

        //2.当没有连接时,程序会阻塞,等待连接,如果有客户端连接,则会返回socket对象,程序继续
        Socket socket = serverSocket.accept();
        System.out.println("服务端socket=" + socket.getClass());

        //3.读取数据
        InputStream inputStream = socket.getInputStream();

        /*byte[] buf = new byte[1024];
        int readLen = 0;

        while((readLen=inputStream.read()) != -1){
            System.out.println(new String(buf,0,readLen));
        }*/

        //方式2:如果读取乱码,使用转换流读取
        InputStreamReader isr = new InputStreamReader(inputStream, "utf-8");
        BufferedReader bufferedReader = new BufferedReader(isr);
        String s = bufferedReader.readLine();
        System.out.println("服务端接收=" + s);

        //服务端发送消息
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("hello,client".getBytes());
        socket.shutdownOutput(); //输出结束标记


        //4.关闭流
        inputStream.close();
        socket.close();
        serverSocket.close();
        System.out.println("服务端退出。");

    }
}

package com.feiyang.basic16_net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @author:飞扬
 * @公众hao:程序员飞扬
 * @description: socke双向通信,客户端
 */
public class SocketTCP02Client {
    public static void main(String[] args) throws IOException {
        //1.连接服务端(IP,端口)
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        System.out.println("客户端socket=" + socket.getClass());

        //2.连接上之后会返回一个socket对象,得到和socket关联的输出流对象
        OutputStream outputStream = socket.getOutputStream();

        //3.写数据
        outputStream.write("hello,server".getBytes());
        System.out.println("客户端写数据。。。");
        socket.shutdownOutput();//输出结束标记

        //接收数据
        InputStream inputStream = socket.getInputStream();
        InputStreamReader isr = new InputStreamReader(inputStream, "utf-8");
        BufferedReader bufferedReader = new BufferedReader(isr);
        String s = bufferedReader.readLine();
        System.out.println("客户端接收=" + s);

        //4.关闭io流,关闭socket连接
        outputStream.close();
        socket.close();
        System.out.println("客户端退出。");
    }
}

运行结果:

3.应用案例03(字符流双向通信)

 
package com.feiyang.basic16_net;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author:飞扬
 * @公众hao:程序员飞扬
 * @description: socke通信,字符流,服务端
 */
public class SocketTCP03Server {
    public static void main(String[] args) throws IOException {
        //1.在本机9999端口监听
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("服务端在9999端口监听...");

        //2.当没有连接时,程序会阻塞,等待连接,如果有客户端连接,则会返回socket对象,程序继续
        Socket socket = serverSocket.accept();
        System.out.println("服务端socket=" + socket.getClass());

        //3.读取数据
        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
        String s = bufferedReader.readLine();
        System.out.println("服务端接收=" + s);

        //服务端发送消息
        OutputStream outputStream = socket.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
        bufferedWriter.write("hello,client,字符流");
        bufferedWriter.newLine();
        bufferedWriter.flush();

        //4.关闭流
        bufferedReader.close();
        bufferedWriter.close();
        socket.close();
        serverSocket.close();
        System.out.println("服务端退出。");

    }
}
package com.feiyang.basic16_net;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @author:飞扬
 * @公众hao:程序员飞扬
 * @description: socke通信,字符流,客户端
 */
public class SocketTCP03Client {
    public static void main(String[] args) throws IOException {
        //1.连接服务端(IP,端口)
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        System.out.println("客户端socket=" + socket.getClass());

        //2.连接上之后会返回一个socket对象,得到和socket关联的输出流对象
        OutputStream outputStream = socket.getOutputStream();

        //3.写数据
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
        bufferedWriter.write("hello,server,字符流");
        bufferedWriter.newLine();
        bufferedWriter.flush();

        //接收数据
        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
        String s = bufferedReader.readLine();
        System.out.println("客户端接收=" + s);

        //4.关闭io流,关闭socket连接
        bufferedReader.close();
        bufferedWriter.close();
        socket.close();
        System.out.println("客户端退出。");
    }
}

e1376a6e5aebf7e51369011b605c57d5.png

4.应用案例04(网络上传文件)

cb6aa7ff43dfd65870c831dbcecb2711.png

思路分析:

package com.feiyang.basic16_net;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author:飞扬
 * @公众hao:程序员飞扬
 * @description:
 */
public class StreamUtils {

    /**
     * stream流转换成字节数组
     * @param is
     * @return
     * @throws IOException
     */
    public static byte[] streamtoByteArray(InputStream is) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int len;
        while((len=is.read(b)) != -1){
            bos.write(b,0,len);
        }
        byte[] array = bos.toByteArray();
        bos.close();
        return array;
    }
/**
     * 将stream转换成String
     * @param is
     * @return
     * @throws IOException
     */
    public static String streamToString(InputStream is) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while((line = bufferedReader.readLine()) != null){
            stringBuilder.append(line + "\r\n");

        }
        return stringBuilder.toString();
    }
}

package com.feiyang.basic16_net;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author:飞扬
 * @公众hao:程序员飞扬
 * @description:
 */
public class TCPFileUploadServer {

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

        ServerSocket serverSocket = new ServerSocket(8888);
        Socket socket = serverSocket.accept();

        BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
        byte[] bytes = StreamUtils.streamtoByteArray(bis);

        String destPath = "src\\bbb.png";
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destPath));
        bos.write(bytes);
        System.out.println("服务端,写入完毕");
        bos.close();

        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        writer.write("服务端收到图片");
        System.out.println("服务端:服务端收到图片");
        writer.flush();
        socket.shutdownOutput();

        bis.close();
        socket.close();
        serverSocket.close();


    }
}

package com.feiyang.basic16_net;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @author:飞扬
 * @公众hao:程序员飞扬
 * @description:
 */
public class TCPFileUploadClient {

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket(InetAddress.getLocalHost(), 8888);

        String file = "d:\\bbb.png";
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        byte[] bytes = StreamUtils.streamtoByteArray(bis);

        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
        bos.write(bytes);
        bis.close();
        socket.shutdownOutput();

        String s = StreamUtils.streamToString(socket.getInputStream());
        System.out.println("客户端:" + s);

        bos.close();
        socket.close();


    }

}

运行结果:

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2022-10-08 21:14:47  更:2022-10-08 21:17:06 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/25 21:46:01-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码