     9 

      Java 

    ,     Java   
    Internet.    
    ,   
  Web-      
      ( java.net). 
     Java       Internet    
intranet.     Internet-,  
,  TCP/IP.     
   TCP/IP    , 
   Internet  intranet-.    
-  Java   java.net.  ,  
   ,      , 
 -   ,    
Internet   . 
         TCP/IP    
.       ,    
 !]1"   ,  , 
   java.net. 
      Internet   ^   , 
  (socket).        . 
    ""  . ,   
  ,    .   java.net 
  Socket.  ,      
  ,    Java InputStream  
OutputStream. (          8.) 
 ,           
   ,         
 . 
         Internet,  ,  ,  
IP-,    .     Java,  
      ,  
 ,      

    265 

      (, HTTP-)  Java.    
  URL,         
HTTP-  FTP-. 
    I 

     Internet: TCP/IP 

       Internet  ""      
.    TCP/IP (Transmission Control Protocol/Internet Protocol). 
TCP/IP     ,  
         
.       :   
lOBaseT,  ,  ISDN-     
 .     ,  Internet. 
    TCP/IP         
  , ,     
  .       8.   
  ,        
  ,       Internet. 
     .       
 TCP/IP,    .     
  WWW,    http://ds.internic.net.   
    Internet,    , 
   . 

    IP- 

      ,   Internet,      
Internet-.         
 .  Internet-,  //"-, ,     
    . 
        .      
,    "" , ..  
   .      
.        
,   ,      . 
      . 
,        
  . ,   , 
     ,  ,  
-      ,  ,  
    .    
 ,      . 

    266	Visual J++:   

         Internet    
,            
""   .       
      IP-. 
    IP-   32- .  Java- IP- 
     .     ,   
 3.  ,   IP-      
   ,      ,  
  .   ,  : 

    192.176.7.45 

       : "      
     ".     . 
      Internet     
IP-    : 
     , !       ? 
     IP-:       ... , . 
       ? 
     ,   ! 
     -? 
    ,         
     Internet.    
    (    IP-),   
 .   ,      URL: 

    http://www.microsoft.com/visualj 

     "www.microsoft.com"        
  Microsoft.     URL: 

    http://198.105.232.30/visualj 

      "www.microsoft.com"     IP- "198.105.232.30". 

     InetAddress 

     Java   InetAddress,    java.net, 
  IP- Internet.     
,      InetAddress,  
    . 

     9.   Java	267 

      getByName()  InetAddress    
 InetAddress      IP-  : 

    InetAddress inetaddr = InetAddress.getByName ("www.microsoft.com"); 

     

    InetAddress inetaddr = InetAddress.getByName("198.105.232.30"); 

    (   IP-         
      ,   .)    
  (, - ,      
Internet-),     UnknownHostException. 
     getAllByName()   getByName().    
,   getAHByNameQ   IP-,    
 . ,    IP-     
   Internet , ,      
IP-.  getAHByName()   IP-,   
      .    , 
,   ,          InetAddress: 

    import java.net.*; 
    public class ShowAddresses { 
    public static void main(String[] astrArgs) { 
    //       // ,  
   Internet. for(int ii=0 ; iiostrArgs. length ; ii++) ( try { 
    InetAddress[] inetaddrs = 
    InetAddress . getAHByName (astrArgs [ii]) ; 
System.out.println(astrArgs[ii] + ":"); for(int jj=0 ; jj<inetaddrs.length ; jj++) 
    System.out.println(inetaddrsIjj]); } catch (UnknownHostException e) { 
    System.out.println("Error connecting to " + astrArgs[ii]); 
    } 
           "jview ShowAddresses 
www.microsoft.com java.sun.com www.javasoft.com"    : 
    www.microsoft.com: www.microsoft.com/198.105.232.6 
www.microsoft.com/198.105.232 . 7 www.microsoft.com/198.105.232.30 


    268 Visual J++:   


    www.microsoft.com/198.105.232.10 www.microsoft.com/207.68.137.7 
www.microsoft.com/207.68.137.8 www.microsoft.com/207.68.137 . 9 
www.microsoft.com/207.68.137 .34 www.microsoft.com/207.68.137.35 
www.microsoft.com/207.68.137.36 www.microsoft.com/207.68.137 .40 
www.microsoft.com/207.68.137.41 www.microsoft.com/207.68.137.42 
www.microsoft.com/207.68.137.43 www.microsoft.com/198.105.232.4 
www.microsoft.com/198.105.232.5 Java.sun.com: 
    Java.sun.com/206.26.48.100 www.j avasoft.com www.JavaSoft.com/206.26.48.100 

       ,   www.microsoft.com   
IP-,   java.sun.com  www.javasoft.com    IP-. 
     getAddressQ     ,  
 IP-,     InetAddress.   , 
   32- IP-      
 0,         3. 
     getHostNameQ       ,  
 IP-.   InetAddress    IP- 
,       .   InetAddress  
-  ,      ,   IP-, 
    InetAddress. 
         InetAddress    
111().1 getLocalHostQ     InetAddress, 
 IP- ,       
  . ,    Java    
 "cyrano.supercorp.com" (  ),  

    InetAddress inetlocal = InetAddress.getLocalHost(); String 
strLocalHostName = inetlocal.getHostName(); Systern.out.println(strLocalHostName); 

         : 

    cyrano.supercorp.com 


       

    IP-   ,  , .  
IP-    ,  , 


     9.   Java	269 


   Internet    .   
   . 9.1. 

    IP- 
     
     Internet  

    . 9.1. IP-     

          Internet,   
    .     
           . 
     Java (  
      COM/DCOM)     
    .  ,      
         
    (       )  
     ,   
   ! 
        (socket connection)   
  ,     
  Internet.  "" (, socket  "", "") 
      ,     
 ,         . 
,      .    Internet 
     .  ""   
        . ,  
  ,     .  ,  
   " ".  ""    
   Internet. 

    27 Visual J++:   


        

           , 
  Internet,    InetAddress,   
 IP-  .    ,   
  InetAddress.   InetAddress  ,  
    Socket       . 
      Socket   Java ,   
.    Socket   new.   
  Socket  InetAddress   ,    
,      .  
  ,    : 

    InetAddress inetaddr = InetAddress.getByName ("www.microsoft.com"); Socket 
sock = new Socket(inetaddr, 80); 

       ,       
      80    
"www.microsoft.com",  -  .  
         
   WWW-  Microsoft.    
,  ""  WWW  HTTP-, 
HTML-  -        
 Microsoft. 
     ,        
,       ,    . 
  Socket        
   . ,    ,  
    ,      
Socket   . ,      
 ,      ,   . 

        

       Socket ,       
      ,   
  .      
  ,    Socket.  
getOutputStream()  Socket     . 
        Internet , 
    . 
    ,        . 
   ,          
.               
        

     9.   Java	271 

.         Java.    
    Internet,      
API   . 

     - 

        Internet    . 
        ,   
   . ,     
  -.        
   ,    IP-,    
 .      , 
    ,     
Internet-  . 
        Java  -.   
      IP-,    
      . ,     
,   ,      - 
. ,     ,   ,  
   -  .  -   
      InetAddress  Socket. 
        Internet  -,    
. ,  Windows 95,  ,     
     .    .   
  - .       
    "www.microsoft.com"  "www.javasoft.com", 
 ,       ,    
   . 
       - Java: 

    import java.io.*    //   , import java.net.*   // 
    . 
    public class EchoClient { 
    public static final int ECHO_PORT = 7; 
    public static void main(String[] astrArgs) { 
    //        
    //  IP-. 
    
    if(0 == astrArgs.length) { 
    System.err.println("Usage Error: \n EchoClient \ 
    <machine-name>"); System.exit (-1); } 
    PrintStream out = null; 
    DatalnputStream socket_in = null; 
    DatalnputStream user_in = new DatalnputStream(System.in); 


    272	Visual J++:   


    try { 
    InetAddress addr = InetAddress.getByName(astrArgs[0]); 
    Socket sock = new Socket(addr, ECHO_PORT); 
    out = new PrintStream(sock.getOutputStream()); 
    socket_in = new DatalnputStream (sock.getlnputStream()); } catch 
(Exception e) { 
    System.err.println("Error:\n Connecting to remote \ port: " + e) ; 
    e.printstakTrace(System.err) ; 
    System.exit(-1); } 
    //       , //  
   .   //  "Quit",  
, try { 
    boolean fDone = false; while(!fDone) { 
    String str = user_in.readLine(); if("QUIT".equals(str)) { fDone = true; 
break; } 
    out.println(str); 
    String strResponse = socket_in.readLine(); System.out.println("From the 
socket: " +strResponse), } } catch (Exception e) { 
    System.err.println("Error:\n Problem with \ 
    communications: " + e); e.printStackTrace(System.err); System.exit(-1); } 
    if(null != out) 
    out.close(); if (null != socket_in) 
    socket_in.close(); user_in.close(); } catch (Exeption e) { 
    System.err.println("Error:\n Problem closing the 
    socket: " + e); 
    e.PrintStackTrace(System.err); System.exit(-1); 
    ) 
    } 


      

      -     ,   
.      .  
  ,   

     9.   Java	273 

Internet      28800 , ,    
Internet   ,      
 . 
         ,   ,   
 read()       ,  
      .  ,   
 ,    read(),  ,   
     Internet  .     
 . ,    10      
    readQ,       ,  
     (   )   
     . 
          ,    readQ 
    EOFException.     
 ,     EchoClient   
(    ): 

    //       , //   
 .    // "Quit",  , 
try { 
    boolean fDone = false; while(!fDone) ( 
    String str = user_in.readLine (); if("QUIT".equals(str)) { fDone = true; 
break; } 
    out.println(str); 
    String strResponse = socket_in.readLine (); 
    System.out.println("From the socket: " + 
    strResponse); } } catch (EOFException eEOF) { 
    System.out.println("Socket has been closed \ 
    from the other side"); fDone = true; } catch (Exception e) { 
    System.err.println("Error:\n Problem with \ 
    communications: " + e); e.printStackTrace(System.err); System.exit(-1); } 

             
   writeQ    ,   
     .       
writeQ     ,  


    274	Visual J++:   

       lOException.      
 EchoClient   try/catch,    socket_out.println(): 

    } catch (lOException eWrite) ( 
    System.out.println("Socket was closed from other side \ 
    during write()"); fDone = true; 


      n   

               
Internet   ,      
   .     
,     :  ,  
 ,       .   
,       .  
,         
        
  . 
    ,     ,   
           
.     ,    
.          .  
     12:00:01    12:00:02,   
 ,     12:00:01  
12:00:02.     12:00:02  12:00:01 ,  
    ,     . 
            
( TCP-).      
      (datagram).   
      .   
       ,    
,    ,         . 
     Internet         
,  ,       
      . TCP-   
,       .    
      ,   
 ,      .   
 ,       

     9.   Java	275 

       .  .  
     ,   
   ,      .   
      . 
    TCP-        ,   
   .     
   TCP-.     
      ,   TCP-. 
         Socket,    
     .   
 : 

    public Socket(InetAddress addr, int port, boolean stream); 

       stream  true,    
TCP-.    false,    . 

     Internet- 

         Java EchoClient , 
      Internet.     
    ,        
  -.    ,  
"" ,     .  
   ,  ,     , 
  .   -  
,     . 
         ,     , 
 ,    lOException.    
  ,   : 

    Error: 
    Connecting to remote port: Java.net.SocketException: connect 


     ServerSocket 

         Java   ServerSocket. 
 ServerSocket       .  
   ServerSocket    - 
  ,      _   
  . 
             
   ServerSocket.      :  
   , ,     
,     . 

    276	Visual J++:   

     ServerSocket    .    
       , 
 ServerSocket    ,      
  .     jrppTOB  Internet  
 .  - (  )   
,     .    
    ,      
  "", ..   ,     
  . 

     -  Java 

     -,    Java,    
.    -    
  ,      
,      .    
     ,  
   .       
  ,       
 . 
        -.     
  ,   EchoServer,   acceptQ  
ServerSocket,   ServerSocket,     
 .     EchoServer   
   -,      
 . -   ,    
       . 

    import java.net.*; import java.awt.*; import EchoClientHandler; 
    public class AchoServer extends Frame { private static int ECHO_PORT = 7; 
private static String m_strServerName; private static int m_cClients; private 
static EchoServer m_f = null; private static ServerSocket sock; 
    //  main()   EchoServer,  //   
     // . 
    public static void main(String[] astrArgs) { try { 
    sock = new ServerSocket(ECHO_PORT); m_cClients = 0; 
    m_strServerName = InetAddress.getLocalHost().getHostName(}; } catch 
(Exception e) { 
    System.err.println("Error:\n Problem creating the \ ServerSocket: " + e) ; 


     9.   Java	277 


    e.printStackTrace(System.err) System.exit (-1); 
    } 
    //  ,     //  . 
m_f = new EchoServer(); m_f.resize(300, 200); m_f.repaint(); 
    while(true) { try ( 
    Socket s = sock.accept(); 
    new EchoClientHandler(s); //   
    //  . m_cClients++; m_f.repaint(); } catch (Exception 
e) { 
    System.err.println("Error:\n Problem listening\ 
    for clients: " + e) ; e.printStackTrace(System.err); System.exit(-1); } 
    1 
    public static void decrementClientCount() { 
    m_cClients--; .[';>- '-/:;. m_f . repaint () ; 
    ">.. ,') '  .       . .    .- ...,.- .   ..     .    .; ::..  
:.-...:  '' .-.'    '  ' 
    public static void shutdown() { 
    System.out .println ("Shutting down..."); 
    System.exit(-1); } 
    public EchoServer() { 
    super("EchoServer Application"); >	. 
    public void paint(Graphics g) { 
    FontMetrics fm = g.getFontMetrics(); g.drawSreing("Server: " + 
m__strServerName + "\n" + "Clients: " + m_cClients, 10, 10); 
    '   ')      .         - -:   '         - .    .'   ' 
    //   WINDOW_DESTROY   //   shutdown(). 
public boolean handleEvent(Event evt) { if(evt.id == Event.WINDOW_DESTROY) ( shutdown(); 
    return true; //     , //   
 . 


    278 Visual J++:   


    } 
    return false; } } 

     EchoClientHandler  -,   
  ,       ,  
 ,     ,          ( . 
    -,    
    Server. 

    import java.net.*; import java.io.*; 
    public class EchoClientHandler implements Runnable { 
    //     ,  , private Socket 
m_sock = null; 
    //         // , 
     run() . public EchoClientHandler(Socket s) 
{ m_sock = s; 
    Thread t = new Thread(this); t.setDaemon(true); t.start (); } 
    public void run() { try { 
    DatalnputStream is = new 
    DatalnputStream(m_sock.getlnputStream()); PrintStream os = new 
PrintStream(m_sock.getOutputStream()); while(true) ( 
    String str = is.readLine(); os.println(str); } } catch (Exception e) { 
    System.err.println("Error:\n Problem in client, \ 
    killing: " + e) ; e.printStackTrace(System.err); } 
    EchoServer.decrementClientCount(); } } 


     9.   Java	279 


    URL   

        Internet-   
    : 
    1.         . 
    2.          , 
     ,      . 
    3.       . 
    4.        (   
 ),      2. 
    5.          ,  
    . 
        ,     
  ,      WWW.    
 . ,    HTML-  
  Visual J++  WWW-  Microsoft.    
        "visualj".   
   : 
    1.   -       
  ,   "www.microsoft.com".   
    80,     
HTTP-.   ,      : 

    public   Socket  makeHTTPConnection(String  strServerName)    { try   { 
    InetAddress addrServer =  InetAddress.getByName(strServerName); return   
new  Socket(addrServer,   80);      //        80 
    //   //  HTTP-. }   catch   (Exception  e)    { 
return  null; 
    } } 

    2.         , 
  ,      , 
-   : 

    GET /visualj/ HTTP/1.0 

       HTTP-  ,    "/visualj". 
,      , , 
  WWW-  ,    . 

    28	Visual J+v:   

    3.   WWW-  HTML- -   
.     ,    
,      ,     ,  
 (  ),     .  
  -    
,   "/visualj". 
    4.     HTTP-  ()   
  .   ,      
          
   .        
  , -    HTTP-  
WWW-,       ,  
   . 
    5.       yvisualj" (    
HTML-)    . 
      ,    , 
     .   , , 
FTP    . ,    1, 
    ,      
.  FTP-  Internet    21.  3, 4 
 5  . 
      2  FTP-   . 
        
.   HTTP-    
     (  ,  
,   ,  -  
  ),     "GET", "POST"  
"HEAD", ,     .  FTP- 
   . ,      
"/some/cool/resource.zip"  FTP- -   
 : 

    CD some CD cool GET resource.zip 

         ,  HTTP-. 
     ,    
   . 

    URL 

    ,         
WWW,  URL (Uniform Resource Locator   

     9.   Java	281 

      ).    URL    Visual J++: 

    http://www.microsoft.com/visualj 

     URL     .     
,     .     
"http".       .   
   ,    -.  
     .       
"www.microsoft.com".      , 
("/visualj").   URL  URI (Uniform Resource Identifier  
  ). URI    ,  
       . URI  
     , ..    
-   , a URI    ,   
 . 
     URL       
WWW.  URL,     ,   
HTTP- ("http://www.microsoft.com/visualj"), FTP-cep-, 
Gopher-  Internet-   .   
 URL ,       ,  
   . 
     Java    URL    URL.  
 ,    URL,   public- 
.  ,   URL,     Visual 
J++  WWW-  Microsoft,     
HTML- Visual J++.       ,  
   ,    ..  URL 
        . 
      ,    ,   
      ,   
 .   Java-,     
 Visual J++: 

    URL urlVJ = new URL("http://www.microsoft.com/visualj"); Object obj = urlVJ.getContent(); 


      

     ,   URL,    (content 
type),   ,    ,    
. ,   "image/gif' ,     
        GIF. 
        URL     
"file:".     , ,  

    282	Visual J++:   

  .       URI. 
 "file:"    .  ,   
  .      ,   
 ,   URL.getContentQ;   
     System.outPrintStream. 

    import java.io.*; import java.net.*; 
    public class PrintOutText { 
    //       , //   
.   , //   .  
 //  . 
    public static void main(String astrArgs[]) { if(0 == astrArgs.length) { 
    System.err.println("Usage Error:\n PrintOutFile <file path>"); 
System.exit(-1); } 
    Object obj = null; try f 
    URL url = new URL("file://" + astrArgs[0]); 
    obj = url.getContent(); 
    if (null == obj) | | (! (obj instanceof InputStream))) { 
    System.out.println("Content could be read: " + obj); System.exit(-1); } } 
catch (Exception e) { 
    System.err.println("Error:\n Unable to open file: " + e); 
e.printStackTrace(System.err); System.exit(-1); } 
    //         System.out. DatalnputStream 
is = new DatalnputStream((InputStream)obj); try { 
    String str = ""; do { 
    System.out.println(str); str  =   is.readLine(); 
    }   while((null   !=   str)    &&    (str.length()    !=   0)); 
is.close(); }   catch(lOException  ioe)    { 
    System.err.println("Error:\n  Reading  from  file:   "   +  ioe); 
ioe.printStackTrace(System.err); System.exit(-1); } ) ) 


     9.   Java	283 

        ,      
      ,    URL. 
  PrintOutText,      
,   URL,      .  , 
  getContentQ,    ,   
,    ,     .   
,  ,  ,  URL,     
  ,       GIF 
     AU.     getContent() 
     Image  AudioClip,    
  . 

    import java.io.*; import java.net.*; 
    public class PrintOutURL { 
    //       , //   . 
    public static void main(String astrArgs[]) { if(0 == astrArgs.length) { 
    System.err.println("Usage Error:\n PrintOutURL <URL>"); System.exit(-1); } 
    Object obj = null; try { 
    URL url = new_URL(astrArgs[0]);        : 'obj = url. getContent (} ; -  : 
       if (null ==" obj)"""! | 
    (! (obj instanceof InputStream))) ( System.out.println("Content could be 
read \ 
    as text: " + obj); System.exit(-1); } } catch (Exception e) { 
    System.err.println("Error:\n Unable to open file: " + e); 
e.prontStackTrace(System.err); System.exit(-1); } 
    //          
    // System.out. 
    DatalnputStream is = new DatalnputStream((InputStream)obj); 
    try { 
    String str = ""; do { 
    System.out.println(str); str = is.readLine(); } while((null != str) && 
(str.length () != 0)); 


    284 Visual J++:   


    is.close (); }   catch(lOException  ioe)    { 
    System.err.println("Error:\n Reading from file:   "  +  ioe) 
ioe.printStackTrace(System.err); System.exit(-1); } ) } 


       URL 

       ,        
getContentQ  URL      ,  
   ?   URL  ,    
    ,   URL,     . 
     getURLConnectionQ  URL   URLConnection  
,   URL. URL-  , 
  Socket,    .  
URL-,    -,   
Internet-.    ,    ,   
getURLConnectionQ   URLConnection,   
       .   
     ,     . 
         ,   Java,  
 URL-. ,     , 
   ,      .   
   ,        
java.net,   URL-  1~   
     . 
        URL-     Internet-: 

    URL url = new URL(strResource); URLConnection conn = 
url.getURLConnection(); corTnTconhect () ; InputStream is = conn.getlnputStream(); 
    //   . is.close(); 

        .  URL- 
   URL,        
  URLConnection.connectQ.     

     9.   Java	285 

   ,    
URL-,        
    . ,  Internet-  
   .      
FTP.     ,     
allowUserlnteractionQ  URLConnection.     
     URL- ( 
 URLConnection.ge-tOutputStreamO). , HTTP- "POST" 
,         , 
  HTML-,   -  . ,  
      WWW.     , 
       HTTP-. 
    1	   WWW-,       
 "POST".   URI  ,   
      .     
 URI,    . 
    ,   ,     
   .      
     URL-,    
   URLConnection.getOutputStream().    
   setOutputStreamQ  URLConnection,  
 URL-,       
   .    ,   
          URL-: 

    public InputStream getPostedData!nputStream(URL url, byte[] ab, 
    int offset, int length) throws lOException { , //   
lOException ,  //    . 
    //       ab. 
    URLConnection conn = url.openConnection (); 
    conn.setDoOutput(true); 
    OutputStream os = conn.getOutputStream(); 
    os.write(ab, offset, length); 
    os.close () ; 
    //   allowUserlnteraction  ,  II   
OutputStream   InputStream  // URLConnection. return conn.getlnputStream(); 
    }


       

       ,     Internet  
 , ,         
 .       

    286	Visual J++:   

      "<type>/<subtype>". ,   GIF  
  "image/gif',     
"audio/an", HTML-  "text/html",    
   "text/plain".  ,  ,    
,    "application/octet-stream",    
    . 
      URLConnection   ,     
getContentTypeQ     .     
 ,       URLConnection. 
            
   ,    .   
   ,       
.       : 

    property-type: property value 

        ,     
 URLConnection,   ,     . 

     

      

    getURL()	 URL,      URL-. 
         URLConnection. 
    getContentLength()           ( )  
.      ,     
URLConnection.connect()  ,     
.  HTTP-   ,   
FTP-  . 
    getContentType()	   "<type>/<subtype>",  
   .      HTTP-  FTP-.  
      
"application/octet-stream",       
   "text/plain". 
    getContentEncodingO      ,   
,   . ,      zip-. 
    getExpiration()	   Internet  " 
",   ,      . 
,        24 ,  
-      6 . 
    getDate()	   . 
    getLastModified()	       
        .    
    ______________   ,     . 

     9.   Java 287 


    	  

    getHeaderField()	   .    
 ,     ,     
 n-   . 
    getHeaderFieldInt()        ,     
 , ..      " : 
".   ,     
   . 
    getHeaderFieldDate()      ,     
.      ,    
    .    Date. 
    getHeaderfieldKeyO           - ,   
.  ,   ,   . 


     

     "" Internet  TCP/IP.    Internet 
 ""       TCP/IP.   
     ,    
 ,       . 
 InetAddress   Java IP-.      
 Internet-.         . 
         Internet-   ,  
     -,  ( 
"")    .     
HTTP-  FTP-.    Server-Socket,   
  .       
   ,    acceptQ     
   . 
    URL     ""   Internet. 
    ,    ,  
  .       
HTML-      . URL   
,   (IP-)  URI, * ,   
 . URI    ,     
 ,      Windows-, 
   . 
     URL   Java  URL.   ,  
  getContentsQ   ,   
 URL.  ,    getlnputStreamQ  
      . 
     ( ,  
HTTP,  )      ,   
getOutputStreamQ  URL. 

    288	Visual J++:   

