當前位置: 首頁>>代碼示例>>Java>>正文


Java AsynchronousSocketChannel.write方法代碼示例

本文整理匯總了Java中java.nio.channels.AsynchronousSocketChannel.write方法的典型用法代碼示例。如果您正苦於以下問題:Java AsynchronousSocketChannel.write方法的具體用法?Java AsynchronousSocketChannel.write怎麽用?Java AsynchronousSocketChannel.write使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.nio.channels.AsynchronousSocketChannel的用法示例。


在下文中一共展示了AsynchronousSocketChannel.write方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: sendByteBuffer

import java.nio.channels.AsynchronousSocketChannel; //導入方法依賴的package包/類
/**
 * 
 * @param byteBuffer
 * @param packetCount
 * @param packets
 *
 * @author: tanyaowu
 * @創建時間: 2017年2月4日 下午9:10:01
 *
 */
public void sendByteBuffer(ByteBuffer byteBuffer, Integer packetCount, Object packets)
{
	if (byteBuffer == null)
	{
		log.error("{},byteBuffer is null", channelContext);
		return;
	}

	if (!AioUtils.checkBeforeIO(channelContext))
	{
		return;
	}

	byteBuffer.flip();
	AsynchronousSocketChannel asynchronousSocketChannel = channelContext.getAsynchronousSocketChannel();
	WriteCompletionHandler<SessionContext, P, R> writeCompletionHandler = channelContext.getWriteCompletionHandler();
	try
	{
		writeCompletionHandler.getWriteSemaphore().acquire();
	} catch (InterruptedException e)
	{
		log.error(e.toString(), e);
	}
	asynchronousSocketChannel.write(byteBuffer, packets, writeCompletionHandler);
	channelContext.getStat().setLatestTimeOfSentPacket(SystemTimer.currentTimeMillis());
}
 
開發者ID:tywo45,項目名稱:talent-aio,代碼行數:37,代碼來源:SendRunnable.java

示例2: onOpen

import java.nio.channels.AsynchronousSocketChannel; //導入方法依賴的package包/類
public static void onOpen(AsynchronousSocketChannel asynchronousSocketChannel) throws InterruptedException, ExecutionException, TimeoutException, NoSuchAlgorithmException {
    String WebSocketsMagicString = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
    Properties properties = new Properties();
    StringBuilder sb = new StringBuilder();
    while (inputBuffer.hasRemaining()) {
        sb.append((char) (inputBuffer.get() & 0xff));
    }
    String[] lines = sb.toString().split("\\n");
    for (String line : lines) {
        String[] keyVal = line.split(":");
        if (keyVal.length == 2) {
            properties.put(keyVal[0].trim(), keyVal[1].trim());
        }
    }
    String message
            = "HTTP/1.1 101 Switching Protocols\r\n"
            + "Connection: Upgrade\r\n"
            + "Sec-WebSocket-Accept: " + Base64.encodeBytes(MessageDigest.getInstance("SHA1").digest((properties.getProperty("Sec-WebSocket-Key") + WebSocketsMagicString).getBytes())) + "\r\n"
            + "Upgrade: websocket\r\n"
            + "\r\n";
    outputBuffer = ByteBuffer.allocate(message.getBytes().length);
    outputBuffer.put(message.getBytes());
    outputBuffer.flip();
    while (outputBuffer.hasRemaining()) {
        asynchronousSocketChannel.write(outputBuffer);
    }
    outputBuffer = encodeUnmaskedFrame(1, "Connection Established");
    outputBuffer.flip();
    outputBuffer.rewind();
    while (outputBuffer.hasRemaining()) {
        asynchronousSocketChannel.write(outputBuffer);
    }
}
 
開發者ID:ThreaT,項目名稱:WebServers,代碼行數:34,代碼來源:WebSocketServer.java

示例3: onMessage

import java.nio.channels.AsynchronousSocketChannel; //導入方法依賴的package包/類
public static void onMessage(AsynchronousSocketChannel asynchronousSocketChannel) throws Exception {
    inputBuffer.flip();
    inputBuffer.rewind();
    String message = decodeMaskedFrame(inputBuffer);
    outputBuffer = encodeUnmaskedFrame(1, message);
    outputBuffer.flip();
    outputBuffer.rewind();
    while (outputBuffer.hasRemaining()) {
        asynchronousSocketChannel.write(outputBuffer);
    }
}
 
開發者ID:ThreaT,項目名稱:WebServers,代碼行數:12,代碼來源:WebSocketServer.java

示例4: onClose

import java.nio.channels.AsynchronousSocketChannel; //導入方法依賴的package包/類
public static void onClose(AsynchronousSocketChannel asynchronousSocketChannel) throws IOException {
    outputBuffer = encodeUnmaskedFrame(1, "Connection Closed");
    outputBuffer.flip();
    outputBuffer.rewind();
    while (outputBuffer.hasRemaining()) {
        asynchronousSocketChannel.write(outputBuffer);
    }
    ByteBuffer closeBuffer = encodeUnmaskedFrame(8, "");
    closeBuffer.flip();
    while (closeBuffer.hasRemaining()) {
        asynchronousSocketChannel.write(closeBuffer);
    }
    connectionOpen = false;
    asynchronousSocketChannel.close();
}
 
開發者ID:ThreaT,項目名稱:WebServers,代碼行數:16,代碼來源:WebSocketServer.java

示例5: write

import java.nio.channels.AsynchronousSocketChannel; //導入方法依賴的package包/類
private static void write(AsynchronousSocketChannel asynchronousSocketChannel) throws IOException {
    asynchronousSocketChannel.write(outputBuffer);
    outputBuffer.clear();
    CharBuffer charBuffer = CharBuffer.allocate(1024);
    for (;;) {
        try {
            charBuffer.put("HTTP/1.0 ").put("200 OK").put("\r\n");
            charBuffer.put("Server: niossl/0.1").put("\r\n");
            charBuffer.put("Content-type: ").put("text/html; charset=iso-8859-1").put("\r\n");
            charBuffer.put("Content-length: ").put("31").put("\r\n");
            charBuffer.put("\r\n");
            charBuffer.put(request);
            charBuffer.put("<html><head><title>HttpsServer</title></head><body><h3>HelloWorld!</h3></body></html>");
            break;
        } catch (BufferOverflowException x) {
            charBuffer = CharBuffer.allocate(charBuffer.capacity() * 2);
        }
    }
    charBuffer.flip();
    SSLEngineResult sslEngineResult = sslEngine.wrap(ascii.encode(charBuffer), outputBuffer);
    outputBuffer.flip();
    switch (sslEngineResult.getStatus()) {
        case OK:
            if (sslEngineResult.getHandshakeStatus() == SSLEngineResult.HandshakeStatus.NEED_TASK) {
                Runnable runnable;
                while ((runnable = sslEngine.getDelegatedTask()) != null) {
                    runnable.run();
                }
            }
            break;
    }
    if (outputBuffer.hasRemaining()) {
        asynchronousSocketChannel.write(outputBuffer);
    }
}
 
開發者ID:ThreaT,項目名稱:WebServers,代碼行數:36,代碼來源:WebSocketSecureServer.java

示例6: main

import java.nio.channels.AsynchronousSocketChannel; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
        AsynchronousSocketChannel sc = AsynchronousSocketChannel.open();
        System.out.println(sc.getClass());
        Future connected = sc.connect(new InetSocketAddress("localhost", 9999));
// later ensure we are connected.
        connected.get();

        ByteBuffer bb = ByteBuffer.allocateDirect(4096);
// populate bb
        Future<Integer> writeFuture = sc.write(bb);
    }
 
開發者ID:lifey,項目名稱:compfut,代碼行數:12,代碼來源:AsyncChannel.java

示例7: writeStuffThreadAsync

import java.nio.channels.AsynchronousSocketChannel; //導入方法依賴的package包/類
private void writeStuffThreadAsync(
		AsynchronousServerSocketChannel socket, AsynchronousSocketChannel ch, int byteCount, int countDown) {
	ByteBuffer buf = ByteBuffer.wrap("Fountain water!\n".getBytes());
	ch.write(buf, 0, writeCompletionHandler(socket, ch, byteCount, buf, countDown));
}
 
開發者ID:arienkock,項目名稱:parallelism-benchmarks,代碼行數:6,代碼來源:FountainSocketBenchmark.java


注:本文中的java.nio.channels.AsynchronousSocketChannel.write方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。