A Java NIO Socketchannel is a channel it connected to a TCP network socket. There are two ways a socketchannel can be created:you open a socketchannel and connect to a server somewhere on the Inter Net. A socketchannel can be created as a incoming connection arrives at a serversocketchannel.
Opening a Socketchannel
Here are how open a socketchannel:
Socketchannel Socketchannel = Socketchannel.open ();
Socketchannel.connect (New Inetsocketaddress ("http://jenkov.com", 80));
Closing a Socketchannel
You are close a socketchannel the calling of the Socketchannel.close () method. This is the done by:
Socketchannel.close ();
Reading from a socketchannel
To read data from a socketchannel and call one of the Read () methods. This is example:
Bytebuffer buf = Bytebuffer.allocate (in);
int bytesread = Socketchannel.read (BUF);
The-A-a-Buffer is allocated. The data read from the Socketchannel was read into the Buffer.
Second the Socketchannel.read () is called. This is the reads data from the Socketchannel to the Buffer. The int returned by the "read () method" tells how many bytes were into the Buffer. If-1 is returned, the End-of-stream are reached (the connection is closed).
writing to a Socketchannel
Writing data to a socketchannel are done using the Socketchannel.write () method, which takes a Buffer as parameter. This is example:
String NewData = "New String to write to file ..." + System.currenttimemillis ();
Bytebuffer buf = Bytebuffer.allocate (in);
Buf.clear ();
Buf.put (Newdata.getbytes ());
Buf.flip ();
while (Buf.hasremaining ()) {
channel.write (BUF);
}
Notice how the Socketchannel.write () is called inside a while-loop. There is no guarantee the how many bytes the write () method writes to the Socketchannel. Therefore we repeat the write () call until the Buffer has no further to write.
non-blocking Mode
can set a socketchannel into non-blocking mode. You can call connect (), read () and write () in asynchronous mode.
Connect ()
If the Socketchannel is in non-blocking mode, and your call Connect (), the "may" return before a connection is Establi Shed. To determine whether the connection are established, can call the Finishconnect () method, like this:
Socketchannel.configureblocking (false);
Socketchannel.connect (New Inetsocketaddress ("http://jenkov.com");
while (! Socketchannel.finishconnect ()) {
//wait, or do something else ...
}
Write ()
In non-blocking mode the "write () method" may return without has written anything. Therefore you need to call the Write () method in a loop. But, since this are already being done with the previous write examples, no need to did anything here.
Read ()
In non-blocking mode the "read () method" may return without has read any data at all. Therefore you need to pay attention to the returned int, which tells how many, bytes were read.
non-blocking Mode with selectors
The non-blocking mode of Socketchannel is much better with Selector ' s. By registering one or more Socketchannel ' s with a Selector, you can ask the Selector for channels this are ready for Readi Ng, writing etc. How to use the Selector ' s with Socketchannel ' are explained in the more detail in a later text at this tutorial.