send string to SocketChannel - Java java.nio.channels

Java examples for java.nio.channels:SocketChannel

Description

send string to SocketChannel

Demo Code


//package com.java2s;

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

public class Main {
    /**/*from   www  .j  ava  2 s  . co  m*/
     * send str
     * 
     * @param channel
     * @param str
     * @throws Exception
     */
    public static void write(SocketChannel channel, String str)
            throws Exception {
        channel.write(ByteBuffer.wrap(str.getBytes()));
    }

    /**
     * send str
     * 
     * @param channel
     * @param contents
     * @throws Exception
     */
    public static void write(SocketChannel channel, byte[] contents)
            throws Exception {
        channel.write(ByteBuffer.wrap(contents));
    }
}

Related Tutorials