Mega Code Archive

 
Categories / Java Tutorial / Network
 

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