Writing to a SocketChannel : NIO Socket « Network Protocol « Java






Writing to a SocketChannel

 
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);
  }
}

   
  








Related examples in the same category

1.Non block server
2.Finger Server
3.Reading from a SocketChannel
4.Creating a Non-Blocking Server Socket
5.Creating a Non-Blocking Socket: requires a socket channel.
6.Accepting a Connection on a ServerSocketChannel
7.Using a Selector to Manage Non-Blocking Server Sockets