Java by API/java.nio.channels/SocketChannel — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 17:43, 31 мая 2010
SocketChannel: connect(SocketAddress remote) throws IOException
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
public class Main {
public static void main(String[] argv) throws Exception {
ByteBuffer buf = ByteBuffer.allocateDirect(1024);
SocketChannel sChannel = SocketChannel.open();
sChannel.configureBlocking(false);
sChannel.connect(new InetSocketAddress("hostName", 12345));
int numBytesRead = sChannel.read(buf);
if (numBytesRead == -1) {
sChannel.close();
} else {
buf.flip();
}
}
}
SocketChannel: read(ByteBuffer dst)
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.util.Iterator;
import java.util.Set;
public class MainClass {
public static void main(String[] args) throws IOException {
Charset charset = Charset.forName("ISO-8859-1");
CharsetEncoder encoder = charset.newEncoder();
CharsetDecoder decoder = charset.newDecoder();
ByteBuffer buffer = ByteBuffer.allocate(512);
Selector selector = Selector.open();
ServerSocketChannel server = ServerSocketChannel.open();
server.socket().bind(new java.net.InetSocketAddress(8000));
server.configureBlocking(false);
SelectionKey serverkey = server.register(selector, SelectionKey.OP_ACCEPT);
for (;;) {
selector.select();
Set keys = selector.selectedKeys();
for (Iterator i = keys.iterator(); i.hasNext();) {
SelectionKey key = (SelectionKey) i.next();
i.remove();
if (key == serverkey) {
if (key.isAcceptable()) {
SocketChannel client = server.accept();
client.configureBlocking(false);
SelectionKey clientkey = client.register(selector, SelectionKey.OP_READ);
clientkey.attach(new Integer(0));
}
} else {
SocketChannel client = (SocketChannel) key.channel();
if (!key.isReadable())
continue;
int bytesread = client.read(buffer);
if (bytesread == -1) {
key.cancel();
client.close();
continue;
}
buffer.flip();
String request = decoder.decode(buffer).toString();
buffer.clear();
if (request.trim().equals("quit")) {
client.write(encoder.encode(CharBuffer.wrap("Bye.")));
key.cancel();
client.close();
} else {
int num = ((Integer) key.attachment()).intValue();
String response = num + ": " + request.toUpperCase();
client.write(encoder.encode(CharBuffer.wrap(response)));
key.attach(new Integer(num + 1));
}
}
}
}
}
}
SocketChannel: write(ByteBuffer src)
import java.nio.CharBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
public class MainClass {
public static void main(String[] args) throws Exception{
CharsetEncoder encoder = Charset.forName("US-ASCII").newEncoder();
ServerSocketChannel server = ServerSocketChannel.open();
server.socket().bind(new java.net.InetSocketAddress(8000));
for (;;) { // This server runs forever
SocketChannel client = server.accept();
String response = new java.util.Date().toString() + "\r\n";
client.write(encoder.encode(CharBuffer.wrap(response)));
client.close();
}
}
}