46. Simulate the user login function

Source: Internet
Author: User

 Packagecn.com.pb.base;Importjava.net.*;ImportJava.io.*; Public classloginclient { Public Static voidMain (string[] args) {Try {            //establish a Client socket connection, specify the location of the server and the portSocket socket=NewSocket ("localhost", 8800); //Open the input and output streamOutputStream os=Socket.getoutputstream (); InputStream is=Socket.getinputstream (); //Send client logon information, that is, write information to the output streamString info= "user name: Tom; user password: 123456";            Os.write (Info.getbytes ());                    Socket.shutdownoutput (); //receives the response from the server segment, which reads the information from the input streamString reply=NULL; BufferedReader BR=NewBufferedReader (NewInputStreamReader (IS));  while(! ((Reply=br.readline ()) = =NULL) {System.out.println ("I am the client, the response of the server is:" +reply); }            //4. Close ResourcesBr.close ();                    Is.close ();            Os.close ();        Socket.close (); } Catch(unknownhostexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}
 Packagecn.com.pb.base;Importjava.net.*;ImportJava.io.*; Public classLoginserver { Public Static voidMain (string[] args) {Try {            //set up a server socket (serversocket) to specify the port and start listeningServerSocket serversocket=NewServerSocket (8800); //use the Accept () method to wait for the client to trigger communicationSocket socket=serversocket.accept (); //Open the input and output streamInputStream is=Socket.getinputstream (); OutputStream OS=Socket.getoutputstream (); //get client information, that is, reading information from the input streamBufferedReader br=NewBufferedReader (NewInputStreamReader (IS)); String Info=NULL;  while(! ((Info=br.readline ()) = =NULL) {System.out.println ("I am the server, the customer login information is:" +info); }                        //a response to the client that writes information to the output streamString reply= "welcome you, login successful!";            Os.write (Reply.getbytes ()); //Close ResourceBr.close ();            Os.close ();            Is.close ();            Socket.close ();        Serversocket.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}

46. Simulate the user login function

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.