Java Tutorial/Network/Buffer Socket — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 08:03, 1 июня 2010
Use ByteBuffer for Socket communication
<source lang="java">
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(); } }
}</source>