Java by API/javax.net/Socket

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

new Socket(String addr,int port)

   <source lang="java">

/*

* 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

Bad Request (Invalid Hostname)

*/

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));
 }
   

}

      </source>
   
  
 
  



Socket: getInputStream()

   <source lang="java">

/*

* 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

Bad Request (Invalid Hostname)

*/

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));
 }
   

}

      </source>
   
  
 
  



Socket: getOutputStream()

   <source lang="java">

/*

* 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

Bad Request (Invalid Hostname)

*/

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));
 }
   

}

      </source>