Skip to content

Instantly share code, notes, and snippets.

@jartur
Created February 5, 2011 10:27
Show Gist options
  • Save jartur/812352 to your computer and use it in GitHub Desktop.
Save jartur/812352 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client {
public static void main(String[] argv)
{
PrintWriter out;
BufferedReader in;
try
{
Socket sock = new Socket("localhost", 1843);
sock.setSoTimeout(20000);
out = new PrintWriter(sock.getOutputStream(), true);
in =new BufferedReader(new InputStreamReader(sock.getInputStream()));
out.print("<policy-file-request/>\0");
out.flush();
String r;
while((r = in.readLine()) != null)
{
System.out.println(r);
}
out.close();
in.close();
sock.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.*;
import org.jboss.netty.channel.socket.SocketChannelConfig;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
import org.jboss.netty.handler.codec.frame.Delimiters;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
import org.jboss.netty.handler.timeout.ReadTimeoutHandler;
import org.jboss.netty.util.CharsetUtil;
import org.jboss.netty.util.HashedWheelTimer;
import org.jboss.netty.util.Timer;
import java.net.InetSocketAddress;
import java.util.concurrent.*;
import static org.jboss.netty.channel.Channels.pipeline;
public class Server {
static final Timer timer = new HashedWheelTimer(100, TimeUnit.MILLISECONDS, 256);
public static final String POLICY_REQUEST = "<policy-file-request/>";
public static final String POLICY_XML =
"<?xml version=\"1.0\"?>" +
"<!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\">\n" +
"<cross-domain-policy>\n" +
" <site-control permitted-cross-domain-policies=\"master-only\"/>\n" +
" <allow-access-from domain=\"*\" to-ports=\"80,2000-2100,20010\" />\n" +
"</cross-domain-policy>";
public static final ExecutorService ex = new ThreadPoolExecutor(100, 2000, 10, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(true));
public static void main(String[] argv) throws Exception
{
ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception
{
ChannelPipeline pipeline = pipeline();
pipeline.addLast("timer", new ReadTimeoutHandler(timer, 10));
pipeline.addLast("frameDecoder", new DelimiterBasedFrameDecoder(1024, Delimiters.nulDelimiter()));
pipeline.addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast("main", new SimpleChannelHandler() {
@Override
public void channelConnected(ChannelHandlerContext ctx, final ChannelStateEvent e) throws Exception
{
SocketChannelConfig cc = (SocketChannelConfig) e.getChannel().getConfig();
cc.setTcpNoDelay(true);
}
@Override
public void messageReceived(ChannelHandlerContext ctx, final MessageEvent e) throws Exception
{
String msg = (String) e.getMessage();
if (msg.equalsIgnoreCase(POLICY_REQUEST))
{
ex.execute(new Runnable() {
public void run()
{
e.getChannel().write(POLICY_XML + "\0");
}
});
}
else
{
e.getChannel().close();
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception
{
e.getChannel().close();
}
});
pipeline.addLast("stringEncoder", new StringEncoder(CharsetUtil.UTF_8));
return pipeline;
}
});
bootstrap.setOption("reuseAddress", true);
bootstrap.setOption("child.reuseAddress", true);
bootstrap.bind(new InetSocketAddress(1843));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment