Socket communication based on TCP protocol, realize multithreading simple login __ Multithreading

Source: Internet
Author: User
Tags readline
Thank you for the Web "Java socket application-communication is so practiced" directory Structure

Server.java

/** * Socket communication based on TCP protocol, implementation login * Service end/public class Server {public static void main (string[] args) {try {
            1 Create a server-side socket, serversocket, that can only bind to the port and listen for this port.
            ServerSocket serversocket = new ServerSocket (8888);
            Socket socket = NULL;
            int count = 0;
            2 Call Accept () method to start listening, waiting for the client's connection System.out.println ("**************** server is about to start, waiting for the client to connect *************");
                while (true) {//Call the Accept () method to begin listening, wait for the client's connection socket = Serversocket.accept ();
                A new thread is worn serverthread serverthread = new Serverthread (socket);
                Start thread Serverthread.start ();
                count++;
                System.out.println ("Number of clients:" +count);
                InetAddress address = socket.getinetaddress ();
            SYSTEM.OUT.PRINTLN ("Current client IP" +address);
        } catch (IOException e) {e.printstacktrace ();
}
    }} 
Serverthread. Java
/** * Server thread handling class/public class Serverthread extends thread {socket socket = NULL;
    Public serverthread (socket socket) {this.socket = socket;
        public void Run () {InputStream is = null;
        InputStreamReader ISR = null;
        BufferedReader br = null;
        OutputStream OS = null;
        PrintWriter pw = null; try {//3. Gets the input stream and reads the client information is = Socket.getinputstream ();//byte input stream ISR = new Inputstreamrea
            Der to convert a stream of bytes to a character stream br = new BufferedReader (ISR);//Add cache String info = null for input streams; while (info = Br.readline ())!= null) {//Loop reads client information System.out.println ("I am the server.")
            Client said "+ info";
            Socket.shutdowninput ()//closes the input stream//4. Gets the output stream, responding to the client request OS = Socket.getoutputstream ();
            PW = new PrintWriter (OS); Pw.write ("welcome you.")
            ");
        Pw.flush (); The catch (IOException e) {e.printstacktrace ();
                Finally {//5. Close resource try {if (PW!= null) pw.close ();
                if (OS!= null) os.close ();
                if (br!= null) br.close ();
                if (ISR!= null) isr.close ();
                if (ISR!= null) is.close ();
            if (ISR!= null) socket.close ();
            catch (IOException e) {e.printstacktrace (); }


        }
    }

}
Client.java
/** * Based on the TCP protocol socket communication, implementation login * Client * * Public class Client {public static void main (string[] args) {try {
            1. Create client socket, specify server address and port socket socket = new socket ("localhost", 8888); 2. Get the output stream. Used to send information to the server outputstream OS = Socket.getoutputstream ()//byte output stream printwriter pw = new PrintWriter (OS)
            //The output stream is packaged as a print stream Pw.write ("Username: admin2 Password: 123");
            Pw.flush (); Socket.shutdownoutput ()//Turn off output stream//3. Gets the input stream to read the server's response information InputStream InputStream = socket.getinputst
            Ream ();
            BufferedReader br = new BufferedReader (new InputStreamReader (InputStream));
            String info = null;
            while (info = Br.readline ())!= null) {System.out.println ("I am the client, server says + info");
            //4. Close other resources pw.close ();
            Os.close ();
        Socket.close ();
 catch (IOException e) {e.printstacktrace ();       }

    }
} 

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.