1 The server method is divided into three steps,
start() receive() stop()
2 The client is the browser. After the server is established, enter the ip: port in the client to access it.
3 The code is as follows:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; /** * Create a server and start it * @author Administrator Enter http://localhost:8888 in the browser to print the information requested by the browser in the console The print result is as follows: GET / HTTP/1.1 Host: localhost:8888 Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36 Accept-Encoding: gzip,deflate,sdch Accept-Language: zh-CN,zh;q=0.8 */ public class Server { private ServerSocket server; /** * @param args */ public static void main(String[] args) { Server server = new Server(); server.start(); } /** * start method */ public void start(){ try { server = new ServerSocket(8888); this.receive(); } catch (IOException e) { e.printStackTrace (); } } /** * Receive client */ private void receive(){ try { Socket client =server.accept(); StringBuilder sb =new StringBuilder(); String msg =null; BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream())); while((msg=br.readLine()).length()>0){ sb.append(msg); sb.append("\r\n"); } //Receive the request information from the client String requestInfo =sb.toString().trim(); System.out.println(requestInfo); } catch (IOException e) { //e.printStackTrace(); } } /** * listen to the server */ public void stop(){ } }