day92---Java基础之网络编程

day92---Java基础之网络编程

1、InetAddress类的使用

package com.az.day92;

import java.net.InetAddress;

public class InetAddressTest {
    public static void main(String[] args) {

        try {
            InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
            System.out.println(inetAddress1);
			//	/127.0.0.1

            InetAddress inetAddress2 = InetAddress.getByName("www.mi.com");
            System.out.println(inetAddress2);
			//	www.mi.com/183.232.187.9

            InetAddress inetAddress3 = InetAddress.getByName("10.68.8.149");
            System.out.println(inetAddress3);
            //	/10.68.8.149

            //获取本地主机地址
            InetAddress inetAddress4 = InetAddress.getLocalHost();
            System.out.println(inetAddress4);
            //	azspc/10.68.8.149

            //获取主机名
            System.out.println(inetAddress2.getHostName());
            //	www.mi.com

            //获取主机地址
            System.out.println(inetAddress2.getHostAddress());
            //	183.232.187.9

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2、TCP网络编程

服务端

package com.az.day92;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author az
 * @create 2020/11/23 22:20
 */
public class TestServer {
    public static void main(String[] args) {
        //服务端
        try (
                //1.创建服务器端的ServerSocket,指明自己的端口号
                ServerSocket ss = new ServerSocket(8899);

                //2.调用accept()表示接收来自于客户端的socket
                Socket socket = ss.accept();

                //3.获取输入流
                InputStream is = socket.getInputStream();

                //4.读取输入流中的数据
                ByteArrayOutputStream baos = new ByteArrayOutputStream();


        ) {
            byte[] buffer = new byte[5];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }

            System.out.println(baos.toString());
            System.out.println("收到了来自于:" + socket.getInetAddress() + "的数据");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

客户端

package com.az.day92;

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

public class TCPClient {
    public static void main(String[] args) {
        //客户端
        try (
                //1、创建一个socket对象,指明服务器的IP和端口号
                Socket socket = new 
            			Socket(InetAddress.getByName("10.68.8.149"), 8899);

                //2、获取一个输出流,用于输出数据
                OutputStream os = socket.getOutputStream();

        ) {

            //3、写出数据
            os.write("我是客户端GG".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3、UDP网络编程

package com.az.day92;

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

/**
 * @author az
 * @create 2020/11/23 22:57
 */
public class UDPReceiverTest {
    public static void main(String[] args) {
        //接收端
        try (
                DatagramSocket socket = new DatagramSocket(9090);
        ) {

            byte[] buffer = new byte[100];
            DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);

            try {
                socket.receive(packet);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println(new String(packet.getData(), 0, packet.getLength()));
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
}
package com.az.day92;

import java.io.IOException;
import java.net.*;

/**
 * @author az
 * @create 2020/11/23 22:43
 */
public class UDPSenderTest {
    public static void main(String[] args) {
        //发送端
        try (
                DatagramSocket socket = new DatagramSocket();

        ) {

            String str = "biu~biu~bbiu~~";
            byte[] data = str.getBytes();

            InetAddress inetAddress1 = null;
            try {
                inetAddress1 = InetAddress.getLocalHost();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            DatagramPacket packet =
                    new DatagramPacket(data, 0, data.length, inetAddress1, 9090);

            try {
                socket.send(packet);
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
}

……:

2020年11月23日 周一,

上一篇:JAVA网络编程InetAddress类


下一篇:InetAddress类简介