Multithreading issues in TCP usage
1, the problem: The General TCP program in the processing of multi-threaded situations, there will be problems:
Such as: There is a, b two clients to send data to the server, when a client connection is later acquired by the service side, the service side to execute the specific process; At this point the B client connects, only waits.
Because the server has not finished processing the a client's request, there is a loop back to execute the next accept method. Therefore, the B client object is not available temporarily.
2, solve the idea:
The best service side is to encapsulate each client in a separate thread so that multiple client requests can be processed at the same time.
Program code:
Client:
1 classpicsend{2 3 Public Static voidMain (String args[]) {4 Try {5Socket s =NewSocket (Inetaddress.getlocalhost (). Gethostaddress (), 11001 );6FileInputStream ins =NewFileInputStream ("Test/1.jpg");7OutputStream out =S.getoutputstream ();8 //BufferedReader bufr = new BufferedReader (New InputStreamReader (S.getinputstream ()));9 byte[] buf =New byte[1024];Ten intLen = 0; One while(len = Ins.read (BUF))! =-1){ AOut.write (buf, 0, Len); - } - theS.shutdownoutput ();//end image upload, read server return information - - //String str = bufr.readline (); - //System.out.println (str); +InputStream in =S.getinputstream (); - byte[] bytes =New byte[1024]; + intLENS =in.read (bytes); ASYSTEM.OUT.PRINTLN ("server returned:" +NewString (bytes,0, Lens)); at - ins.close (); - s.close (); - -}Catch(Exception e) { - //TODO auto-generated Catch block in e.printstacktrace (); - } to + } - the *}
Server-side:
1 classthreadserver{2 Public Static voidMain (String args[]) {3 Try {4ServerSocket Server =NewServerSocket (10001);5 6 while(true){//Note: True in while is because the accept method is a blocking method and does not cause the program to loop indefinitely7Socket s =server.accept ();8 NewThread (NewPicthread (s)). Start ();9 }Ten}Catch(IOException e) { One //TODO auto-generated Catch block A e.printstacktrace (); - } - the } - } - - classPicthreadImplementsrunnable{//encapsulating the client, implementing a single thread + PrivateSocket S; - Picthread (Socket s) { + This. S =s; A at } - - Public voidRun () {//writes the client to the code in the Run method - Try { -InputStream in =S.getinputstream (); -FileOutputStream Fout =NewFileOutputStream ("Thread.jpg"); in byte[] buf =New byte[1024]; - intLen = 0; to while((Len=in.read (BUF))!=-1){ +Fout.write (buf,0, Len); - } the *OutputStream out =S.getoutputstream (); $ byte[] bytes = "Image upload succeeded". GetBytes ();Panax Notoginseng out.write (bytes); - the fout.close (); + s.close (); A the}Catch(Exception e) { + //TODO auto-generated Catch block - e.printstacktrace (); $ } $ - } -
This enables multiple clients to send files to the server.
3. Design Highlights
In this TCP program, it is very clever to encapsulate each client in a single thread, so that the server has the ability to handle multithreading, which is very useful in development.
New ServerSocket (10001); while (true) { = server.accept (); New Thread (new Picthread (s)). Start ();
Multiple clients uploading files