Java NIO SocketChannel Tutorial

Java NIO SocketChannel Tutorial

SocketChannel is a selectable channel belonging to the java.nio.channels package and is used for reading/writing stream oriented data and using TCP connection based protocol. The SocketChannel class provides input and output from a network.

From the Javadocs, you can see that SocketChannel implements both ScatteringByteChannel and GatheringByteChannel. More on that, in a separate post.

Topics Covered in this Tutorial

  1. Opening a SocketChannel
  2. Reading Data from a SocketChannel
  3. Writing Data to a SocketChannel
  4. Closing a SocketChannel
  5. Check if Connected

Opening a SocketChannel

To connect to a remote URL, you create a java.net.SocketAddress instance object and use that object in the open() method of the SocketChannel class.

SocketAddress address = new InetSocketAddress("https://www.avaldes.com", 80);
SocketChannel channel = SocketChannel.open(address);

Reading Data from a SocketChannel

When reading data from a SocketChannel, we read data into buffers. In this example, we create a ByteBuffer with a capacity of 100 bytes using the allocate() method. We then pass this buffer into the SocketChannel’s read() method.

ByteBuffer buffer= ByteBuffer.allocate(100);
int bytesRead = socketChannel.read(buffer);

Writing Data to a SocketChannel

This example reads data from a given buffer and writes a sequence of bytes to the socketChannel. In order to output data to the socketChannel we use the socketChannel’s write() method.

ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
buffer.put(msg.getBytes());
buffer.flip();
int bytesWritten = socketChannel.write(buffer);

Closing a SocketChannel

Closing a socket channel is simple, just use the close() method.

socketChannel.close();

Check if Connected

boolean isConnect = socketChannel.isConnected();

That’s It!

I hope you enjoyed this tutorial. It was certainly a lot of fun putting it together and testing it out. Please continue to share the love and like us so that we can continue bringing you quality tutorials. Happy Coding!!!

java_nio_socketchannel

Java NIO Related Tutorials

  • Java NIO Tutorial
    In this tutorial series we discuss the new features of Java NIO.
  • Java NIO Buffers
    This post covers NIO Buffers in more detail and provides practical examples of using buffers in real world applications.
  • Java NIO Channels
    This post covers NIO Channels in more detail and provides examples on network connections and Java I/O in relation to files.
  • Java NIO Selectors
    In this tutorial we learn how to use the Selector class from the java.io.channels package library to build High-Performance I/O client-server using NIO.
  • Java NIO File Channel
    In this tutorial we learn how to use the FileChannel class from the java.io.channels package library and provide working examples on all of the main methods.
  • Java NIO Socket Channel
    In this tutorial we learn how to use the SocketChannel and how it is used for reading/writing stream oriented data and using TCP connection based protocol.
  • Java NIO DatagramChannel Tutorial
    In this tutorial we learn how to use the DatagramChannel to allow developers to build high-performant data streaming applications that send and receive datagrams using a protocol called UDP.
  • Java NIO and NIO2 Path Tutorial
    This tutorial will introduce the Path interface and many of its methods. The Path interface was made available as part of the Java SE 7 release in the Java NIO 2 File API.

Please Share Us on Social Media

Facebooktwitterredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *