First, Understand Java Socket. Java defines two very important classes: socketserver and socket. Their respective purposes can be seen from their names. Socketserver is used as the serverProgram, Socket is used to connect the client program of the server. Note that these two classes differ only when the connection is initial. Once the connection is established, the server and client are two equivalent entities, both of which can be read and written.
All read operations are completed by the inputstream class, And all write operations are completed by the outputstream class. However, these two classes are relatively low-level, and they are only responsible for the transmission of byte streams. Therefore, they are rarely directly used to read and write data.
The input stream has two common classes: datainputstream and bufferedreader. Datainputstream is used to read binary data from a socket stream, such as a 16-bit or 32-bit integer. Bufferedreader reads data by row.
The output stream also has two common classes: dataoutputstream and printwriter, which correspond to the two classes of the input stream respectively. One is to write binary data, and the other is to write data by row.
The following describes the HTTP protocol (Hypertext Transfer Protocol ). First, you need to understand that the HTTP packet is divided into two parts: header and body, which are separated by a blank line. There are several request methods, such as get and post, which will not be described in detail. You can read related books or directly read RFC for details.
This is the basic knowledge. You can do some programming work below.
The following is a very simple Web server. No matter what the client request is, it returns only one very simple HTML page.
Import java.net. *; import Java. io. *;/*** a very simple web server. * @ author zhhailon */public class miniwebserver {protected void start () {serversocket s; system. out. println ("miniwebserver starting up on port 8080"); system. out. println ("(press Ctrl-C to exit)"); try {// create a server socket S = new serversocket (8080);} catch (exception e) {system. out. println ("error:" + E); return;} system. out. prin TLN ("waiting for connection"); For (;) {try {// wait for the connection socket remote = S. accept (); // accept () returns a connected socket, that is, the client system. out. print ("connection, sending data. "); // do you still remember bufferedreader? Read bufferedreader in = new bufferedreader (New inputstreamreader (remote. getinputstream (); // do you still remember printwriter? Write printwriter out = new printwriter (remote. getoutputstream (); // read data, we will ignore the read content until we read an empty line // remember the HTTP Header It ends with an empty line. We don't care about its content string STR = "."; while (! Str. equals ("") {STR = in. readline ();} // send a response // send an HTTP header out. println ("HTTP/1.0 200 OK"); out. println ("Content-Type: text/html"); out. println ("server: simplewebserver"); // do not forget an empty line out. println (""); // sends the HTML page out. println ("OK, I'm the uugly page .. welcome .. "); // flush () Ensure that all data is transferred out. flush (); // close the remote connection. close ();} catch (exception e) {system. out. println ("error:" + E) ;}}/ ***** @ Param ARGs */public static void main (string [] ARGs) {miniwebserver Ws = new miniwebserver (); ws. start ();}}
Because port 80 of my computer is occupied, I chose to listen to port 8080. Next, javac, Java, and try our mini web server. Open the browser and enter http: // localhost: 8080/. OK, I'm the uugly page... welcome ..
Abve all, which briefly introduces the socket usage in Java and practices a small Web server. I always believe in practice, so more coding is required!