Java Socket Access Web page

Source: Internet
Author: User

originally learned socket programming, because recently I also do my own server, but without any security mechanism, I think of this server can withstand the maximum amount of concurrency, of course, this is related to the memory of the server.

such as: www.lipyi.com this URL, at the same time there are tens of thousands of users access (or perhaps the same user), then the server is not going to collapse? This is when I consider a lot of URLs behind the time stamp, the original is to avoid this problem. Also see a netizen said can filter out the same IP, this is also a good way, but feel for many small sites, time stamp and filter IP is not done.

The following is my home page in Java that I wrote to access Csdn, and when I run this program, my homepage does not open. Tired feeling does not love.

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";p ublic final static int PORT = 80;public final static int people_num = 10*10000 ;p ublic static int num = 0;/** * author:lip */public static void Main (string[] args) {for (int i = 0; i < people_num; I + +) {System.out.println ("How many people:" + num); Clientthread thread = new Clientthread (false); Thread.Start ();}}} Class Clientthread extends Thread{socket Socket = null; BufferedReader br = null; PrintWriter pw = Null;boolean isout;//Whether the output returns the result 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);//Read server data BR = new BufferedReader (New InputStreamReader (Socket.getinputstream (), "Utf-8"));p w = 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 ();p w.close (); Socket.close ();} catch (IOException e) {e.printstacktrace ();}}}}
Encountered a problem is that the return result is always garbled, I am here
br = new BufferedReader (New InputStreamReader (Socket.getinputstream (), "Utf-8") set a variety of encoding is still garbled, but I see the browser encoding is UTF-8, Don't know why this is?
  Nothing to run this program, to other sites to make some pressure, or good drop ....

Java Socket Access Web page

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.