Create a Web service based on a socket

Source: Internet
Author: User

Create a Web service based on a socket

Why to use the socket , do let's see .

SOCKET schematic review:

-------------------Write socketservice to complete the lowercase to uppercase function-----------------------------

ServerSocket server-side code is as follows:

 Public Static voidMain (string[] args)throwsIOException {//1: Establish the server-side TCP socket service, must listen to a portServerSocket SS =NewServerSocket (9999); //2: Gets the client object on the connection through the Accept method of the server-side socket objectSocket s =NULL; //3: Get the data for the client         while(true) {            //Accept the socket service, if there is, no then jam, waits =ss.accept (); System.out.println ("Accept success ..."); //gets the output stream sent by the client from the Socekt input stream .InputStream in =S.getinputstream (); byte[] buf =New byte[1024]; intLen =In.read (BUF); String Str=NewString (buf, 0, Len); System.out.println ("The data sent from the client is as follows:");            System.out.println (str); //through the server-side socket output stream, the write data is transmitted to the client socket input streamOutputStream out =S.getoutputstream (); //Convert letters to uppercaseOut.write (Str.touppercase (). GetBytes ());        S.close (); }    }

Accessing the Socketservice service through a Java client

 Public Static voidMain (string[] args)throwsException {Scanner input=NewScanner (system.in); //1: Create a TCP protocol-based socket service that specifies the connection server and port number when establishing an objectSocket s=NewSocket ("127.0.0.1", 9999); //2: Get the output stream in the socket by establishing the socket object, call the Getoutstream methodOutputStream out=S.getoutputstream (); System.out.println ("Please enter the letters you want to convert, or the words!"); //3: Writing to the socket output streamString word=Input.next ();        Out.write (Word.getbytes ()); //4: Gets the input stream from the socket through the established socket object, the input stream will accept the data from the server sideInputStream in=S.getinputstream (); byte[] buf=New byte[1024]; //5: Get the input byte stream data, note that this method is blocked, if not get the data will always wait        intlen=In.read (BUF); String Str=NewString (buf,0, Len); System.out.println ("The result of the service return is as follows:");        System.out.println (str); //Close SocketS.close (); }    }

Note Start the server side before starting the client: Otherwise a connection exception will occur

The server-side display results are as follows:

Accept success ..... The data sent from the client is as follows: Hello

The client side displays the following results:

Please enter the letters you want to convert, or the words! the results of the Hello service return are as follows: Hello

Access to Socketservice via IE

Service side:

/wwwww http/1.1host:localhost:9999User-agent:mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:46.0) gecko/20100101 firefox/46.0accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/ *; Q=0.8accept-language:zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3accept-encoding:gzip, DeflateCookie: _ga= Ga1.1.1127292809.1501852337connection:keep-alivecache-control:max-age=0

Client:

HTTP protocol format Simple review:

The HTTP request consists of three parts: the request line, the message header, the request body

Request Line:

Start with a method symbol, separated by a space, followed by the requested URI and version of the Protocol, in the following format: Method Request-uri http-version

Message header:

The Accept:accept request header field is used to specify what types of information the client accepts

Host: (When sending a request, the header domain is required) The request header domain is used primarily to specify the Internet host and port number of the requested resource, which is usually extracted from the HTTP URL by default 80 omitted

Content-type the media type referenced in the header domain, the corresponding decoding mechanism must be used
Content-type:text/html;charset=utf-8

The HTTP response is also made up of three parts: status line, message header, response body

Http-version Status-code Reason-phrase

Where http-version represents the version of the server HTTP protocol, Status-code represents the response status code sent back by the server, and Reason-phrase represents a textual description of the status code.

http/1.1200 OK

Problem thinking

We are currently creating a socket server with the Java language, and there is no problem with Java socket access. There is no problem with the C # socket client access (stating that programs written in different languages can be accessed through the socket) with IE access to the socket service side also no problem, ie itself is developed with VC + + language

But if our service is a little more complicated. For example, my past words Operation 1: lowercase to uppercase, 2: Uppercase to lowercase 3: English to Chinese ... And we found out that the Internet Explorer not only transmits the data but also transmits the protocol when the request is sent. And many of our applications are based on web development at the moment. If we process the Web request. Then you need to get the data from the protocol that was sent. So the best solution is to specify the format, the client unified format to send, as long as the format is unified, the server side from the specified format to obtain data is OK

Create a Web service based on a socket

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.