import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; public class Gateway implements Runnable { public boolean isActive; public void isActive(boolean value) { this.isActive = value; } public boolean isActive() { return this.isActive; } public static void main(String[] args) { try { new Gateway().start(); } catch (java.io.IOException e) { e.printStackTrace(); } } public SocketChannel channel; public String target = "irc.kingskrown.com"; public int port = 6667; public void start() throws java.io.IOException { System.out.println("Opening SocketChannel."); channel = SocketChannel.open(); System.out.println("Configuring blocking to false."); channel.configureBlocking(false); System.out.println("Connecting to " + target + " at port " + port); channel.connect(new java.net.InetSocketAddress(target, port)); if (channel.isConnectionPending()) { channel.finishConnect(); System.out.println("SocketChannel finishing connection."); } if (channel.isConnected()) { System.out.println("SocketChannel connected."); this.isActive(true); new Thread(this).start(); } else { System.out.println("SocketChannel unable to connect."); } } public ByteBuffer in; public ByteBuffer out; public void run() { in = ByteBuffer.allocate(512); while (this.isActive()) { try { in.clear(); if (channel.read(in) < 0 && !in.hasRemaining()) { System.out.println("No data to read."); break; } else { if (!this.hasRegistered()) { this.send("USER java \"kingskrown.com\" \"irc.kingskrown.com\" :java\r\n"); this.send("NICK java\r\n"); this.hasRegistered(true); continue; } System.out.println(in.array()); } try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } catch (java.io.IOException e) { e.printStackTrace(); } } } public boolean hasRegistered; public void hasRegistered(boolean bool) { this.hasRegistered = bool; } public boolean hasRegistered() { return this.hasRegistered; } public void send(String dat) throws java.io.IOException { ByteBuffer out = ByteBuffer.wrap(dat.getBytes()); channel.write(out); } public void parseLine(String line) { } }