Writing to a SocketChannel : SocketChannel « Network « Java Tutorial






import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class Main {
  public static void main(String[] argv) throws Exception {
    SocketChannel sChannel = SocketChannel.open();
    sChannel.configureBlocking(false);
    sChannel.connect(new InetSocketAddress("hostName", 12345));

    ByteBuffer buf = ByteBuffer.allocateDirect(1024);
    buf.put((byte) 0xFF);

    buf.flip();

    int numBytesWritten = sChannel.write(buf);
  }
}








19.13.SocketChannel
19.13.1.Create SocketChannel from IP address
19.13.2.Reading from a SocketChannel
19.13.3.Writing to a SocketChannel
19.13.4.Channel selector
19.13.5.Use SocketChannel to get Web page
19.13.6.Demonstrate asynchronous connection of a SocketChannel
19.13.7.Creating a Non-Blocking Socket: requires a socket channel.