Java Basic Knowledge Enhancement Network programming note 14:tcp multiple clients upload to a server thinking (multithreading improvements)

Source: Internet
Author: User

1. Thinking of uploading multiple clients to a server

A while loop improves the ability of a server to receive multiple clients.

But this is a problem .
If this is the case, let's say I have Zhang San, John Doe, Harry the three of them executing the client separately
Zhang San: study hard. avi (100M) 256k
John Doe: Day to day. mp3 (3M) 1M
Harry: ILoveJava.txt (1k) 100M

This will wait Zhang San Pass, then go to preach John Doe ...

2. Using Multithreading improvements:

(1) Thread class Userthread:

1  Packagecn.itcast_15;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.BufferedWriter;5 ImportJava.io.FileWriter;6 Importjava.io.IOException;7 ImportJava.io.InputStreamReader;8 ImportJava.io.OutputStreamWriter;9 ImportJava.net.Socket;Ten  One  Public classUserthreadImplementsRunnable { A     PrivateSocket S; -  -      PublicUserthread (Socket s) { the          This. S =s; -     } -  - @Override +      Public voidrun () { -         Try { +             //flow within the encapsulated channel ABufferedReader br =NewBufferedReader (NewInputStreamReader ( at S.getinputstream ())); -             //Encapsulating text Files -             //bufferedwriter bw = new BufferedWriter (New -             //FileWriter ("Copy.java")); -  -             //to prevent name collisions inString newName = System.currenttimemillis () + ". Java"; -BufferedWriter BW =NewBufferedWriter (NewFileWriter (NewName)); to  +String line =NULL; -              while(line = Br.readline ())! =NULL) {//Blocking the Bw.write (line); * bw.newline (); $ Bw.flush ();Panax Notoginseng             } -  the             //Give the feedback +BufferedWriter Bwserver =NewBufferedWriter ( A                     NewOutputStreamWriter (S.getoutputstream ())); theBwserver.write ("File Upload succeeded"); + bwserver.newline (); - Bwserver.flush (); $  $             //Freeing Resources - bw.close (); - s.close (); the}Catch(IOException e) { - e.printstacktrace ();Wuyi         } the     } -  Wu}

(2) Client:

1  Packagecn.itcast_15;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.BufferedWriter;5 ImportJava.io.FileReader;6 Importjava.io.IOException;7 ImportJava.io.InputStreamReader;8 ImportJava.io.OutputStreamWriter;9 ImportJava.net.Socket;Ten  One  Public classuploadclient { A      Public Static voidMain (string[] args)throwsIOException { -         //creating a Client socket object -Socket s =NewSocket ("192.168.12.92", 11111); the  -         //Encapsulating text Files -         //bufferedreader br = new BufferedReader (New FileReader ( -         //"Inetaddressdemo.java")); +BufferedReader br =NewBufferedReader (NewFileReader ( -"Receivedemo.java")); +         //Package Channel Internal flow ABufferedWriter BW =NewBufferedWriter (NewOutputStreamWriter ( at S.getoutputstream ())); -  -String line =NULL; -          while(line = Br.readline ())! =NULL) {//Blocking - Bw.write (line); - bw.newline (); in Bw.flush (); -         } to  +         //The socket provides a termination, it notifies the server you don't have to wait, I don't have the data . - s.shutdownoutput (); the  *         //Receive Feedback $BufferedReader brclient =NewBufferedReader (NewInputStreamReader (Panax Notoginseng S.getinputstream ())); -String client = Brclient.readline ();//Blocking the System.out.println (client); +  A         //Freeing Resources the br.close (); + s.close (); -     } $}

(3) Server side:

1  Packagecn.itcast_15;2 3 Importjava.io.IOException;4 ImportJava.net.ServerSocket;5 ImportJava.net.Socket;6 7  Public classUploadserver {8      Public Static voidMain (string[] args)throwsIOException {9         //Create a Server socket objectTenServerSocket SS =NewServerSocket (11111); One  A          while(true) { -Socket s =ss.accept (); -             NewThread (NewUserthread (s)). Start (); the         } -     } -}

Java Basic Knowledge Enhancement Network programming note 14:tcp multiple clients upload to a server thinking (multithreading improvements)

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.