package fr.upem.tcp;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;

public class ServerBizarre {

	public static void main(String[] args) throws IOException {

		ServerSocketChannel ss = ServerSocketChannel.open();
		ss.bind(new InetSocketAddress(7777));
		SocketChannel s = ss.accept();
		Charset utf8 = Charset.forName("UTF-8");
		CharsetDecoder dec = utf8.newDecoder();
		dec.onMalformedInput(CodingErrorAction.REPLACE);
		dec.replaceWith("*");
		ByteBuffer bb = ByteBuffer.allocate(1024);
		int lu;
	    while ((lu = s.read(bb)) != -1) {
	    	bb.flip();
            System.out.print(dec.decode(bb).toString());
            bb.clear();
	    }
		s.close();
	}

}
