Java Socket Application communication implementation of multiple and login, query IP address
Service side:
Package Javalearn;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import Java.io.PrintWriter;
Import java.net.InetAddress;
Import Java.net.ServerSocket;
Import Java.net.Socket;
/** * Created by Chenhe on 2017/2/20. * Based on TCP protocol socket communication, to achieve user login/public class Server {public static void main (string[] args) {try {/
/1. Create a service socket, that is, Serversocke, specify the binding port, and listen for this port serversocket serversocket = new ServerSocket (8888);
2. Call the Accept () method to start listening, waiting for the client's connection System.out.println ("The server is about to start, waiting for the connection of the client");
Socket socket = NULL;
Record number of clients int count = 0;
while (true) {//Call the Accept () method to begin listening, wait for the client's connection socket = Serversocket.accept ();
To create a new thread serverthread Serverthread = the newer serverthread (socket); Start thread serveRthread.start ();
Count ++;//counts the number of clients System.out.println ("Number of clients:" +count);
Gets the current client's IP address inetaddress adress = socket.getinetaddress ();
SYSTEM.OUT.PRINTLN ("IP address of the current client:" +address.gethostaddress ()); ////3. Gets the input stream, and the client information is missing//InputStream is = Socket.getinputstream ();//Byte input stream//Inputstrea
Mreader ISR = new InputStreamReader (IS);//Convert a stream of bytes to character streams//BufferedReader BR = new BufferedReader (ISR);//Add buffering for character streams
String info = null;
while (info = Br.readline ())!=null) {//loop read client information//SYSTEM.OUT.PRINTLN ("I am server, client said:" +info); //Socket.shutdowninput ()/////4. Get output stream, response-side request//OutputStream
OS = Socket.getoutputstream ();
PrintWriter pw = new PrintWriter (OS); Pw.write ("welcome you.")
admin ");
Pw.flush (); //close resource//Pw.closE ();
Os.close ();
Br.close ();
Isr.close ();
Is.close ();
Socket.close ();
Serversocket.close ();
catch (IOException e) {e.printstacktrace ();
}
}
}
Thread class:
Package Javalearn;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import Java.io.PrintWriter;
Import Java.net.Socket;
/** * Created by Chenhe on 2017/2/21.
* Server Threading Class */public class Serverthread extends thread{//And this thread-related socket socket sockets = NULL;
Public serverthread (socket socket) {this.socket = socket; }//Thread performs action, responding to client request public void Run () {InputStream is = null;//byte input stream InputStreamReader ISR = null;
Converts a stream of bytes to character streams BufferedReader br = null;//adds buffering for character streams outputstream OS = null;
PrintWriter pw = null;
try {//3. Gets the input stream and is missing the client information is = Socket.getinputstream ();
ISR = new InputStreamReader (IS);
br = new BufferedReader (ISR);
String info = null; while (info = Br.readline ())!=null) {//loop read client information System.out.println ("I am the server," said the client: "+iNFO);
Socket.shutdowninput ()//closes the input stream//4. Gets the output stream, the response-side Request OS = Socket.getoutputstream ();
PW = new PrintWriter (OS); Pw.write ("welcome you.")
admin ");
Pw.flush ();
catch (IOException e) {e.printstacktrace ();
}finally {try {//close resource if (pw!=null) pw.close ();
if (os!=null) os.close ();
if (br!=null) br.close ();
if (isr!=null) isr.close ();
if (is!=null) is.close ();
if (socket!=null) socket.close ();
catch (IOException e) {e.printstacktrace ();
}
}
}
}
Client:
Package Javalearn;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import Java.io.PrintWriter;
Import Java.net.Socket;
/** * Created by Chenhe on 2017/2/20. * Client/public class client {public static void main (string[] args) {try {//1. Creates a client socket, specifying
server address and port socket socket = new socket ("localhost", 8888); 2. Get output stream, send information to server side outputstream OS = Socket.getoutputstream ();//byte output stream printwriter pw = new PRINTW
Riter (OS);//The output stream is packaged as a print stream Pw.write ("username: admin; Password: 1236"); Pw.flush ()//flushes the cache, like the server-side Submit information socket.shutdownoutput ()//Turn off output stream//3. Gets the input stream and reads the response information on the servers side Inpu
Tstream is = Socket.getinputstream ();
BufferedReader br = new BufferedReader (new InputStreamReader (IS));
String info = null;
while (info = Br.readline ())!=null) { System.out.println ("I am the client," said the server: "+info);
//4. Close resource Br.close ();
Is.close ();
Pw.close ();
Os.close ();
Socket.close ();
catch (IOException e) {e.printstacktrace ();
}
}
}