Multi-threaded Multi-user access to the server
I. Thread method
1 import Java. io. ioexception; 2 Import Java. io. inputstream; 3 Import Java. io. objectinputstream; 4 Import Java. io. outputstream; 5 import Java. io. printwriter; 6 Import java.net. serversocket; 7 Import java.net. socket; 8 9 Import Pb. socket. entity. user; 10 11/** 12 * Special Thread class 13 **/14 public class serverthread extends thread {15 // socket16 Socket socket related to this thread = NULL; 17 18 public serverthread (Socket socket ){ 19 this. socket = socket; 20} 21 22 // thread startup: response to customer request 23 public void run () {24 try {25 // 3. obtain the input stream 26 inputstream is = socket. getinputstream (); 27 // get stream: the object can be deserialized 28 objectinputstream OIS = new objectinputstream (is); 29 // get output stream 30 outputstream OS = socket. getoutputstream (); 31 printwriter PW = new printwriter (OS); 32 // 4. read user input information 33 user = (User) Ois. readobject (); 34 system. out. println ("user information:" + User. getloginname () + "---" + us Er. getpwd (); 35 // give the customer a response 36 string reply = "Welcome! "; 37 PW. write (reply); 38 PW. flush (); 39 // 5. close resource 40 PW. close (); 41 OS. close (); 42 Ois. close (); 43 is. close (); 44 socket. close (); 45} catch (ioexception e) {46 // todo auto-generated catch block47 E. printstacktrace (); 48} catch (classnotfoundexception e) {49 // todo auto-generated catch block50 E. printstacktrace (); 51} 52} 53}
Ii. server code
/*** Server **/public class loginserver {public static void main (string [] ARGs) {try {// 1. create a server socket (serversocket), bind the specified port, and start listening to serversocket = new serversocket (8800); // 2. use the accept () method to block the waiting listener and obtain a new connection Socket socket = NULL; // number of customers int num = 0; // It is always in the listener status while (true) {socket = serversocket. accept (); serverthread = new serverthread (socket); serverthread. start (); num ++; system. out. println ("number of customers:" + num); // the customer's IP information inetaddress IA = socket. getinetaddress (); // the customer's IP string IP address = IA. gethostaddress (); system. out. println ("the customer's IP address is:" + IP); // the customer's host name string hostname = IA. gethostname (); system. out. println ("the customer's host is:" + hostname) ;}} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace ();}}}
Iii. client code
1 import Java. io. bufferedreader; 2 Import Java. io. ioexception; 3 Import Java. io. inputstream; 4 Import Java. io. inputstreamreader; 5 import Java. io. objectoutputstream; 6 Import Java. io. outputstream; 7 Import Java. io. printwriter; 8 Import java.net. socket; 9 Import java.net. unknownhostexception; 10 11 import Pb. socket. entity. user; 12 13/** 14 * client 15 **/16 public class loginclient {17 public static void Main (string [] ARGs) {18 try {19 // 1. establish a client socket connection, specify the server location and Port 20 Socket socket = new socket ("localhost", 8800); 21 // 2. obtain the socket read/write stream 22 outputstream OS = socket. getoutputstream (); 23 // Object serialization stream 24 objectoutputstream OOS = new objectoutputstream (OS); 25 // input stream 26 inputstream is = socket. getinputstream (); 27 bufferedreader BR = new bufferedreader (New inputstreamreader (is); 28 // 3. use the stream to perform read/write operations on the socket according to certain protocols 29 user = new User (); 30 user. setloginname ("Kate"); 31 user. setpwd ("120056"); 32 OOS. writeobject (User); 33 34 socket. shutdownoutput (); 35 // receive the server response and print 36 string reply = NULL; 37 while (! (Reply = BR. readline () = NULL) {38 system. out. println ("I Am a client, the server's response is:" + reply); 39} 40 // 4. close resource 41 BR. close (); 42 is. close (); 43 OOS. close (); 44 OS. close (); 45 socket. close (); 46} catch (unknownhostexception e) {47 // todo auto-generated catch block48 E. printstacktrace (); 49} catch (ioexception e) {50 // todo auto-generated catch block51 E. printstacktrace (); 52} 53 54} 55}
Sokect (2) TCP protocol 1