This chapter mainly introduces an important module of Tomcat connector, but only a reduced version
The role of connector is to create an instance of ServerSocket ServerSocket, a bound port, a listening port, and when accepted to a client request, creates a Httpprocessor instance processor, Use ServerSocket as a parameter to Httpprocessor instance processor.process (ServerSocket) to process the request
import java.io.ioexception;import java.net.inetaddress;import java.net.serversocket;import java.net.Socket; Public classHttpConnector implements Runnable {Boolean stopped; PrivateString scheme ="http"; PublicString Getscheme () {returnscheme; } Public voidrun () {ServerSocket ServerSocket=NULL; intPort =8080; Try{ServerSocket=NewServerSocket (Port,1, Inetaddress.getbyname ("127.0.0.1")); } Catch(IOException e) {e.printstacktrace (); System.exit (1); } while(!stopped) { //Accept the next incoming connection from the server socketSocket socket =NULL; Try{Socket=serversocket.accept (); } Catch(Exception e) {Continue; } //Hand This socket off to an httpprocessorHttpprocessor processor =NewHttpprocessor ( This); Processor.process (socket); } } Public voidstart () {Thread thread=NewThread ( This); Thread.Start (); }}
How Tomcat work Porter Charpter 3:connector