A modified java socket communication program suitable for beginners

Source: Internet
Author: User

The following program describes how to simulate a browser to send a request to the server and output the server's return information on the console. This is what I wrote after reading what others have done, it is suitable for beginners to read java socket programs. There is also a drawback, that when connected to the Real Server, because the speed is too slow, there is no result. I don't talk much about it. There are many online programs, but they are similar. Package smallTest; import java. io. bufferedReader; import java. io. inputStream; import java. io. inputStreamReader; import java. io. outputStream; import java. io. printWriter; import java.net. socket; public class ClientDemo {/*** @ param args */public static void main (String [] args) {try {Socket s = new Socket ("127.0.0.1 ", 80); InputStream ins = s. getInputStream (); OutputStream out = s. getOutputStream (); PrintWriter pw = new PrintWriter (out, true); StringBuffer sb = new StringBuffer (); sb. append ("GET/test.htm HTTP/1.1 \ r \ n"); sb. append ("Accept: */* \ r \ n"); sb. append ("Accept-Language: zh-cn \ r \ n"); sb. append ("Host: localhost: 80 \ r \ n"); sb. append ("Connection: Keep-Alive \ r \ n"); // note, this is the key. Here, a carriage return line is required, indicating that the message header is complete, otherwise, the server will wait for sb. append ("\ r \ n"); pw. println (sb. toString (); pw. flush (); String temp; int contentLength = 200; BufferedReader br1 = new BufferedReader (new InputStreamReader (ins); for (int I = 0; I <6; I ++) do {temp = br1.readLine (); System. out. println (temp); if (temp. startsWith ("Content-Length") {contentLength = Integer. parseInt (temp. split (":") [1]. trim () ;}}while (br1.readLine () = "\ r \ n"); for (int I = 0; I <contentLength; I ++) do {temp = br1.readLine (); if (temp = null) break; System. out. println (temp);} while (br1.readLine () = "\ r \ n"); pw. close (); s. close ();} catch (Exception e) {e. printStackTrace ();}}} 

 

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.