NIO Socket Sample

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

    private InetSocketAddress address = new InetSocketAddress("***.***.***.***",9100);
    private SocketChannel client = null;

        client=SocketChannel.open();
            client.connect(address);           
            client.configureBlocking(false);//非阻塞
            ByteBuffer ByteBufferloginData = ByteBuffer.wrap(LoginData);
            ByteBuffer ByteBufferCommData = ByteBuffer.wrap(CommunicationData);           
            client.write(ByteBufferloginData);
            client.write(ByteBufferCommData);
            int nBytes = 0;
            ByteBuffer buf = ByteBuffer.allocate(2048);
            while ((nBytes = client.read(buf)) > 0)
            {
                buf.flip();
                out.println(buf);
                buf.flip();
            }
            return null;

你可能感兴趣的:(socket)