Java专题十二:网络

2021-02-07 11:17

阅读:636

标签:bind   master   accept   back   static   cep   end   new   output   

Java专题十二:网络

目录
  • Java专题十二:网络
    • 12.1.套接字
      • 12.1.1 服务器端(java.net.ServerSocket)
      • 12.1.2客户端(java.net.Socket)
    • 12.2.高级API

12.1.套接字

  • Socket: 客户端套接字
  • ServerSocket: 服务器端套接字

Linux中Socket编程:http://www.man7.org/linux/man-pages/man2/socket.2.html

SOCKET_API DESCRIPTION
int socket(int domain, int type, int protocol); create an endpoint for communication
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); accept a connection on a socket
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); initiate a connection on a socket
int bind(int sockfd, const struct sockaddr *add, socklen_t addrlen); bind a name to a socket
int listen(int sockfd, int backlog); listen for connections on a socket
ssize_t send(int sockfd, const void *buf, size_t len, int flags); send a message on a socket
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen); send a message on a socket
ssize_t recv(int sockfd, const void *buf, size_t len, int flags); receive a message on a socket
ssize_t recvfrom(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen); receive a message on a socket
ssize_t read(int fd, void *buf, size_t count); read from a file descriptor
ssize_t write(int fd, const void *buf, size_t count); write to a file descriptor
int close(int fd); close a file descriptor

12.1.1 服务器端(java.net.ServerSocket

  • 创建Socket(new ServerSocket()
  • 绑定端口(bind(SocketAddress endpoint)
  • 监听端口(accept()
  • 等待客户端连接
  • 接收和发送数据(getInputStream()、getOutputStream()
  • 关闭Socket(close()

12.1.2客户端(java.net.Socket

  • 创建Socket(Socket()
  • 发起连接(connect(SocketAddress endpoint)
  • 发送和接收数据(getOutputStream()、getInputStream()
  • 关闭Socket(close()
    技术图片

12.2.高级API

java.net 包中的许多类可以提供更加高级的抽象,允许方便地访问网络上的资源。

  • URI: 表示统一资源标识符(URI)引用
  • URL: 表示统一资源定位器,即指向万维网上“资源”的指针
  • URLConnection: 表示应用程序和URL之间的通信链接
  • HttpURLConnection: 支持HTTP特定功能的URLConnection,规范见http://www.w3.org/pub/www/Protocols/

例:从网络上下载文件并保存到本地,详细参考NetTools

NetTools.download("https://dl.google.com/android/repository/platform-tools-latest-windows.zip","");
public static boolean download(String url, String saveDir)
        throws URISyntaxException, IOException {
    // get download file name
    String fileName = url.substring(url.indexOf(‘/‘));
    if (saveDir == null || saveDir.trim().equals("")){
        saveDir = "";
    }else{
        File out = new File(saveDir);
        if (!out.exists() || !out.isDirectory()){
            throw new FileNotFoundException();
        }
    }
    // open connection
    URI uri = new URI(url);
    URL url0 = uri.toURL();
    HttpURLConnection conn = (HttpURLConnection)url0.openConnection();
    conn.setRequestProperty("User-Agent",
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36");
    conn.connect();
    // response is ok
    if (conn.getResponseCode() == HttpURLConnection.HTTP_OK){
        InputStream is = conn.getInputStream();
        File file = new File(saveDir, fileName);
        FileOutputStream fos = new FileOutputStream(new File("platform-tools-latest-windows.zip"));
        byte[] buf = new byte[1024];
        int readCount;
        while((readCount = is.read(buf)) > 0){
            fos.write(buf, 0, readCount);
        }
        is.close();
        fos.close();
        return true;
    }

    return false;
}

Java专题十二:网络

标签:bind   master   accept   back   static   cep   end   new   output   

原文地址:https://www.cnblogs.com/myibu/p/12775638.html


评论


亲,登录后才可以留言!