java Socket Tcp 瀏覽器和伺服器(二)

劍握在手發表於2013-11-26

package cn.itcast.net.p2.ie_server;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class MyBrowser {

 /**
  * @param args
  * @throws IOException
  * @throws UnknownHostException
  */
 public static void main(String[] args) throws UnknownHostException, IOException {

  Socket s = new Socket("192.168.1.100",8080);
  
  //模擬瀏覽器,給tomcat服務端傳送符合http協議的請求訊息。
  PrintWriter out = new PrintWriter(s.getOutputStream(),true);
  out.println("GET /myweb/1.html HTTP/1.1");
  out.println("Accept: */*");
  out.println("Host: 192.168.1.100:8080");
  out.println("Connection: close");
  out.println();
  out.println();
  
  
  InputStream in = s.getInputStream();
  
  byte[] buf = new byte[1024];
  int len = in.read(buf);
  
  String str =new String(buf,0,len);
  System.out.println(str);
  
  s.close();
  
  //http://192.168.1.100:8080/myweb/1.html
 }

}

 

 

 

 

package cn.itcast.net.p2.ie_server;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class MyTomcat {

 /**
  * @param args
  * @throws IOException
  */
 public static void main(String[] args) throws IOException {

  ServerSocket ss = new ServerSocket(9090);
  
  Socket s = ss.accept();
  System.out.println(s.getInetAddress().getHostAddress()+".....connected");
  
  InputStream in = s.getInputStream();
  
  byte[] buf = new byte[1024];
  
  int len = in.read(buf);
  String text = new String(buf,0,len);
  System.out.println(text);
  
  
  //給客戶端一個反饋資訊。
  PrintWriter out = new PrintWriter(s.getOutputStream(),true);
  
  out.println("<font color='red' size='7'>歡迎光臨</font>");
  
  s.close();
  ss.close();
 }

}

 

 

 

 

package cn.itcast.net.p2.ie_server;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class URLDemo {

 /**
  * @param args
  * @throws IOException
  */
 public static void main(String[] args) throws IOException {

  String str_url = "http://192.168.1.100:8080/myweb/1.html";
  
  URL url = new URL(str_url);
  
//  System.out.println("getProtocol:"+url.getProtocol());
//  System.out.println("getHost:"+url.getHost());
//  System.out.println("getPort:"+url.getPort());
//  System.out.println("getFile:"+url.getFile());
//  System.out.println("getPath:"+url.getPath());
//  System.out.println("getQuery:"+url.getQuery());
  
//  InputStream in = url.openStream();
  
  //獲取url物件的Url聯結器物件。將連線封裝成了物件:java中內建的可以解析的具體協議的物件+socket.
  URLConnection conn = url.openConnection();
  
//  String value = conn.getHeaderField("Content-Type");
//  System.out.println(value);
  
//  System.out.println(conn);
  //sun.net.www.protocol.http.HttpURLConnection:http://192.168.1.100:8080/myweb/1.html
  
  InputStream in = conn.getInputStream();
  
  byte[] buf = new byte[1024];
  int len = in.read(buf);
  
  String text = new String(buf,0,len);
  
  System.out.println(text);
  
  in.close();
  
 }

}

 

 

 

 

package cn.itcast.net.p3.iegui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.InputStream;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class MyBrowseGUI extends javax.swing.JFrame {
 private JTextField url_text;
 private JButton goto_but;
 private JScrollPane jScrollPane1;
 private JTextArea page_content;

 /**
 * Auto-generated main method to display this JFrame
 */
 public static void main(String[] args) {
  SwingUtilities.invokeLater(new Runnable() {
   public void run() {
    MyBrowseGUI inst = new MyBrowseGUI();
    inst.setLocationRelativeTo(null);
    inst.setVisible(true);
   }
  });
 }
 
 public MyBrowseGUI() {
  super();
  initGUI();
 }
 
 private void initGUI() {
  try {
   setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   getContentPane().setLayout(null);
   {
    url_text = new JTextField();
    getContentPane().add(url_text);
    url_text.setBounds(12, 36, 531, 44);
    url_text.addKeyListener(new KeyAdapter() {
     public void keyPressed(KeyEvent evt) {
      url_textKeyPressed(evt);
     }
    });
   }
   {
    goto_but = new JButton();
    getContentPane().add(goto_but);
    goto_but.setText("\u8f6c \u5230");
    goto_but.setBounds(555, 36, 134, 44);
    goto_but.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent evt) {
      goto_butActionPerformed(evt);
     }
    });
   }
   {
    jScrollPane1 = new JScrollPane();
    getContentPane().add(jScrollPane1);
    jScrollPane1.setBounds(12, 92, 676, 414);
    {
     page_content = new JTextArea();
     jScrollPane1.setViewportView(page_content);
    }
   }
   pack();
   this.setSize(708, 545);
  } catch (Exception e) {
      //add your error handling code here
   e.printStackTrace();
  }
 }
 
 private void goto_butActionPerformed(ActionEvent evt) {
  showPage();
 }
 
 private void url_textKeyPressed(KeyEvent evt) {
  if(evt.getKeyCode()==KeyEvent.VK_ENTER)
   showPage();
  
 }

 private void showPage() {
  try {
   
   String url_str = url_text.getText();
   URL url = new URL(url_str);
   
   InputStream in = url.openConnection().getInputStream();//url.openStream();
   
   page_content.setText("");
   
   byte[] buf = new byte[1024];
   int len = in.read(buf);
   String text = new String(buf,0,len,"utf-8");
   
   page_content.setText(text);
   
   in.close();
   
   
  } catch (Exception e) {
   // TODO: handle exception
  }
 }

}

相關文章