Java socket programming (III) server sockets

Server sockets list 9.2 is part of a server application Listing 9.2 a simple server program / * * a program that listens to ports and provides HTML documents*/ Class simplewebserver {public static void main (string args []) {ServerSocket ServerSocket = null; socket ClientSocket = null; int connections = 0; try {{/ / create a server socket ServerSocket = new ServerSocket (80,5); while (connections < 5) {/ / wait for the connection ClientSocket = ServerSocket. Accept(); / / service connection serviceclient (ClientSocket) ; connects++; } serverSocket. close(); } catch (IOException ioe) { System.out.println("Error in SimpleWebServer: " + ioe); } } Public static void serviceclient (socket client) throws IOException {datainputstream inbound = null; dataoutputstream outbound = null; try {/ / get the IO stream inbound = new datainputstream (client. Getinputstream()); outbound = new dataoutputstream (client. Getoutputstream()); / / format the output (response header and few HTML documents) StringBuffer buffer = PrepareOutput(); String inputLine; while ((inputLine = inbound.readLine()) != Null) {/ / if the end of the HTTP request is reached, send a response if (inputline. Equals ("") {outbound.writebytes (buffer. Tostring()); break;}}} Finally {/ / clear system.out.println ("cleaning up connection:" + client); TLN ("cleaning up connection:" + client); outbound. Close(); inbound. Close(); client. Close(); client. Close();}} The server does not actively establish a connection Instead, they passively listen to a client's connection request and then serve them The server is built by the class ServerSocket The following program establishes a server socket and binds it to port 80: ServerSocket ServerSocket = new ServerSocket (80,5); The first parameter is the port on which the server will listen The second parameter is optional The API document states that this is a listening time, but in the traditional socket program, the second parameter is the listening depth A server can receive multiple connection requests at the same time, but can only process one at a time The listener heap is a connection request queue with no answer The above request establishes a connection to handle the last five requests If the following parameter is omitted, the default value is 50 ServerSocket serverSocket = new ServerSocket(80,5); Once the socket establishes and starts listening for connections, incoming connections will be established and placed on the listening heap The accetp () method takes the connection out of the heap Socket clientSocket = serverSocket. accept(); This method returns a client connection used to talk to the visitor The server itself cannot establish a conversation. On the contrary, the server socket will use the accept () method to generate a new socket The server socket is still open and arranges new connection requests Like the client socket, the following step is to establish the input and output stream: datainputstream inbound = new datainputstream (ClientSocket. Getinputstream()); DataOutputStream outbound = new DataOutputStream( clientSocket.getOutputStream() ); General I / O operations can be used in new streams It waits for the client to send a blank line before the server responds When the session ends, the server closes the stream and the client socket What happens if there is no request in the queue? That method will await the arrival of a This behavior is called blocking The accept () method will block the server thread until a call arrives When the five connections are closed, the server exits Any calls in the queue will be cancelled All servers should have the following basic steps: 1 Create a server socket and start listening 2. Use the accept () method to get a new connection 3. Establish input and output streams 4. Generate a session on an existing protocol 5. Close the client stream and socket 6. Go back to step 2 or step 7 7. Close the server socket

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>