• 网络编程 笔记


    目录

    域名和端口号 

     网络通讯协议 

    TCP和UDP

    ​编辑 InetAddress类

    网络多线程_Socket (套接字)

    小练习

     Natstat 指令

    ​编辑

     TCP网络通信小秘密

    ​编辑 UDP网络通信编程

     基础介绍

    UDP小练习


    域名和端口号 

     注意:1.网络开发中不要使用0-1024的端口

    2.端口号不能重复,否则会出现端口被占用的情况

     网络通讯协议 

    人和人交流(通讯):依靠语言【中文,英语,日语...】 

    语言本身就是一种协议。

    在网络编程中,数据的组织形式就是协议。

    TCP和UDP

     InetAddress类

     用法

    1. //获取本机的InetAddress对象
    2. InetAddress localHost = InetAddress.getLocalHost();
    3. System.out.println(localHost);
    4. //2.根据指定的主机名获取 InetAddress对象
    5. InetAddress host1 = InetAddress.getByName("DESKTOP-6HLDCVM");
    6. System.out.println(host1);
    7. //3.根据域名 返回InetAddress对象比如 www.baidu.com
    8. InetAddress host2 = InetAddress.getByName("www.baidu.com");
    9. System.out.println(host2);
    10. //4.通过返回InetAddress对象 反向获取地址
    11. String hostAddress = host2.getCanonicalHostName();
    12. System.out.println(hostAddress);
    13. //5. 通过INetAddress 获取域名/主机名
    14. String hostName = host2.getHostName();
    15. String hostName2 = host1.getHostName();
    16. System.out.println(hostName);
    17. System.out.println(hostName2);

    网络多线程_Socket (套接字)

     

             发起连接的一方通过Socket调用OutputStream将数据写到数据通道中,然后接收方通过Socket调用InputStream读取数据。结束连接的时候要关闭Socket。

    小练习

     思路分析

    客户端 

    1. public class Scoket02 {
    2. public static void main(String[] args) throws IOException {
    3. //1.连接本机的9999端口 ,连接成功返回一个socket对象
    4. Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
    5. System.out.println("socket对象获取");
    6. //得到和 socket对象关联的输出流对象
    7. OutputStream outputStream = socket.getOutputStream();
    8. //通过输出流 写入数据到通道
    9. outputStream.write("hello".getBytes());
    10. //关闭流
    11. socket.close();
    12. outputStream.close();
    13. System.out.println("客户端关闭");
    14. }
    15. }

    服务端

    1. //服务端
    2. public class Socket01 {
    3. public static void main(String[] args) throws IOException {
    4. //在本机的9999端口监听,等待连接
    5. ServerSocket serverSocket = new ServerSocket(9999);
    6. System.out.println("服务端在9999监听");
    7. //当没有客户端连接9999端口时,程序会阻塞,等待连接
    8. Socket socket = serverSocket.accept();//当有客户端连接时会返回一个 socket对象
    9. System.out.println("有客户连接,正在获取数据");
    10. //通过socket 获取通道中的数据
    11. InputStream inputStream = socket.getInputStream();
    12. //IO读取
    13. byte[] buf =new byte[1024];
    14. int readLen = 0;
    15. while ((readLen = inputStream.read(buf)) != -1){
    16. System.out.println(new String(buf,0,readLen));
    17. }
    18. //关闭流
    19. inputStream.close();
    20. socket.close();
    21. serverSocket.close();
    22. }
    23. }

     小练习2

    1. public class Socket03 {
    2. public static void main(String[] args) throws IOException {
    3. //服务器在9999端口监听
    4. ServerSocket serverSocket = new ServerSocket(9999);
    5. //等待客户端连接
    6. System.out.println("等待用户端连接");
    7. Socket socket = serverSocket.accept();
    8. //有客户端连接 读取通道内消息
    9. InputStream inputStream = socket.getInputStream();
    10. int readlen = 0;
    11. byte[] buf = new byte[1024];
    12. while ((readlen = inputStream.read(buf)) != -1 ){
    13. System.out.println(new String(buf,0,readlen));
    14. }
    15. //获取数据后准备,回信
    16. OutputStream outputStream = socket.getOutputStream();
    17. outputStream.write("hello,client".getBytes());
    18. socket.shutdownOutput();
    19. //关闭流
    20. outputStream.close();
    21. inputStream.close();
    22. socket.close();
    23. serverSocket.close();
    24. System.out.println("服务端退出");
    25. }
    26. }
    1. public class Socket04 {
    2. public static void main(String[] args) throws IOException {
    3. //连接到9999端口
    4. Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
    5. //发送消息
    6. OutputStream outputStream = socket.getOutputStream();
    7. outputStream.write("hello server".getBytes());
    8. System.out.println("等待对方回信");
    9. socket.shutdownOutput();
    10. //接收回信
    11. InputStream inputStream = socket.getInputStream();
    12. int i = 0;
    13. byte[] b = new byte[1024];
    14. while ((i = inputStream.read(b))!= -1){
    15. System.out.println(new String(b,0,i));
    16. }
    17. System.out.println("客户端退出");
    18. //关闭流
    19. inputStream.close();
    20. outputStream.close();
    21. socket.close();
    22. }
    23. }

    小练习3

    有了前面的示范,这题也很容易解决

    利用Buffered 和 转换流

    1. public class Socket05 {
    2. public static void main(String[] args) throws IOException {
    3. //监听9999
    4. ServerSocket serverSocket = new ServerSocket(9999);
    5. //等待连接
    6. Socket socket = serverSocket.accept();
    7. InputStream inputStream = socket.getInputStream();
    8. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    9. String string = bufferedReader.readLine();
    10. System.out.println(string);
    11. //回信
    12. OutputStream outputStream = socket.getOutputStream();
    13. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
    14. bufferedWriter.write("你也好,客户端");
    15. bufferedWriter.newLine();//换行表示输入结束
    16. bufferedWriter.flush();如果使用字符流一点要刷新,否则不能加入通道
    17. //关闭流
    18. socket.close();
    19. serverSocket.close();
    20. bufferedReader.close();
    21. bufferedWriter.close();
    22. System.out.println("服务端关闭");
    23. }
    24. }
    1. public class Socket06 {
    2. public static void main(String[] args) throws IOException {
    3. Socket socket = new Socket(InetAddress.getLocalHost(),9999);
    4. OutputStream outputStream = socket.getOutputStream();
    5. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
    6. bufferedWriter.write("你好啊服务器");
    7. bufferedWriter.newLine();//换行表示输入结束
    8. bufferedWriter.flush();//如果使用字符流一点要刷新,否则不能加入通道
    9. //接收回信
    10. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    11. String string = bufferedReader.readLine();
    12. System.out.println(string);
    13. //关闭流
    14. bufferedReader.close();
    15. bufferedWriter.close();
    16. socket.close();
    17. System.out.println("服务端关闭");
    18. }
    19. }

     Natstat 指令

    还有个 netstat -anb  可以显示哪个程序连接到这个端口

     TCP网络通信小秘密

     UDP网络通信编程

     基础介绍

     

     

    UDP小练习

    1. public class UdpReceiverA {
    2. public static void main(String[] args) throws IOException {
    3. //1.创建一个DatagramSocket,准备在9999接收数据
    4. DatagramSocket datagramSocket = new DatagramSocket(9999);
    5. //2.构建一个 DatagramPacket对象,准备接收数据
    6. //UDP协议数据包最大64k 64*1024
    7. byte[] buf = new byte[64 * 1024];
    8. DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
    9. //3.调用,接收方法,将通过网络传输的 DatagramPacket 对象
    10. datagramSocket.receive(datagramPacket);//在9999端口等待,如果有数据包传输,就接收
    11. //4.把Packet 进行拆包,取出数据并显示
    12. int len = datagramPacket.getLength();//实际接收到的数据字节长度
    13. byte[] data = datagramPacket.getData();//接收到的数据
    14. String s =new String(data,0,len);
    15. System.out.println(s);
    16. //回复消息
    17. byte[] bytes = "没问题,我去找你".getBytes();
    18. DatagramPacket datagramPacket1 = new DatagramPacket(bytes,bytes.length, InetAddress.getByName("192.168.43.237"),9998);
    19. datagramSocket.send(datagramPacket1);
    20. //5.关闭流
    21. datagramSocket.close();
    22. }
    23. }
    1. public class UdpSenderB {
    2. public static void main(String[] args) throws IOException {
    3. //1。创建DatagramPacket 对象,准备接收和发送数据
    4. DatagramSocket socket = new DatagramSocket(9998);//发送端也可以是接收端,如果
    5. //之后接收数据,就在9998端口接收
    6. //2.发送数据封转 DatagramPacket(数组,数组长度,对方ip地址,端口号)
    7. byte[] bytes = "hello,今天吃火锅".getBytes();
    8. DatagramPacket packet =
    9. new DatagramPacket(bytes,bytes.length, InetAddress.getByName("192.168.43.237"),9999);
    10. socket.send(packet);//发送封装后的数据
    11. //接收回信
    12. byte[] bytes1 = new byte[1024*5];//
    13. DatagramPacket datagramPacket = new DatagramPacket(bytes1, bytes1.length);
    14. int len = datagramPacket.getLength();
    15. socket.receive(datagramPacket);
    16. byte[] bytes2 = datagramPacket.getData();
    17. String s = new String(bytes2,0,len);
    18. System.out.println(s);
    19. socket.close();
    20. }
    21. }

  • 相关阅读:
    SpringBoot-2.4 新建工程及常规配置
    【论文阅读 07】Anomaly region detection and localization in metal surface inspection
    【分层强化学习】HAC源码解读
    Spring Boot之AJAX异步发送帖子
    React Server Component: 混合式渲染
    SpringCloud_GateWay服务网关
    6-9接口应用:工厂模式
    人不成熟的特征
    Ubuntu 设置开机自动执行脚本
    【附源码】Python计算机毕业设计社区论坛
  • 原文地址:https://blog.csdn.net/hagong9/article/details/125995211