Java NIO (v) Socket channel

Source: Internet
Author: User

Socket Channel

The socket channel and file channel have different characteristics:

    • The socket channel class can run in nonblocking mode and is optional. These two characteristics can activate large programs (such as network services and middleware components) with great scalability and flexibility, so there is no need to add a thread to each socket connection. This feature avoids the overall context-switching overhead required to manage a large number of threads, and with NIO, one or several threads can manage hundreds or thousands of socket connections, with little or no loss of performance
    • All socket channel classes (SOCKETCHANNEL,SERVERSOCKETCHANNEL,DATAGRAMCHANNEL) will create a corresponding socket object, such as Socket,serversocket, when instantiated. Datagramsocket, these sockets can be obtained by invoking the socket () method of the corresponding channel class. In addition, these three sockets have the Getchannel () method
    • Each socket channel has an associated Java.net.socket object, which is not the case. If you create a socket object using traditional (direct instantiation), it does not have a socket channel associated with it, and its getchannel () method always returns null
Open Socketchannel
Socketchannel channel = socketchannel.open (); Channel.connect (new inetsocketaddress ("Http://xxx, com ", 80));
Close Socketchannel
Channel.close ();
Read data from Socketchannel
Bytebuffer buf = bytebuffer.allocate (+); int bytr = Channel.read (BUF);

The method reads the data from Socketchannel to buffer, and the return value of the Read () method indicates how many bytes were read into buffer, indicating that the end of the stream was read when the return value was 1

Write Socketchannel
1 String str = "some thing"; 2 Bytebuffer buf = bytebuffer.allocate (+); 3 Buf.put (Str.getbytes ()); 4 Buf.flip (); 5  while (buf.hasremaining) {6  socketchannel.write (BUF);       7 }8 socketchannel.close ();

Note: The call to the Socketchannel.write () method is in a while loop. The Write () method does not guarantee how many bytes can be written to Socketchannel. So, we repeatedly call write () until buffer has no bytes to write.

Non-blocking mode

To place a socket channel in nonblocking mode, rely on the parent class Selectablechannel:

1  Public Abstract classSelectablechannelextendsAbstractinterruptiblechannelImplementsChannel {2     ...3      Public Abstract voidConfigureblocking (BooleanBlockthrowsIOException;4      Public Abstract Booleanisblocking ();5      Public AbstractObject Blocknglock ();6     ...7}

As can be seen from the API of Selectablechannel, setting or resetting the blocking mode of a channel is simple, as long as the Configureblocking () method is called, the pass parameter value is set to block mode. If the parameter value is false, it is non-blocking mode. Also, call the Isblocking () method to determine if it is blocked. The Blockinglock () method returns an opaque object reference that is used internally when the channel implementation modifies blocking mode, and only the thread that owns the lock on the object can change the blocking mode of the channel. This method is convenient for ensuring that the blocking mode of the socket channel does not change and that the blocking mode is temporarily changed without affecting other threads while executing the critical part of the code.

Connect () method

The non-blocking mode is lowered with the Connect () method, which may return before the connection is established, in order to determine whether the connection is established, call the Finishconnect () method

Socketchannel.configureblocking (false); Socketchannel.connect (new inetsocketaddress ( " http://xxx.com "  the ));  while (!socketchannel.finishconnect ()) {  //Some codes  if no connection is successful ...}

Write () method

In non-blocking mode, the write () method may return if nothing has been written. So you need to call write () in the loop

Read () method

In non-blocking mode, the read () method may return when no data has been read. So pay attention to its int return value, which will tell you how many bytes were read

Add: nonblocking mode works better with selectors, by registering one or more socketchannel to selector, you can ask the selector which channel is ready to read, write, etc.

Serversocketchannel

The Serversocketchannel in Java NiO is a channel that can listen for incoming TCP connections, just like serversocket in standard IO. Serversocketchannel class in Java.nio.channels package

Serversocketchannel Serverchannel = serversocketchannel.open (); Serverchannel.socket (). Bind (new Inetsocketaddress (9999));  while (true) {  = serverchannel.accept ();  }

Open Serversocketchannel

Serversocketchannel Serverchannel = Serversocketchannel.open ();

Close Serversocketchannel

Serverchannel.close ();

Listening for new Incoming connections

The new incoming connection is monitored by the serversocketchannel.accept () method. The Accept () method returns a Socketchannel that contains the new incoming connection, so the Accept () method blocks until a new connection arrives

Typically, you don't just listen to a single connection, call the Accept () method in the while loop

 while (true) {  = serversocketchannel.accept ();  }

Non-blocking mode

Serversocketchannel can be set to non-blocking mode. In nonblocking mode, the Accept () method returns immediately, or null if no new incoming connection has been entered. Therefore, it is necessary to check if the returned socketchannel is null

Serversocketchannel Serverchannel = serversocketchannel.open (); Serverchannel.socket (). Bind (new Inetsocketaddress (9999));  while (true) {  = serverchannel.accept ();     if NULL ) {      //Some codes    }    }

Service-side programs for socket channels

 Public classSocketserver { Public Static voidMain (string[] args)throwsException {intPort = 1234;Serversocketchannel SSC=Serversocketchannel.open (); Ssc.configureblocking (false); Non-blocking mode ServerSocket SS=Ssc.socket (); Ss.bind (Newinetsocketaddress (port));  while(true) {Socketchannel sc=ssc.accept (); if(sc = =NULL)             {                 //If there is currently no data, wait 1 seconds for the data to be polled again, you can use selector here after learning selectorThread.Sleep (1000); }            Else{bytebuffer bb= Bytebuffer.allocate (100);                Sc.read (BB);                 Bb.flip ();  while(Bb.hasremaining ()) {System.out.print (Char) Bb.get ());                } sc.close (); System.exit (0); }        }    }}

Client program for Socket channel

 Public classnonblockingsocketclient {Private Static FinalString STR = "Hello world!"; Private Static FinalString remote_ip= "127.0.0.1";  Public Static voidMain (string[] args)throwsException {intPort = 1234;Socketchannel SC=Socketchannel.open (); Sc.configureblocking (false); Non-blocking mode Sc.connect (Newinetsocketaddress (REMOTE_IP, Port));  while(!Sc.finishconnect ()) {System.out.println ("with" + remote_ip+ connection is being established, please wait a moment! "); Thread.Sleep (10); } System.out.println ("The connection is established, the content to be written to the specified ip+ port!" Time is "+System.currenttimemillis ()); Bytebuffer BB=bytebuffer.allocate (Str.length ());        Bb.put (Str.getbytes ()); Bb.flip (); //Be sure to reverse the data before writing the buffer (flip)Sc.write (BB);        Bb.clear ();     Sc.close (); } }

Datagramchannel

The Datagramchannel in Java NiO is a channel that can send and receive UDP packets. Because UDP is a non-connected network protocol, it cannot be read and written like other channels. It sends and receives a packet.

Open Datagramchannel

Datagramchannel Datagramchannel = datagramchannel.open ();d atagramchannel.socket (). Bind (new Inetsocketaddress (8089));

Receive () method

Bytebuffer buf = bytebuffer.allocate (+);d atagramchannel.receive (BUF);

The receive () method copies the received packet contents to the specified buffer. If buffer does not tolerate the data received, the extra data will be discarded

Send () method

String str = "Some codes"= bytebuffer.allocate (+); Buf.put (Str.getbytes ()); Buf.flip (); int New Inetsocketaddress ("xxx.com", 80));

This example sends a string of characters to UDP port 80 of the "xxx.com" server. Because the server does not monitor this port, nothing happens. Nor does it inform you if the packets you sent are received, because UDP has no guarantee of data transfer

Connect to feature Address

Datagramchannel.connect ("xxx.com", 80);

Datagramchannel can be "connected" to a specific address in the network. Because UDP is not connected, connecting to a specific address does not create a true connection like a TCP channel. Instead, lock the datagramchannel so that it can send and receive data from a specific address

When connected, you can also use the read () and write () methods, just as you would with a traditional channel. It's just that there's no guarantee of data transmission.

int bytesread = datagramchannel.read (buf); int Byteswritten = datagramchannel.write (But);

Java NIO (v) Socket channel

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.