Skip to content
Snippets Groups Projects
Commit 6062a491 authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

Server

parent 4126e3d7
No related merge requests found
Pipeline #113 failed with stages
in 0 seconds
......@@ -35,6 +35,17 @@ public class IndexReader {
}
public byte[] readData(int index) {
try (FileChannel fc = FileChannel.open(getFile())) {
return getData(fc, index);
}
catch (IOException exc) {
log.error("read", exc);
return null;
}
}
private Path getFile() throws IOException {
Path LocalFile = Files.createTempFile(null, null).getParent().resolve(
IMAGE_NAME);
......
package koz01.java2.lab08;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import lombok.extern.log4j.Log4j2;
@Log4j2
public class RemoteIndexServer {
public static void main(String[] args) throws IOException {
Selector selector = Selector.open();
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.bind(new InetSocketAddress(Integer.parseInt(args[0])));
ssc.configureBlocking(false);
ssc.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
int numberOfKeys = selector.select();
if (numberOfKeys == 0) {
continue;
}
Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
while (keys.hasNext()) {
SelectionKey sk = keys.next();
if (sk.channel() == ssc) {
SocketChannel sch = ssc.accept();
log.info("client connected {}", sch.getRemoteAddress());
// TODO register sch
}
else {
SocketChannel sc = (SocketChannel) sk.channel();
int index = 0;// TODO get index of image
byte []data = new IndexReader().readData(index);
// TODO send data.lenght and data
}
keys.remove();
}
}
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment