Documente Academic
Documente Profesional
Documente Cultură
ServerSocketChannel)
Opciones Configurables:
Constructor
Protected ServerSocketChannel(SerlectorProvider p)
Mtodos
It.remove()
If(h.isAcceptable()){
SocketChannel cl=s.accept();
System.out.println(Cliente conectado
desde+cl.socket().getInetAddress()
+;+cl.socket().getPort());
cl.configureBlocking(false);
cl..register(sel,SelectionKey.OP_READ|
SelectomKet.OP_WRITE)
continue;
}
If(k.isReadable()){
SocketChannel ch=(SocketChannel)k.channel();
->Referencia del socket
ByteBuffer b=ByteBuffer.allocateDirect(2000);
Int n= ch.read(b);
System.out.println(Se leyeron+n+bytes);
b.flip();
while(b.hasNext()){
System.out.println(b.get());
}
continue;
}else if(k.isWritable()){
SocketChanlle ch=(SocketChannel)k.channel();
String msj=Un mensaje;
ByteBuffer b=ByteBuffer.wrap(msj.getBytes());
ch.write(b);
continue;
//Enviar un entero,flotante,doble
ByteBuffer b=ByteBuffer.allocate(4);
b.putInt(x);
b.flip;
//Enviar un objeto
Objeto o=nre Objeto(.)
ByteArrayOutPutStream baos=new
ByteArrayOutputStream();
ObjectOutputStream oos=new
ObjectOutputStream(baos);
oss.writeObject(o);
oss.flush();
byte[]buf=baos.toByteArray();
ByteBuffer b=ByteBuffer.wrap(buf);
ch.write(b);
}
}
}
}catch(Exception e){
e.printStackTrace();
}
Clase SocketChannel(java.nio.channels.SocketChannel)
Opciones Configurables:
SO_SNDBUF
SO_RCVBUF
SO_KEEPALIVE
SO_REUSADOR
SO_LINGER
TCP_NODELAY
Constructor
Protected SocketChannel(SelectorProvider p)
Metodos
System.out.println(Conexin establecida);
ch.register(sel,SelectionKey.OP_READ|
SelectionKey.OP_WRITE);
continue;
}
//if(b.hasArray()){
ObjectInputStream ois=ObjectInputStream(new
ByteArrayInputStream(b.array()));
Objetot o=(Objeto1)ois.readObject();
Buffers
AllocateDirectr(Datos en ram continuos ->Mayor velocidad)
Allocate(Datos en ram discontinuos ->Menos velocidad)
Caracteristicas:
Position=0
Mark=nulo->Genera marcas con mtodo mark que sirven para
identificar posiciones
Limit=ultima posicion
Capacity=cantidad del buffer
b.limit(b.position());
b.position(0);
b.flip()->Posiciona limit a position, y position a 0
Metodos:
PutTipo();
GetTipo();
Metodo wrap utiliza
.allocate()
.put()
.flip()