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