Server code:
//Server side public class Server { public static void main(String[] args) throws Exception { ServerSocket serverSocket = new ServerSocket(8888); System.out.println("Server startup=========="); while(!serverSocket.isClosed()){ Socket socket = serverSocket.accept(); //Blocking, waiting for connection System.out.println("And " + socket.toString() + "Connection successful"); try{ // I/O InputStream inputStream = socket.getInputStream(); //Receive data stream BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8")); String message; while((message = reader.readLine())!= null){ if(message.length() == 0) break; System.out.println(message); } System.out.println("Received to:" + socket.toString() + "Data"); }catch (IOException e){ e.printStackTrace(); }finally { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
Server side startup:
Use browser to interact with server
localhost:8888
Go back to the server to view the output:
The connection between the browser and the server is established successfully, and the request packet is sent according to the HTTP protocol:
The request header in HTTP protocol usually includes four parts:
- Request line, including request type, resource path, HTTP version and other information.
- The request header, immediately after the request line, is used to describe the additional information to be used by the server.
- Blank line, the blank line behind the request header, is required.
- Request data, also known as the principal, can add any data.
In this example, the request data is empty.
In the above, when the browser interacts with the server, it prompts that the web page cannot run.
This is because the server does not respond to the request sent by the browser. We need to write the code to respond to the browser on the server side.
public class Server { public static void main(String[] args) throws Exception { ServerSocket serverSocket = new ServerSocket(8888); System.out.println("Server startup=========="); while(!serverSocket.isClosed()){ Socket socket = serverSocket.accept(); //Blocking, waiting for connection System.out.println("And " + socket.toString() + "Connection successful"); try{ // I/O InputStream inputStream = socket.getInputStream(); //Receive data stream BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8")); String message; while((message = reader.readLine())!= null){ if(message.length() == 0) break; System.out.println(message); } System.out.println("Received to:" + socket.toString() + "Data"); OutputStream outputStream = socket.getOutputStream(); outputStream.write("HTTP/1.1 200 OK\r\n".getBytes()); outputStream.write("Content-Length:11\r\n\n".getBytes()); outputStream.write("Hello World!".getBytes()); outputStream.flush(); }catch (IOException e){ e.printStackTrace(); }finally { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
Browser revisit: localhost:8888
Get the response result.
Attachment: HTTP protocol - response status code
- 1XX (temporary response)
A status code that represents a temporary response and requires the requester to continue the operation - 2XX (successful)
Status code indicating that the request was successfully processed. - 3XX (redirect)
Indicates that further action is required to complete the request. Typically, these status codes are used for redirection. - 4XX (request error)
These status codes indicate that the request may be in error and hinder the processing of the server. - 5XX (server error)
These status codes indicate that the server encountered an internal error while trying to process the request. These errors may be caused by the server itself, not by the request.