The difference between channel and stream:
- The stream is byte-based and read-write is unidirectional.
- The channel is based on fast buffer and can be read and written asynchronously. In addition to FileChannel are two-way.
Key implementations of the channel:
- FileChannel
- DATAGRAMCHANNEL:UDP Reading and writing
- Socketchannel:tcp Reading and writing
- Serversocketchannel
Supports Scatter/gather (scatter and gather)
- Dispersion (scatter) reads from the channel means that the data read is written to multiple buffer during a read operation. As a result, the channel will "scatter" (scatter) the data read from the channel into multiple buffer.
Aggregation (gather) writes a channel that writes multiple buffer data to the same channel during a write operation, so that the channel sends the data in multiple buffer "clustered (gather)" to the channel.
Bytebuffer Header = bytebuffer.allocate (+); Bytebuffer body = bytebuffer.allocate (1024x768= {header, Body};channel.read (Bufferarray);
Creation of the channel:
In addition to FileChannel, all are created using open.
The FileChannel is created as follows:
New Randomaccessfile ("Data/nio-data.txt", "rw"= Afile.getchannel ();
Socket Channel
A peer socket object is created when the socket object ServerSocket, socket, and Datagramsocket;socket channels in the java.net package are instantiated respectively.
The socket channel can run non-blocking mode and is selectable, non-blocking I/O is closely linked to selectivity, which is why the managed blocking API is defined in Selectablechannel. Setting non-blocking is very simple, as long as you call the Configureblocking (False) method. If you need to change the blocking mode halfway, you must first obtain the lock of the object returned by the Blockinglock () method.
Serversocketchannel instances:
1. Use a peer socket to bind the listener.
2. In a non-blocking state, accept returns null immediately on no connection
Bytebuffer buffer = bytebuffer.wrap ("Hello World". GetBytes ()); Serversocketchannel SSC=Serversocketchannel.open (); Ssc.socket (). Bind (NewInetsocketaddress (12345)); Ssc.configureblocking (false); for (;;) {System.out.println ("Waiting for connections"); Socketchannel SC=ssc.accept (); if(sc = =NULL) TimeUnit.SECONDS.sleep (2000); Else{System.out.println ("Incoming connection from:" +Sc.socket (). getremotesocketaddress ()); Buffer.rewind (); Sc.write (buffer); Sc.close (); }}
- Socketchannel
As opposed to Serversocketchannel, it acts as a client, initiates a connection to the listener server, and begins to receive data after the connection is successful.
The open () method that calls it is just open but not connected, the connection needs to be made immediately after the Connect () method is called, or the open (socketaddress remote) method can be called in two steps.
You will find that the Connect () method does not provide the Timout parameter, and as an alternative, you can use the isconnected (), isconnectpending (), or Finishconnect () method to check the status of the connection.
- 1. IsConnected Determine if it has been established
- 2. Finishconnect blocking on non-blocking sockets waiting for connection to succeed
Non-blocking read immediately returns 0 on countless data
- Datagramchannel
It is not connected, it can be either a server or a client.
Java NIO Channel Channels