Home > other >  Written in the Socket Http server can only be a data read from the client
Written in the Socket Http server can only be a data read from the client

Time:09-22

Written in the socket of the HTTP server can only be a data read from the client, the back will not be able to read, but still have a TCP connection between client and server, and can be returned to the client data
The server-side code:
Public class Server {
Private static final Integer port=7777;
Public static void main (String [] args) {
ServerSocket ServerSocket.
Try {
System. Out.println (" Server start... ");
Serversocket=new Serversocket (port);//create a Socket, monitor the client request
//the server has been in a listening state
While (true) {
The Socket Socket=Serversocket. The accept ();//server has been in a listening state, and here waiting, when you have the client's message arrives, to continue to perform down
System. Out. Println (" build a new TCP link with the client, the addressis: "+ socket. GetInetAddress () +" : "+ socket. The getPort ());
//concurrent processing HTTP requests the client
Service (socket);
}
} the catch (IOException e) {
e.printStackTrace();
}
}

Public static void service (Socket Socket) {
New Thread (new Runnable () {
@ Override
Public void the run () {
InputStream inSocket;
Try {
InSocket=socket. GetInputStream ();//get the HTTP request header
Int size=inSocket. Available ();
Byte [] buffer=new byte [size];
InSocket. Read (buffer);
String request=new String (buffer);
//System. Out. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \ nClientBrowser: \ n "+ request +" \ n -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ");

String contentType="application/json";
If (request. Length () & gt; 0 {
System. The out. Println (request);
}

//the response is sent to the client
//System. Out. Println (flag);
String responseFirstLine="HTTP/1.1 200 OK \ r \ n";
String responseHead="content-type:" + contentType + "\ r \ n";
OutputStream outSocket=socket. GetOutputStream ();
//System. Out. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \ nServerResponse: \ n "+ request +" \ n -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ");
OutSocket. Write (responseFirstLine getBytes ());
OutSocket. Write (responseHead getBytes ());
OutSocket. Write (" \ r \ n ". GetBytes ());
//response message header, for the following written content

OutSocket. Write (" {\ "name " : \ "gyy ", "the getBytes ());
OutSocket. Write (" \ "words: ", \ "I love you "} ". GetBytes ());
OutSocket. Flush ();
//inSocket. Close ();
OutSocket. Close ();
Socket. The close ();
} the catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}

CodePudding user response:

When I step in to see, and be able to read to the message, but can be run directly, bosses, what should I do

CodePudding user response:

Read to determine the return value
  • Related