The use of sockets in Java |
- The sockets in Java are divided into ordinary sockets and niosocket.
How to use a common socket |
The network communication in Java through the socket implementation, the socket is divided into serversocket and socket two categories, serversocket for the server side, you can listen to the request through the Accept method, listen to the request to return the socket, The socket is used to complete the specific data transfer, and the client can initiate the request and transfer the data using the socket. The use of ServerSocket can be divided into three steps:
- Create a serversocket. ServerSocket is constructed in 5 ways, the most convenient of which is serversocket (int port), which requires only one port.
- Call the created ServerSocket's accept method to listen. The Accept method is a blocking method, which means that the program will stop waiting for the connection request after the Accept method is called, and the program will not continue execution until the request is received, and the Accept method returns a socket after the request.
- Use the socket returned by the Accept method to communicate with the client
In the following code, we create the ServerSocket on the server side and call the Accept method to listen to the client's request and return a socket when the request is received.
Public classServer { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub Try { //Create a serversocket listening 8080 portServerSocket Server =NewServerSocket (8080); //Wait for requestSocket socket =server.accept (); //use socket to communicate after accepting request, create bufferedreader for reading dataBufferedReader is =NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); String Line=Is.readline (); System.out.println ("Received Frome client:" +Line ); //Create a printwriter for sending dataPrintWriter PW =NewPrintWriter (Socket.getoutputstream ()); Pw.println ("This data was from server"); Pw.flush (); //Close ResourcePw.close (); Is.close (); Socket.close (); Server.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
View Code
Then we look at the client socket code, the socket is the same, the first to create a Socket,socket construction method is very much, here is the socket (String host, int port), The address and port number of the target host is passed in ( the server and client code in this experiment code are not on the same machine, Server IP Address: 192.168.6.42, so if the reader in the experiment process serversocket and client under the same host, then the IP address in the client needs to be changed to: 127.0.0.1, The process of creating a socket creates a connection to the server, creates a socket, creates writer and reader to transfer the data, and frees the resource to close the connection after the data transfer is complete.
Public classClient { Public Static voidMain (string[] args) {//TODO auto-generated Method StubString msg = "Client data"; Try { //Create a socket that is linked to the server's 8080 portSocket socket =NewSocket ("192.168.6.42", 8080); //Read and write data using PrintWriter and BufferedReaderPrintWriter PW =NewPrintWriter (Socket.getoutputstream ()); BufferedReader is=NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); //Send Datapw.println (msg); Pw.flush (); //Receive DataString line =Is.readline (); System.out.println ("Received from server" +Line ); //Close ResourcePw.close (); Is.close (); Socket.close (); } Catch(unknownhostexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
View Code
Finally, start the server and then start the client to complete the client and server communication once.
Starting with JDK1.4, Java has added new IO-mode-nio, and NiO has adopted a new approach at the bottom, greatly improving the efficiency of IO. The socket we use is also a type of Io, and NIO provides the appropriate tools: Serversocketchannel and Socketchannel, which correspond to the original ServerSocket and server respectively.
Before we get to know Niosocket, we understand the buffer, Channel, Selector. In order to facilitate understanding, we look at an example, to the Christmas, need to send greeting cards and apples to the students, the monitor is the most laborious, every time to take an apple and a greeting card to a classmate, send after the completion of a greeting card and an Apple sent to another classmate, until the whole class to get cards and apples so far, This is the ordinary socket processing mode, to a request, serversocket processing, processing completed, continue to accept the request, this method is very inefficient Ah! Or the example of Christmas, the monitor found that the class committee not only he one, informed the Life Committee (female) and the organization members (male) to help him send greeting cards and apples, girls greeting card is pink, the boy's greeting card is blue, the Life Committee is responsible for the card from the class to select Girls Cards, and the organization members are responsible for boys Then the Life Committee and the organization members to the dormitory for the dormitory to collect the students to receive the greeting card and Apple, the monitor will be Christmas Apple and greeting card work arranged to two Class committee, you can continue to do other work. This is Niosocket,buffer is all the goods delivered, that is, apples and greeting cards in the example, and the channel is the passage of the goods, that is, the dormitory in the example, responsible for moving the gift back to their dorm, and the Life Committee and the organizing committee acted as the selector duty, Responsible for the sorting of gifts.
Serversocketchannel can be created with its own static factory method open, with each serversocketchannel corresponding to a serversocket (obtained by calling its socket ()), If you use the obtained ServerSocket directly to listen to the request, it is still normal serversocket, and the Niosocket is implemented by the ServerSocket binding port number that is obtained. Serversocketchannel can use the Configureblocking method to set whether blocking mode is used, and if it is set to non-blocking mode, it can be called Register method registration Selector.
Selector can be created by its static factory method open, and then registered to Serversocketchannel or Socketchannel via the channel's Register method. After the registration is complete, selector can wait for the request through the Select method, which has a long type parameter, represents the maximum wait time, and if the request receives the corresponding operation within that time, returns the number of requests that can be processed, or returns 0 after the timeout, If an overloaded method with parameters of 0 or no parameters is passed in, the Select method uses blocking mode to know the occurrence of a corresponding action request. When a request is received, selector calls the Selectdkeys method to return the Selectionkey collection.
Selectionkey saves the channel and selector that handle the current request, and provides different types of operations. The channel can select a specific operation (request operation, connection operation, read operation, write operation) by registering the selector with the second parameter of register, and only the corresponding operation selector in register will be concerned with the request of the corresponding type operation.
Introduction of so many estimates are annoying, we will take a look at the server-side niosocket processing process it:
- Create a Serversocketchannel and set the appropriate port number, whether it is blocking mode
- Create selector and register to Serversocketchannel
- Call Selector's Selector method to wait for the request
- Selector returns the Selectionkey collection using Selectdkeys after receiving the request
- Use Selectionkey to obtain channel, selector, and operation types and perform specific operations.
Public classNioserver { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub Try { //Create Serversocketchannel, listen on port 8080Serversocketchannel SSC =Serversocketchannel.open (); Ssc.socket (). Bind (NewInetsocketaddress (8080)); //set to non-blocking modeSsc.configureblocking (false); //registering selectors for SSCSelector Selector =Selector.open (); Ssc.register (selector, selectionkey.op_accept); //Creating the ProcessorHandler Handler =NewHandler (1024); while(true){ //waits for a request, each time it waits for 3s, the thread continues to run down after 3s, and is blocked if it passes in 0 or does not pass in Parameters if(Selector.select (3000) = = 0) {System.out.println ("Wait for Request timeout----"); Continue; } System.out.println ("Processing Request----"); //get selectionkey of processingIterator<selectionkey> Keyiter =Selector.selectedkeys (). iterator (); while(Keyiter.hasnext ()) {Selectionkey key=Keyiter.next (); Try{ //When a connection request is received if(Key.isacceptable ()) {handler.handleaccept (key); } //Read Data if(Key.isreadable ()) {handler.handleread (key); } }Catch(IOException ex) {keyiter.remove (); Continue; } //after processing, remove the currently used key from the pending Selectionkey iteratorKeyiter.remove (); } } } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } Private Static classhandler{Private intbuffersize = 1024; PrivateString localcharset = "UTF-8"; PublicHandler (intbuffersize) { This. buffersize =buffersize; } Public voidHandleaccept (Selectionkey key)throwsioexception{Socketchannel SC=( (Serversocketchannel) Key.channel ()). Accept (); Sc.configureblocking (false); Sc.register (Key.selector (), Selectionkey.op_read, Bytebuffer.allocate (buffersize)); } Public voidHandleread (Selectionkey key)throwsioexception{//Get channelSocketchannel sc =(Socketchannel) Key.channel (); //get buffer and resetBytebuffer buffer =(Bytebuffer) key.attachment (); Buffer.clear (); //do not read the content is closed if(sc.read (buffer) = =-1) Sc.close (); Else{ //convert buffer to read StateBuffer.flip (); //The value received in buffer is encoded in localcharset format and saved to receivedstringString receivedstring =charset.forname (Localcharset). Newdecoder (). Decode (buffer). toString (); System.out.println ("Received from client:" +receivedstring); //returning data to the clientString sendstring = "This data was from Server"; Buffer=Bytebuffer.wrap (Sendstring.getbytes (Localcharset)); Sc.write (buffer); Sc.close (); } } }}
View Code
The client code is the same as the normal socket, socket socket = new Socket ("192.168.6.42", 8080), and the server-side connection is established to execute the server-side handleaccept () method, Register selector with Serversocketchannel and add selectionkey.op_read parameter to indicate selector care Read method. The client then sends the content to the server side via Printwrite, the server executes the Handleread method to process the received content, and returns the result to the client, the client receives the data through BufferedReader, and finally closes the connection.
The use of sockets in Java