JAVA learning notes (60)-network programming logon instance, java learning notes

Source: Internet
Author: User

JAVA learning notes (60)-network programming logon instance, java learning notes
Client

Import java. io. bufferedReader; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader; import java. io. objectOutputStream; import java. io. outputStream; import java. io. printWriter; import java.net. socket; import java.net. unknownHostException;/** client */public class LoginClient {public static void main (String [] args) {try {// 1. create a client Socket, specify the server address and port number, and establish a connection Socket socket = n Ew Socket ("localhost", 8888); // 2. obtain the output stream and send the OutputStream OS = socket to the server. getOutputStream (); // PrintWriter pw = new PrintWriter (OS); // encapsulate it as a print stream ObjectOutputStream oos = new ObjectOutputStream (OS); User user = new User ("admin ", "123"); oos. writeObject (user); // serialize the object socket. shutdownOutput (); // close the current output stream // 3. obtain the input stream and read the server response information. InputStream is = socket. getInputStream (); BufferedReader br = new BufferedReader (new InputStr EamReader (is); String reply = br. readLine (); while (reply! = Null) {System. out. println ("I Am a client, the server says:" + reply); reply = br. readLine ();} socket. shutdownInput (); // 4. disable resource br. close (); is. close (); oos. close (); OS. close (); socket. close ();} catch (UnknownHostException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}}
Title
Import java. io. bufferedReader; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader; import java. io. objectInputStream; import java. io. objectOutputStream; import java. io. outputStream; import java. io. printWriter; import java.net. serverSocket; import java.net. socket;/** server */public class LoginServer {public static void main (String [] args) {try {ServerSocket serverSocket = new ServerSocket (8888); System. out. println ("****** the server is about to start, waiting for the Client Connection"); int count = 0; // count the number of clients // The loop listener waits for the client connection while (true) {Socket socket = serverSocket. accept (); // create a new thread ServerThread thread = new ServerThread (socket); thread. start (); // start the thread count ++; System. out. println ("number of clients:" + count); System. out. println ("Client IP:" + socket. getInetAddress (). getHostAddress () ;}} catch (IOException e) {e. printStackTrace ();}}}
Server-side thread processing
Import java. io. IOException; import java. io. inputStream; import java. io. objectInputStream; import java. io. outputStream; import java. io. printWriter; import java.net. socket;/** server-side Thread processing class */public class ServerThread extends Thread {// Socket socket related to this Thread = null; public ServerThread (Socket socket) {this. socket = socket;} // The operation executed by the thread @ Override public void run () {InputStream is = null; ObjectInputStream ois = Null; OutputStream OS = null; PrintWriter pw = null; try {is = socket. getInputStream (); // byte stream ois = new ObjectInputStream (is); User user = (User) ois. readObject (); System. out. println ("user name:" + user. getUsername (); System. out. println ("Password:" + user. getPassword (); socket. shutdownInput (); // close the current input stream OS = socket. getOutputStream (); pw = new PrintWriter (OS); pw. println ("Welcome! "); 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
Import java. io. serializable;/** entity class User */public class User implements Serializable {private static final long serialVersionUID = 6682618522883563864L; private int id; private String username; private String password; public User () {super ();} public User (String username, String password) {super (); this. username = username; this. password = password;} public int getId () {return id;} public void setId (int id) {this. id = id;} public String getUsername () {return username;} public void setUsername (String username) {this. username = username;} public String getPassword () {return password;} public void setPassword (String password) {this. password = password;} public static long getSerialversionuid () {return serialVersionUID ;}}

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.