"Push" technology in Java

zhaozj2021-02-16  35

"Push" technology in Java

Bromon original copyright

In a general B / S program (most typical web service), however, how to send a client (server) to the server (Server), the server responds to this request and send back the client requests. In this process, the server side plays a role of a passive response. In general, the server side does not actively write data to the client. Since HTTP is an unconnected protocol, that is, when the server is completed, the connection between the two parties is disconnected until the next client issues a request to re-establish the connection. Such HTTP protocols are saving network resources, but it also brings us some troubles, such as the client can't find data changes in server-side data, and cannot make a timely response. So when we do some programs that need clients and server-side (such as chat room, web short message), we have to refresh the page without stopping the page, so as to obtain the latest data, doing the added server burden. If the server actively write data to the client when the data changes, then the problem is solved, this is the so-called "push" technology. In Java, the client actively applies for data to the server, called "pull"; the server actively wrote data to the client, called "push". We can open a port on the server side, listen to the client's connection, you can write data to the client, this is the principle of our C / S program, "push" technology uses the same principle, but the client is a browser Moreover, as long as we write data to the client according to the format that can be identified by the browser, the client can establish a connection with the server via HTTP and obtain data. HTTP is a very large agreement, but we only need to use a small part. The most important thing is to know what format written data, the browser knows. When we browse a page of a website, when we are www.sohu.com/page.htm, we send such a message to the server: get /page.htm http / 1.0

From: someuser@somehost.com

User-agent: mozilla / 4.0 (compatible; msie 5.0; windows NT 5.0; DIGEXT)

The first line of the blank line is what we have to pay attention to, it is separated from spaces into three parts, one is the method of the request (GET), the second is the requested page, the third is the version of HTTP. If the request is correct, the server will return the following information: http / 1.0 200 ok

Date: Thu, 8 Oct 2002 14:23:11 GMT

Content-Type: Text / HTML

Content-Length: 1644

Hello World! (other content) ... The first line represents the connection results, then the property of the return data, starting with It is the data returned to the browser. In our own procedure, you only need to write data according to the above format, you can implement your own simple Web server. Here is an example of Hello, World:

//myServer.java import java.io. *; import java.net *;. public class myServer {static ServerSocket server = null; static OutputStreamWriter ow = null; public static void main (String args []) {int port = 800 Try {server = new serversocket (port);} catch (exception e) {system.out.println (e);} while {Try {socket socket = server.accept (); OW = New OutputStreamWriter (socket) .getOutputStream ()); OW.WRITE ("HTTP / 1.0 200 OK"); OW.WRITE ("Content-Type: Text / Html); OW.WRITE (" "); OW. Write ("Hello, World"); OW.WRITE (" "); OW.FLUSH (); Socket.close ();} catch (exception e) {system.out.println e);}}}} Compile: Javac MyServer.java Run: Java MyServer then open the browser, enter http: // localhost: 800, will return a "Hello, World" is a simple implementation of "push" technology To expand it, such as adding multi-threaded responses, you can implement no refresh chat room, please readers yourself.

Original published in "Procedure Spring and Autumn" 0301

转载请注明原文地址:https://www.9cbs.com/read-21740.html

New Post(0)