• 网络编程学习


    目录

    1.IP

    2.TcpClientDemo01客户端

    3.TcpServerDemo01服务端

    4.UdpClientDemo01 客户端

    5.UdpServerDemo01 服务端

    6.循环发送消息

    7.循环接收消息


    1.IP

    package com.qyx.www.lesson11;
    ​
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    ​
    //测试IP
    public class TestInetAddress {
        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());//规范的名字
                System.out.println(inetAddress2.getHostAddress());//ip
                System.out.println(inetAddress2.getHostName()); //域名,或者自己电脑的名字
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }
    }

    2.TcpClientDemo01客户端

    package com.qyx.www.lesson11;
    ​
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.nio.charset.StandardCharsets;
    ​
    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 = 8080;
                //2.创建一个socket连接
                socket = new Socket(serverIP,port);
                //3.发送消息 IO流
                os = socket.getOutputStream();
                os.write("你好,欢迎学习狂神说java".getBytes());
    ​
    ​
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if (os!=null){
                    try {
                        os.close();
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                }
    ​
                if (socket!=null){
                    try {
                        socket.close();
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    3.TcpServerDemo01服务端

    package com.qyx.www.lesson11;
    ​
    import java.io.ByteArrayInputStream;
    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;
            InputStream is = null;
            ByteArrayOutputStream baos = 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){
                e.printStackTrace();
            }finally {
                //关闭资源
                if (baos!=null){
                    try {
                        baos.close();
                    }catch (IOException e){
                        e.printStackTrace();
                    }finally {
                        //关闭资源
                        if (baos!=null){
                        try {
                            baos.close();
                        }catch (IOException e){
                            e.printStackTrace();
                        }
                      }
                        if (is!=null){
                            try {
                                is.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();
                            }
                        }
                    }
                }
            }
        }
    }

    4.UdpClientDemo01 客户端

    package com.qyx.www.lesson11;
    ​
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.nio.charset.StandardCharsets;
    ​
    //不需要连接服务器
    public class UdpClientDemo01 {
        public static void main(String[] args) throws Exception{
            //1.建立一个Socket
            DatagramSocket socket = new DatagramSocket();
    ​
            //2.建个包
            String msg = "你好啊,服务器!";
            InetAddress localhost = InetAddress.getByName("localhost");
            int port = 9095;
    ​
            //数据,数据的长度起始,要发送给谁
            DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);
    ​
            //3.发送包
            socket.send(packet);
    ​
            //4.关闭流
            socket.close();
    ​
        }
    }

    5.UdpServerDemo01 服务端

    package com.qyx.www.lesson11;
    ​
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    ​
    public class UdpServerDemo01 {
        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();
        }
    }

    6.循环发送消息

    package com.qyx.www.lesson11;
    ​
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetSocketAddress;
    import java.net.SocketException;
    ​
    public class UdpSenderDemo01 {
        public static void main(String[] args) throws Exception {
            DatagramSocket socket = new DatagramSocket(8080);
    ​
            //准备数据: 控制台读取 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("byte")){
                    break;
                }
            }
    ​
            socket.close();
    ​
        }
    }

    7.循环接收消息

    package com.qyx.www.lesson11;
    ​
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.SocketException;
    ​
    public class UdpReceiveDemo01 {
        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); //阻塞式接受包裹
    ​
                //断开连接 bye
                byte[] data = packet.getData();
                String receiveData = new String(data,0, data.length);
    ​
                System.out.println(receiveData);
    ​
                if (receiveData.equals("bye")){
                    break;
                }
            }
    ​
            socket.close();
        }
    }
  • 相关阅读:
    [lettcode top 100] 0917 两数之和,有效的括号
    pyQt界面制作(登录+跳转页面)
    npm 常用的命令
    分布式.幂等性
    C语言之指针详解
    【week307-amazon】leetcode2386. 找出数组的第 K 大和
    【背包问题】基于禁忌搜索算法求解背包问题附Matlab代码
    【 java 常用类】String、StringBuffer、StringBuilder三者之间的效率对比
    使用CXF调用WSDL(二)
    蛇形填空 I
  • 原文地址:https://blog.csdn.net/qq_60154877/article/details/126348251