Java笔记--网络编程

1、IP地址:InetAddress类

--唯一的标识Internet上的计算机

--本地回环地址(hostAddress)127.0.0.1 主机名(hostName):localhost

//根据域名获取IP地址信息
InetAddress inet = InetAddress.getByName("localhost");
System.out.println(inet.getHostName());
System.out.println(inet.getHostAddress());
        
//获取本机IP地址
inet = InetAddress.getLocalHost();
System.out.println(inet);

2、端口号:标识正在计算机上运行的进程。

3、网络通信协议:计算机网络中实现通信必须有一些约定,即通信协议,对速率、传输代码、代码结构、传输控制步骤、出错控制等制定标准;

--传输层中的两个重要协议:1、传输控制协议TCP(Transmission Control Protocol); 2、用户数据报协议UDP(User DatagramProtocol)。4、网络套接字Socket:端口号与IP地址的组合。

--网络通信就是Socket间的通信,Socket允许程序把网络程序当成一个流,数据在两个Socket间通过IO传输。

--主动发起通信的应用程序成为客户端,等待通信请求的成为服务端。

4、Socket编程例子(TCP):

--Client:

public void client() {
        Socket s = null;
        OutputStream os = null;
        InputStream is = null;
        
        try{
            //1.创建一个Socket对象,通过构造器指明服务端的IP地址和接收程序的端口号
            s = new Socket("127.0.0.1", 9090);
            //2.getOutputStream获得OutputStream对象后发送数据
            os = s.getOutputStream();
            os.write("This is Client".getBytes());
            //3.发送完毕之后关闭发送
            s.shutdownOutput();
            
            //4.getInoutStream获得InoutStream对象,接收服务器返回的数据
            is = s.getInputStream();
            byte[] b = new byte[20];
            int len = 0;
            while((len = is.read(b)) != -1){
                System.out.println(new String(b, 0, len));
            }
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            //5.关闭相应的资源
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(s != null){
                try {
                    s. close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
    }

--Server:

public void Server() {
        ServerSocket ss = null;
        Socket s = null;
        InputStream is = null;
        OutputStream os = null;
        try{
            ss = new ServerSocket(9090);
            s = ss.accept();
            is = s.getInputStream();
            byte[] b = new byte[20];
            int len = 0;
            while((len = is.read(b)) != -1){
                System.out.println(new String(b, 0, len));
            }
            
            os = s.getOutputStream();
            os.write("Accepted".getBytes());
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(s != null){
                try {
                    s. close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(ss != null){
                try {
                    ss.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

 5、类DatagramSocket和DataPacket实现了基于UDP协议的网络程序:

--UDP数据包通过数据报套接字DatagramSocket发送和接收,系统不保证UDP数据报一定能够安全送达目的地,也不确定什么时候可以到达;

--DatagramPacket类封装了UDP数据报,在数据报中包含了发送端的IP地址和端口号以及接收端的IP地址和端口号;

--UDP协议中每个数据报都给出了完整的地址信息,因此无须建立发送放与接收方的连接。

6、DatagramSocket编程(UDP):

--Client:

public void client() {
    DatagramSocket ds = null;
    try {
        ds = new DatagramSocket();
            
        byte[] b = "This is Client".getBytes();
        DatagramPacket dp = new DatagramPacket(b, 0, b.length, InetAddress.getByName("127.0.0.1"), 8787);
            
        ds.send(dp);
    }catch (IOException e) {
        e.printStackTrace();
    }finally{
        ds.close();
    }
}

--Server:

public void server(String[] args) {
    DatagramSocket ds = null;
    try {
        ds = new DatagramSocket(8787);
            
        byte[] b = new byte[1024];
        DatagramPacket dp = new DatagramPacket(b, 0, b.length);
            
        ds.receive(dp);
        System.out.println(new String(b, 0, dp.getLength()));
    }catch (IOException e) {
        e.printStackTrace();
    }finally{
        ds.close();
    }
}

7、URL编程(Uniform Resource Locator:统一资源定位符)

--构成:<传输协议>://<主机名>:端口号/<文件名>;

--URL类:一个URL对象对应着网络上的一个资源;

--URL类常用方法:1)public String getProtocol();//获取该URL的协议

         2)public String getHost();//获取该URL的主机名

         3)public String getPort();//获取该URL的端口号

         4)public String getPath();//获取该URL的文件路径

         5)public String getFile();//获取文件名

         6)public String getRef();//获取文件的相对位置

         7)public String getQuery();//获取该URL的查询名

--URL的openStream()方法能够从网络上读取数据。

8、URLConnection:表示URL所引用的远程对象的连接:

--UrlConnection urlConnection =  url.openConnection();

--InputStream is  = urlConnection.getInputStream();//输入流

--OutputStream os = urlConnection.getOutputStream();//输出流

原文地址:https://www.cnblogs.com/tengtao93/p/4928611.html