Java socket access webpage, using ocket

Source: Internet
Author: User

Java socket access webpage, using ocket

I have learned socket programming before, because I have been working on my own server recently, but I didn't add any security mechanisms, So I thought about the maximum concurrency that this server can afford, of course, this is related to the server memory.

For example, if there are tens of millions of users accessing the website www.lipyi.com at the same time (or even the same user), will the server crash? At this time, I considered that many websites are followed by timestamps, so this problem was avoided. In addition, I also saw some netizens saying that the same IP address can be filtered out. This is also a good method, but I feel that for many small websites, the timestamp and IP address filtering are not done.

The following is my access to my personal homepage in CSDN written in java. When I run this program, my homepage cannot be opened. Tired.

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 I Nt 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 ("Number of persons:" + num); ClientThread thread = new ClientThread (false); thread. start () ;}} class ClientThread extends Thread {Socket socket = null; BufferedReader br = null; PrintWriter pw = null; boolean isout; // whether to output the returned result public ClientThread (boolean I Sout) {this. isout = isout;} public void run () {TestServer. num ++; try {this. sleep (10*1000); socket = new Socket (InetAddress. getByName (TestServer. HOST), TestServer. PORT); // read server data 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 ();}}}}
One problem is that the returned results are always garbled. I am here
Br = new BufferedReader (new InputStreamReader (socket. getInputStream (), "UTF-8") sets a variety of encoding are still garbled, but I see the browser encoding is UTF-8, do not know why?

It's okay to run this program and put some pressure on other websites ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.