Java Network Programming (client and server-side principles)

Source: Internet
Author: User

The browser side enters its own host's IP address + port number (8888), will see the data returned by the server, the Eclipse console will print the information received,

With our custom server, you can see the format of the request information that the browser sends to the server:

Request line ....
The get/http/1.1 request method/myweb/1.html The requested resource path for the HTTP protocol version.
The request message header. Property Name: Property value

The output of the Eclipse console:

192.168.2.103....connected//This is the output of my own program.
get/http/1.1//Request mode, compliance with HTTP Protocol, browser access according to specified protocol, send message to server side
host:192.168.2.103:8888
Connection:keep-alive
Cache-control:max-age=0
accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-insecure-requests:1
user-agent:mozilla/5.0 (Windows NT 6.1) applewebkit/537.36 (khtml, like Gecko) chrome/49.0.2623.108 safari/537.36// Supported browsers and their versions
Accept-encoding:gzip, deflate, sdch//browser-side supported file compression method
accept-language:zh-cn,zh;q=0.8//supported languages on the browser side

Blank line This empty line must have to be used to distinguish the request header from the request body
Request Body ...
Specific implementation code:

1PackageWebprogramingdemo;23ImportJava.io.IOException;4ImportJava.io.InputStream;5ImportJava.io.PrintWriter;6ImportJava.net.ServerSocket;7ImportJava.net.Socket;89PublicClassMytomcat {1011/**12*@paramArgs13*@throwsIOException14*/15//All content sent by the browser is printed16PublicStaticvoid Main (string[] args)ThrowsIOException {17ServerSocket SS =New ServerSocket (8888);Socket s =Ss.accept ();20System.out.println (S.getinetaddress (). Gethostaddress ()+ ".... Connected");InputStream in =S.getinputstream ();23byte[] buf =Newbyte[1024];24int len = In.read (BUF); 25 System.out.println (new String (buf, 026 // give the client a feedback message 27 PrintWriter out = new PrintWriter (S.getoutputstream (), true); 28 out.println ("Welcome to Mytomcat Server" 29  S.close ();  Ss.close (); }32 33}   

Java Network Programming (client and server-side principles)

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.