<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FNetwork_Protocol%2FSocketAddress</id>
		<title>Java/Network Protocol/SocketAddress - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FNetwork_Protocol%2FSocketAddress"/>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java/Network_Protocol/SocketAddress&amp;action=history"/>
		<updated>2026-04-11T04:50:00Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://jexp.ru/index.php?title=Java/Network_Protocol/SocketAddress&amp;diff=8933&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java/Network_Protocol/SocketAddress&amp;diff=8933&amp;oldid=prev"/>
				<updated>2010-06-01T07:21:48Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 07:21, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://jexp.ru/index.php?title=Java/Network_Protocol/SocketAddress&amp;diff=8932&amp;oldid=prev</id>
		<title> в 18:01, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java/Network_Protocol/SocketAddress&amp;diff=8932&amp;oldid=prev"/>
				<updated>2010-05-31T18:01:48Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Http get with CharBuffer and ByteBuffer ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 2004 David Flanagan.  All rights reserved.&lt;br /&gt;
 * This code is from the book Java Examples in a Nutshell, 3nd Edition.&lt;br /&gt;
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.&lt;br /&gt;
 * You may study, use, and modify it for any non-commercial purpose,&lt;br /&gt;
 * including teaching and use in open-source projects.&lt;br /&gt;
 * You may distribute it non-commercially as long as you retain this notice.&lt;br /&gt;
 * For a commercial use license, or to purchase the book, &lt;br /&gt;
 * please visit http://www.davidflanagan.ru/javaexamples3.&lt;br /&gt;
 */&lt;br /&gt;
//package je3.nio;&lt;br /&gt;
import java.io.FileOutputStream;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.net.InetSocketAddress;&lt;br /&gt;
import java.net.SocketAddress;&lt;br /&gt;
import java.net.URI;&lt;br /&gt;
import java.nio.BufferUnderflowException;&lt;br /&gt;
import java.nio.ByteBuffer;&lt;br /&gt;
import java.nio.CharBuffer;&lt;br /&gt;
import java.nio.channels.Channels;&lt;br /&gt;
import java.nio.channels.SocketChannel;&lt;br /&gt;
import java.nio.channels.WritableByteChannel;&lt;br /&gt;
import java.nio.charset.Charset;&lt;br /&gt;
public class HttpGet {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    SocketChannel server = null; // Channel for reading from server&lt;br /&gt;
    FileOutputStream outputStream = null; // Stream to destination file&lt;br /&gt;
    WritableByteChannel destination; // Channel to write to it&lt;br /&gt;
    try { // Exception handling and channel closing code follows this block&lt;br /&gt;
      // Parse the URL. Note we use the new java.net.URI, not URL here.&lt;br /&gt;
      URI uri = new URI(args[0]);&lt;br /&gt;
      // Now query and verify the various parts of the URI&lt;br /&gt;
      String scheme = uri.getScheme();&lt;br /&gt;
      if (scheme == null || !scheme.equals(&amp;quot;http&amp;quot;))&lt;br /&gt;
        throw new IllegalArgumentException(&amp;quot;Must use &amp;quot;http:&amp;quot; protocol&amp;quot;);&lt;br /&gt;
      String hostname = uri.getHost();&lt;br /&gt;
      int port = uri.getPort();&lt;br /&gt;
      if (port == -1)&lt;br /&gt;
        port = 80; // Use default port if none specified&lt;br /&gt;
      String path = uri.getRawPath();&lt;br /&gt;
      if (path == null || path.length() == 0)&lt;br /&gt;
        path = &amp;quot;/&amp;quot;;&lt;br /&gt;
      String query = uri.getRawQuery();&lt;br /&gt;
      query = (query == null) ? &amp;quot;&amp;quot; : &amp;quot;?&amp;quot; + query;&lt;br /&gt;
      // Combine the hostname and port into a single address object.&lt;br /&gt;
      // java.net.SocketAddress and InetSocketAddress are new in Java 1.4&lt;br /&gt;
      SocketAddress serverAddress = new InetSocketAddress(hostname, port);&lt;br /&gt;
      // Open a SocketChannel to the server&lt;br /&gt;
      server = SocketChannel.open(serverAddress);&lt;br /&gt;
      // Put together the HTTP request we&amp;quot;ll send to the server.&lt;br /&gt;
      String request = &amp;quot;GET &amp;quot; + path + query + &amp;quot; HTTP/1.1\r\n&amp;quot; + // The request&lt;br /&gt;
          &amp;quot;Host: &amp;quot; + hostname + &amp;quot;\r\n&amp;quot; + // Required in HTTP 1.1&lt;br /&gt;
          &amp;quot;Connection: close\r\n&amp;quot; + // Don&amp;quot;t keep connection open&lt;br /&gt;
          &amp;quot;User-Agent: &amp;quot; + HttpGet.class.getName() + &amp;quot;\r\n&amp;quot; + &amp;quot;\r\n&amp;quot;; // Blank&lt;br /&gt;
                                                                      // line&lt;br /&gt;
                                                                      // indicates&lt;br /&gt;
                                                                      // end of&lt;br /&gt;
                                                                      // request&lt;br /&gt;
                                                                      // headers&lt;br /&gt;
      // Now wrap a CharBuffer around that request string&lt;br /&gt;
      CharBuffer requestChars = CharBuffer.wrap(request);&lt;br /&gt;
      // Get a Charset object to encode the char buffer into bytes&lt;br /&gt;
      Charset charset = Charset.forName(&amp;quot;ISO-8859-1&amp;quot;);&lt;br /&gt;
      // Use the charset to encode the request into a byte buffer&lt;br /&gt;
      ByteBuffer requestBytes = charset.encode(requestChars);&lt;br /&gt;
      // Finally, we can send this HTTP request to the server.&lt;br /&gt;
      server.write(requestBytes);&lt;br /&gt;
      // Set up an output channel to send the output to.&lt;br /&gt;
      if (args.length &amp;gt; 1) { // Use a specified filename&lt;br /&gt;
        outputStream = new FileOutputStream(args[1]);&lt;br /&gt;
        destination = outputStream.getChannel();&lt;br /&gt;
      } else&lt;br /&gt;
        // Or wrap a channel around standard out&lt;br /&gt;
        destination = Channels.newChannel(System.out);&lt;br /&gt;
      // Allocate a 32 Kilobyte byte buffer for reading the response.&lt;br /&gt;
      // Hopefully we&amp;quot;ll get a low-level &amp;quot;direct&amp;quot; buffer&lt;br /&gt;
      ByteBuffer data = ByteBuffer.allocateDirect(32 * 1024);&lt;br /&gt;
      // Have we discarded the HTTP response headers yet?&lt;br /&gt;
      boolean skippedHeaders = false;&lt;br /&gt;
      // The code sent by the server&lt;br /&gt;
      int responseCode = -1;&lt;br /&gt;
      // Now loop, reading data from the server channel and writing it&lt;br /&gt;
      // to the destination channel until the server indicates that it&lt;br /&gt;
      // has no more data.&lt;br /&gt;
      while (server.read(data) != -1) { // Read data, and check for end&lt;br /&gt;
        data.flip(); // Prepare to extract data from buffer&lt;br /&gt;
        // All HTTP reponses begin with a set of HTTP headers, which&lt;br /&gt;
        // we need to discard. The headers end with the string&lt;br /&gt;
        // &amp;quot;\r\n\r\n&amp;quot;, or the bytes 13,10,13,10. If we haven&amp;quot;t already&lt;br /&gt;
        // skipped them then do so now.&lt;br /&gt;
        if (!skippedHeaders) {&lt;br /&gt;
          // First, though, read the HTTP response code.&lt;br /&gt;
          // Assume that we get the complete first line of the&lt;br /&gt;
          // response when the first read() call returns. Assume also&lt;br /&gt;
          // that the first 9 bytes are the ASCII characters&lt;br /&gt;
          // &amp;quot;HTTP/1.1 &amp;quot;, and that the response code is the ASCII&lt;br /&gt;
          // characters in the following three bytes.&lt;br /&gt;
          if (responseCode == -1) {&lt;br /&gt;
            responseCode = 100 * (data.get(9) - &amp;quot;0&amp;quot;) + 10 * (data.get(10) - &amp;quot;0&amp;quot;) + 1&lt;br /&gt;
                * (data.get(11) - &amp;quot;0&amp;quot;);&lt;br /&gt;
            // If there was an error, report it and quit&lt;br /&gt;
            // Note that we do not handle redirect responses.&lt;br /&gt;
            if (responseCode &amp;lt; 200 || responseCode &amp;gt;= 300) {&lt;br /&gt;
              System.err.println(&amp;quot;HTTP Error: &amp;quot; + responseCode);&lt;br /&gt;
              System.exit(1);&lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
          // Now skip the rest of the headers.&lt;br /&gt;
          try {&lt;br /&gt;
            for (;;) {&lt;br /&gt;
              if ((data.get() == 13) &amp;amp;&amp;amp; (data.get() == 10) &amp;amp;&amp;amp; (data.get() == 13)&lt;br /&gt;
                  &amp;amp;&amp;amp; (data.get() == 10)) {&lt;br /&gt;
                skippedHeaders = true;&lt;br /&gt;
                break;&lt;br /&gt;
              }&lt;br /&gt;
            }&lt;br /&gt;
          } catch (BufferUnderflowException e) {&lt;br /&gt;
            // If we arrive here, it means we reached the end of&lt;br /&gt;
            // the buffer and didn&amp;quot;t find the end of the headers.&lt;br /&gt;
            // There is a chance that the last 1, 2, or 3 bytes in&lt;br /&gt;
            // the buffer were the beginning of the \r\n\r\n&lt;br /&gt;
            // sequence, so back up a bit.&lt;br /&gt;
            data.position(data.position() - 3);&lt;br /&gt;
            // Now discard the headers we have read&lt;br /&gt;
            data.rupact();&lt;br /&gt;
            // And go read more data from the server.&lt;br /&gt;
            continue;&lt;br /&gt;
          }&lt;br /&gt;
        }&lt;br /&gt;
        // Write the data out; drain the buffer fully.&lt;br /&gt;
        while (data.hasRemaining())&lt;br /&gt;
          destination.write(data);&lt;br /&gt;
        // Now that the buffer is drained, put it into fill mode&lt;br /&gt;
        // in preparation for reading more data into it.&lt;br /&gt;
        data.clear(); // data.rupact() also works here&lt;br /&gt;
      }&lt;br /&gt;
    } catch (Exception e) { // Report any errors that arise&lt;br /&gt;
      System.err.println(e);&lt;br /&gt;
      System.err.println(&amp;quot;Usage: java HttpGet &amp;lt;URL&amp;gt; [&amp;lt;filename&amp;gt;]&amp;quot;);&lt;br /&gt;
    } finally { // Close the channels and output file stream, if needed&lt;br /&gt;
      try {&lt;br /&gt;
        if (server != null &amp;amp;&amp;amp; server.isOpen())&lt;br /&gt;
          server.close();&lt;br /&gt;
        if (outputStream != null)&lt;br /&gt;
          outputStream.close();&lt;br /&gt;
      } catch (IOException e) {&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>