Java by API/javax.net/InetAddress — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:35, 31 мая 2010

InetAddress.getByName(String name)

/*
 * Output:
 * 
 * 
HTTP/1.1 400 Bad Request
Content-Type: text/html
Date: Mon, 24 Apr 2006 23:11:15 GMT
Connection: close
Content-Length: 39
<h1>Bad Request (Invalid Hostname)</h1>
 */
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class MainClass {
  public static void main(String args[]) throws Exception {
    byte buff[] = new byte[1024];
    InetAddress addr = InetAddress.getByName("www.jexp.ru");
    Socket s = new Socket(addr,80);
    OutputStream output = s.getOutputStream();
    InputStream input = s.getInputStream();
    String GetCmd = "GET /index.html HTTP/1.0\r\n\r\n";
    GetCmd.getBytes(0,GetCmd.length(),buff,0);
    output.write(buff);
    input.read(buff,0,buff.length);
    System.out.println(new String(buff,0));
  }
    
}





InetAddress.getLocalHost()

/*
 * Output:
 * 
 * 
 * name/132.81.162.18
 */

import java.net.InetAddress;
public class MainClass {
  public static void main(String args[]) throws Exception {
    InetAddress myAddress = InetAddress.getLocalHost();
    System.out.println(myAddress);
  }
}