This instance is written based on the traditional serversocket and socket, So it uses the traditional bio (blocking Io). During concurrency, it is processed by blocking, this instance corresponds to the Mina 2.0 instance -- timeserver, which uses a single thread to provide concurrent access through a loop to better reflect the difference between bio and NiO. The Code is as follows:
Import Java. io. datainputstream; import Java. io. ioexception; import Java. io. printstream; import java.net. serversocket; import java.net. socket; import Java. text. simpledateformat; public class sockettimeserver {public static void main (string ARGs []) {try {serversocket = new serversocket (60001); While (true) {socket Ss = serversocket. accept (); string client = ss. getremotesocketaddress (). tostring (); sys TEM. out. println ("enable a client connection" + client); printstream ss_out = new printstream (ss. getoutputstream (); string temp = NULL; do {datainputstream ss_in = new datainputstream (ss. getinputstream (); temp = ss_in.readline (); If (temp! = NULL & "quit ". equals (temp. trim (). tolowercase () {ss. close (); system. out. println ("close a client connection" + client); break;} system. out. println ("message" "+ temp +" "From:" + client); ss_out.println ("server time:" + new simpledateformat ("yyyy-mm-dd hh: mm: SS "). format (system. currenttimemillis ();} while (temp! = NULL) ;}} catch (ioexception e) {e. printstacktrace ();}}}
The test method is the same as that of the Mina 2.0 instance -- timeserver:
If this class is directly compiled and executed, the server is started and the port is 60001. The client is the easiest to use. In Windows, use cmd to enter the console (Windows 7 does not enable this client by default, select "Control Panel-> Program-> program and function-> enable or disable WINDOWS function" and select "Telnet Server" and "Telnet client" for installation.
127.0.0.1 60001 "access the server, enter or do not enter any content, and click Enter to connect to the server to obtain the server time, and the server can also print the monitored client address; here we can open several more cmd windows and connect through Telnet. We can find that different windows are different sessions for the server. In this example, one machine is enabled for testing on the local machine and the other machine. Unlike Mina, this instance is a traditional socket. Therefore, after a client accesses the server, the server is blocked. At this time, even if other clients are connected to the client, the service cannot be obtained in real time. The previous client connection must be closed or its operation is completed to automatically exit, then other clients can obtain the service:
After the first client is connected:
If the first client does not close the connection, the client of the second machine cannot obtain the result even after the client is connected to the other machine. The service is available only when the first client is closed, in this case, the client window of the second server is displayed (enter 1, 2, 3, 4, and 5 as the first client, and each has a carriage return ):
The control bar on the server is displayed as follows:
Only when the first client closes the connection by entering quit or closing the CMD window can the second client obtain the service:
The window after the first client enters quit is displayed:
In this case, the window of the second client is printed as follows:
The server-side window is printed as follows:
It can be seen that when using traditional socket programming, only multiple threads can be used to provide real-time multi-concurrent access. For NiO-based non-blocking socket programming, see Mina
2.0 instance -- timeserver.