| |
19. 11. 1. 使用ByteBuffer的Socket通信 |
|
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.SocketChannel;
import java.nio.channels.WritableByteChannel;
public class MainClass {
public static void main(String[] args) throws Exception {
int port = 19;
SocketAddress address = new InetSocketAddress("127.0.0.1", port);
SocketChannel client = SocketChannel.open(address);
ByteBuffer buffer = ByteBuffer.allocate(74);
WritableByteChannel out = Channels.newChannel(System.out);
while (client.read(buffer) != -1) {
buffer.flip();
out.write(buffer);
buffer.clear();
}
}
}
|
|
19. 11. 套接字缓冲区 | | 19. 11. 1. | 使用ByteBuffer的Socket通信 | | |
|