Multithreading Techniques for sockets

Source: Internet
Author: User
Tags getmessage

The last used socket will be blocked if the data is not received.

We can use multithreading technology, the data sent over, by the server side of the thread to operate, a thread to operate a data sent past, and then sent back, not blocking the client

Test code

Client

ImportJava.io.DataInputStream;ImportJava.io.DataOutputStream;ImportJava.io.InputStream;Importjava.net.InetAddress;Importjava.net.InetSocketAddress;ImportJava.net.Socket;ImportJava.net.URL;ImportJava.util.Scanner; Public classTest { Public Static voidMain (String args[]) {DataInputStream in=NULL; DataOutputStream out=NULL; Socket Socket1=NULL; InetAddress IP=NULL; Read Read1=NULL;        Thread Thread1; Try{read1=NewRead (); Thread1=NewThread (read1);//ip=inetaddress.getbyname ("127.0.0.1");//inetsocketaddress ip2=new inetsocketaddress (ip,2010);//System.out.println ("3333");//if (socket1.isconnected ()) {}//else{//System.out.println ("3333");//Socket1.connect (IP2);//System.out.println ("222");socket1=NewSocket ("127.0.0.1", 2017); Inch=NewDataInputStream (Socket1.getinputstream ()); out=NewDataOutputStream (Socket1.getoutputstream ());                    Read1.setinputstream (in); Thread1.start ();//            }        }        Catch(Exception e) {System.out.println ("111");        System.out.println (E.getmessage ()); } Scanner scanner1=NewScanner (system.in);  for(; Scanner1.hasnext ();) {DoubleR=scanner1.nextdouble (); Try{out.writedouble (R); }            Catch(Exception e) {System.out.println (E.getmessage ()); System.out.println ("444"); }        }    }}classReadImplementsrunnable{PrivateURL url; PrivateDataInputStream in1; voidSetinputstream (DataInputStream a) {in1=A; }     Public voidrun () {DoubleResult=0;  for(;;) {            Try{result=in1.readdouble (); System.out.println ("Received" +result); }            Catch(Exception e) {System.out.println ("333");            System.out.println (E.getmessage ()); }        }    }}
View Code

Server-side

ImportJava.io.DataInputStream;ImportJava.io.DataOutputStream;Importjava.io.IOException;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public classtest2{ Public Static voidMain (String args[]) {DataInputStream in=NULL; DataOutputStream out=NULL; ServerSocket ServerSocket1=NULL; Socket Socket1=NULL;        ReadAndWrite Thread1;  for(;;) {            Try{ServerSocket1=NewServerSocket (2017); }            Catch(Exception e) {System.out.println ("111");            System.out.println (E.getmessage ()); }            Try{Socket1=serversocket1.accept (); }            Catch(Exception e) {System.out.println ("22");            System.out.println (E.getmessage ()); }            if(socket1!=NULL)                NewReadAndWrite (Socket1). Start (); }    }}classReadAndWriteextendsthread{DataInputStream in=NULL; DataOutputStream out=NULL;    Socket Socket1; Doubleb; ReadAndWrite (socket socket) {Socket1=socket; } @Override Public voidrun () {//TODO Auto-generated method stubs         for(;;) {            Try{ in=NewDataInputStream (Socket1.getinputstream ()); out=NewDataOutputStream (Socket1.getoutputstream ()); A=in.readdouble (); System.out.println ("Subject" +a); b=a*2; Try{Thread.Sleep (3000);//deliberately slow, show results                }                Catch(Exception e) {System.out.println (e.tostring ());            } out.writedouble (b); } Catch(IOException e) {//TODO Auto-generated catch blockSystem.out.println ("111");            System.out.println (E.getmessage ()); }        }    }    }
View Code

Effect

Multithreading Techniques for sockets

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.