<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## Java專題十二:網絡 [TOC] ### 12.1.套接字 - `Socket`: 客戶端套接字 - `ServerSocket`: 服務器端套接字 > Linux中Socket編程:[http://www.man7.org/linux/man-pages/man2/socket.2.html](http://www.man7.org/linux/man-pages/man2/socket.2.html) <table > <tr> <th style="width:70%">SOCKET_API</th> <th style="width:30%">DESCRIPTION</th> </tr> <tr> <td>int socket(int domain, int type, int protocol);</td> <td>create an endpoint for communication</td> </tr> <tr> <td>int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); </td> <td>accept a connection on a socket</td> </tr> <tr> <td> int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);</td> <td>initiate a connection on a socket</td> </tr> <tr> <td> int bind(int sockfd, const struct sockaddr *add, socklen_t addrlen);</td> <td>bind a name to a socket </td> </tr> <tr> <td>int listen(int sockfd, int backlog);</td> <td> listen for connections on a socket </td> </tr> <tr> <td>ssize_t send(int sockfd, const void *buf, size_t len, int flags); </td> <td>send a message on a socket </td> </tr> <tr> <td>ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen); </td> <td>send a message on a socket </td> </tr> <tr> <td>ssize_t recv(int sockfd, const void *buf, size_t len, int flags);</td> <td>receive a message on a socket</td> </tr> <tr> <td>ssize_t recvfrom(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen);</td> <td>receive a message on a socket</td> </tr> <tr> <td>ssize_t read(int fd, void *buf, size_t count); </td> <td>read from a file descriptor</td> </tr> <tr> <td>ssize_t write(int fd, const void *buf, size_t count); </td> <td>write to a file descriptor</td> </tr> <tr> <td>int close(int fd); </td> <td>close a file descriptor</td> </tr> </table> #### 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()`) ![](https://img.kancloud.cn/4d/29/4d2919823ccbd00a9de874a3cf0e7044_280x354.png) ### 12.2.高級API java.net 包中的許多類可以提供更加高級的抽象,允許方便地訪問網絡上的資源。 - `URI`: 表示統一資源標識符(URI)引用 - `URL`: 表示統一資源定位器,即指向萬維網上“資源”的指針 - `URLConnection`: 表示應用程序和URL之間的通信鏈接 - `HttpURLConnection`: 支持HTTP特定功能的URLConnection,規范見[http://www.w3.org/pub/www/Protocols/](http://www.w3.org/pub/www/Protocols) 例:從網絡上下載文件并保存到本地,詳細參考[NetTools](https://github.com/15045120/git-docs/blob/master/tools/NetTools.java) ``` 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; } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看