Java socket 訪問網頁

來源:互聯網
上載者:User

標籤:

     原來學過socket編程,因為最近我也在做自己的伺服器,但是沒有加任何安全機制,我就想到了這個伺服器可以承受的最大並發量是多少,當然這是和伺服器的記憶體有關的。

   如:www.lipyi.com這個網址,同一時間有千萬的使用者訪問(也可能是同一使用者),那麼伺服器是不是要崩掉了?這時候我考慮到很多網址後面都有時間戳記,原來就是避免這個問題的。另外也看到有網友說的可以過濾掉同一IP,這也是一個不錯的辦法,但是感覺對於很多小型的網站來說,時間戳記和過濾IP是沒有做的。

  下面是我用java寫的訪問CSDN中我的個人首頁,當運行這個程式時,我的首頁確實打不開了。累感不愛。

      

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.net.InetAddress;import java.net.Socket;public class TestServer{public final static String HOST = "blog.csdn.net";public final static String PATH = "/yilip";public final static int PORT = 80;public final static int PEOPLE_NUM = 10*10000;public static int num = 0;/** * Author: Lip */public static void main(String[] args){for (int i = 0; i < PEOPLE_NUM; i++){System.out.println("有多少人:" + num);ClientThread thread = new ClientThread(false);thread.start();}}}class ClientThread extends Thread{Socket socket = null;BufferedReader br = null;PrintWriter pw = null;boolean isout;//是否輸出返回結果public ClientThread(boolean isout){this.isout = isout;}public void run(){TestServer.num++;try{this.sleep(10*1000);socket = new Socket(InetAddress.getByName(TestServer.HOST),TestServer.PORT);// 讀取伺服器資料 br = new BufferedReader(new InputStreamReader( socket.getInputStream(),"utf-8"));pw = new PrintWriter((new OutputStreamWriter(socket.getOutputStream())));StringBuffer requestHeader = new StringBuffer();requestHeader.append("GET " + TestServer.PATH+ " HTTP/1.1\n").append("HOST:" + TestServer.HOST + "\n").append("Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\n").append("Accept-Encoding:gzip, deflate, sdch\n").append("Accept-Language:zh-CN,zh;q=0.8\n").append("Cache-Control:no-cache\n").append("User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36\n").append("Encoding:UTF-8\n").append("Connection:keep-alive" + "\n").append("\n");if (isout)System.out.println(requestHeader.toString());pw.println(requestHeader);//pw.flush();String line = "";if(isout) while((line=br.readLine())!=null) {   System.out.println(line); }System.out.println("Get the data from csdn......");} catch (Exception e){e.printStackTrace();} finally{try{System.out.println("close......");// br.close();pw.close();socket.close();} catch (IOException e){e.printStackTrace();}}}}
  遇到的一個問題,就是返回結果總是亂碼,我在這裡
 br = new BufferedReader(new InputStreamReader( socket.getInputStream(),"utf-8")) 設定了多種編碼都還是亂碼,但是我查看瀏覽器中編碼是UTF-8,不知道這是為什嗎?

  沒事運行這個程式,給別的網站搞點壓力,還是不錯滴。。。。

Java socket 訪問網頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.