Java SE -- TCP network programming (3), se Network Programming

Source: Internet
Author: User

Java SE -- TCP network programming (3), se Network Programming

In the previous code, the input stream and output stream of the socket object are closed, but the socket object is not closed, which will cause a waste of server resources. You should call the close () of the socket () method to close the current socket object.

 

Therefore, you can create a ServerScanThread to keep it running in the background and scan to see which socket object's Input Stream and OutputStream are closed, when the input and output streams of the current socket object are scanned, the current socket object is closed.

Scan thread code:

Public class ServerScanThread extends Thread {public static List <Socket> socketList = new external List <Socket> (); // instantiate a static List to store Socket, use the synchronized list to delete public ServerScanThread () {setDaemon (true); // set it to the background thread start ();} public void run () {while (true) {// 1. I want to scan the entire set. If the current set is empty, I will go to bed while (socketList = null | socketList. size () <= 0) {System. out. println ("no socket in the collection now, I will go to bed for 10 s"); // test the code try {Thread. sl Eep (10000);} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace () ;}// 2. if the program runs here, it indicates that there are elements in the socketList. Then, it starts scanning to determine whether its socket object has disabled its input stream and input stream. For (int I = 0; I <socketList. size (); I ++) {Socket socket = socketList. get (I); // obtain a socket object from the socketList set. If (socket. isInputShutdown () & socket. isOutputShutdown () {try {socket. close (); // close the current socket object socketList. remove (I); // remove the current socket object from the current list. System. out. println ("A socket is closed"); // test code // I --; when an element is removed from a list, all element subscripts after it are dropped by one, to ensure that an element is not skipped, You can subtract I from it or scan it again.} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} try {

Thread. sleep (5000); // five seconds after scanning} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace () ;}// after the entire thread is complete, it can be called on the Server. }}

Server code:

Public class Server1 {public static void main (String [] args) {custom input = new partition (System. in); try {@ SuppressWarnings ("resource") ServerSocket server = new ServerSocket (9880); new ServerScanThread (); while (true) {Socket client = server. accept (); ServerScanThread. socketList. add (client); // add the socket Instance Object System to the socketList set. out. println ("from" + client. getInetAddress (). getHostAddress () + "client Connection successful! "); If (client! = Null) {// start dialog new WriterThread ("server", client); new ReaderThread ("server", client) ;}} catch (IOException e) {e. printStackTrace ();} input. close ();}}
  • Output when no client is running:

At this time, there is no socket object, and the program will not stop running in the first while loop inside, printing once every 10 seconds.

  • Output when the client is running but the socket input and output streams are not closed:

Because it is the server that runs first, the while loop will run once first. There is no socket object in this loop. When the client starts running, because no byebye is input, therefore, the socket input stream and output stream will not be closed, and the program will execute the internal for loop continuously.

  • Output when the client is running and the socket input/output stream is closed:

After both ends input byebye to close the input and output streams, the current socket is scanned and closed. Because the scan thread is set to a background thread, when the Server stops running, the thread also stops.

 

If anything is wrong, please correct me. Thank you (● 'shanghai' ●)

 

Related Article

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.