Java Learning Note (60)-Network Programming Login Instance

Source: Internet
Author: User

 import; import; import; import; import; import; import; import; import;/ * * Client * * Public  class loginclient {     Public Static voidMain (string[] args) {Try{//1. Create a client socket, specify the server address and port number, and establish a connectionSocket socket=NewSocket ("localhost",8888);//2. Getting the output stream, sending information to the serverOutputStream Os=socket.getoutputstream ();//printwriter pw=new printwriter (OS);//package as print streamObjectOutputStream oos=NewObjectOutputStream (OS); User user=NewUser ("Admin","123"); Oos.writeobject (user);//Serialized ObjectSocket.shutdownoutput ();//close current output stream            //3. Get the input stream and read the server-side response informationInputStream is=socket.getinputstream (); BufferedReader br=NewBufferedReader (NewInputStreamReader ( is)); String Reply=br.readline (); while(reply!=NULL) {System.out.println ("I'm the client," said the server .+reply);            Reply=br.readline (); } socket.shutdowninput ();//4. Closing ResourcesBr.close (); is. Close ();            Oos.close ();            Os.close ();        Socket.close (); }Catch(Unknownhostexception e)        {E.printstacktrace (); }Catch(IOException e)        {E.printstacktrace (); }    }}
 import; import; import; import; import; import; import; import; import; import;/ * * Server side * / Public  class loginserver {     Public Static voidMain (string[] args) {Try{ServerSocket serversocket=NewServerSocket (8888); System.out.println ("****** server is about to start, waiting for client connections"); int count=0;//Statistics Client number            //Loop monitoring waits for client's connection             while(true) {Socket socket=serversocket.accept ();//Create a new threadServerthread thread=NewServerthread (socket); Thread.Start ();//Start threadcount++; System.out.println ("Number of clients:"+count); System.out.println ("Client IP:"+socket.getinetaddress (). gethostaddress ()); }        }Catch(IOException e)        {E.printstacktrace (); }     }}
Server thread Processing class;;;;;;/ * * Server thread processing class */ Public  class serverthread extends Thread {    //socket associated with this threadSocket socket=NULL; Public Serverthread(Socket socket) { This. Socket=socket; }//actions performed by thread    @Override     Public void Run() {InputStream is=NULL; ObjectInputStream ois=NULL; OutputStream os=NULL; PrintWriter pw=NULL;Try{Is=socket.getinputstream ();//byte streamois=NewObjectInputStream (IS);            User user= (user) ois.readobject (); System.out.println ("User name:"+user.getusername ()); System.out.println ("Password:"+user.getpassword ()); Socket.shutdowninput ();//close current input streamOs=socket.getoutputstream (); pw=NewPrintWriter (OS); Pw.println ("Welcome you!" ");            Pw.flush ();        Socket.shutdownoutput (); }Catch(IOException e)        {E.printstacktrace (); }Catch(ClassNotFoundException e)        {E.printstacktrace (); }finally{Try{if(pw!=NULL) Pw.close ();if(os!=NULL) Os.close ();if(ois!=NULL) Ois.close ();if(is!=NULL) Is.close ();            Socket.close (); }Catch(IOException e)            {E.printstacktrace (); }        }    }}
Title;/ * * Entity class User * / Public  class User implements Serializable {    Private Static Final LongSerialversionuid =6682618522883563864LPrivate intIdPrivateString username;PrivateString password; Public User() {Super(); } Public User(string username, string password) {Super(); This. Username = Username; This. Password = password; } Public int getId() {returnId } Public void setId(intID) { This. id = ID; } PublicStringGetUserName() {returnUsername } Public void Setusername(String username) { This. Username = Username; } PublicStringGetPassword() {returnPassword } Public void SetPassword(String password) { This. Password = password; } Public Static Long Getserialversionuid() {returnSerialversionuid; }}

Java Learning Note (60)-Network Programming Login Instance

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: 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.