PackageCn.com.pb.multi;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 (); //serialization of ObjectsObjectOutputStream oos=Newobjectoutputstream (OS); //Send client logon information, that is, write information to the output streamUser user=NewUser (); User.setloginname ("Tom"); User.setpwd ("123456"); Oos.writeobject (user); 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 ResourcesOos.close (); Is.close (); Os.close (); Socket.close (); } Catch(unknownhostexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } }}
PackageCn.com.pb.multi;Importjava.net.*;ImportJava.io.*; Public classLoginClient2 { 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 (); //serialization of ObjectsObjectOutputStream oos=Newobjectoutputstream (OS); //Send client logon information, that is, write information to the output streamUser user=NewUser (); User.setloginname ("Kate"); User.setpwd ("654321"); Oos.writeobject (user); 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 ResourcesOos.close (); Is.close (); Os.close (); Socket.close (); } Catch(unknownhostexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } }}
PackageCn.com.pb.multi;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=NULL; //monitoring has been in progress while(true) {Socket=serversocket.accept (); Loginthread Loginthread=Newloginthread (socket); Loginthread.start (); } } Catch(unknownhostexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } }}
PackageCn.com.pb.multi;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.ObjectInputStream;ImportJava.io.OutputStream;ImportJava.net.Socket;Importjava.net.UnknownHostException; Public classLoginthreadextendsThread {Socket Socket=NULL; //one thread per boot, corresponding to the socket PublicLoginthread (socket socket) { This. socket=socket; } //start thread, which responds to customer requests Public voidrun () {Try { //Open the input and output streamInputStream is=Socket.getinputstream (); OutputStream OS=Socket.getoutputstream (); //deserializationObjectInputStream ois=NewObjectInputStream (IS); //get client information, that is, reading information from the input stream//BufferedReader br=new BufferedReader (New InputStreamReader (OIS));User user=(User) ois.readobject (); if(! (user==NULL) {System.out.println ("I am the server, the customer login information is:" +user.getloginname () + "," +user.getpwd ()); } //a response to the client that writes information to the output streamString reply= "welcome you, login successful!"; Os.write (Reply.getbytes ()); //Close ResourceOis.close (); Os.close (); Is.close (); Socket.close (); } Catch(unknownhostexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }Catch(ClassNotFoundException e) {e.printstacktrace (); } }}
PackageCn.com.pb.multi;Importjava.io.Serializable;/*** User class **/ Public classUserImplementsserializable{PrivateString LoginName;//User name PrivateString pwd;//User Password PublicUser () {} PublicUser (String loginName, string pwd) {Super(); This. LoginName =LoginName; This. PWD =pwd; } PublicString Getloginname () {returnLoginName; } Public voidsetloginname (String loginName) { This. LoginName =LoginName; } PublicString getpwd () {returnpwd; } Public voidsetpwd (String pwd) { This. PWD =pwd; }}
47. Multi-Client User Login