// Test the connected case to see if PUE is thrown public static void test2() throws Exception { setup(); server.configureBlocking(true); server.connect(isa); server.configureBlocking(false); outBuf.rewind(); server.write(outBuf); server.receive(inBuf); client.close(); Thread.sleep(2000); outBuf.rewind(); try { server.write(outBuf); Thread.sleep(2000); inBuf.clear(); server.read(inBuf); if (onSolarisOrLinux()) throw new Exception("Expected PUE not thrown"); } catch (PortUnreachableException pue) { System.err.println("received PUE"); } server.close(); }
public static void setup() throws Exception { client = DatagramChannel.open(); server = DatagramChannel.open(); client.socket().bind((SocketAddress) null); server.socket().bind((SocketAddress) null); client.configureBlocking(false); server.configureBlocking(false); InetAddress address = InetAddress.getLocalHost(); int port = client.socket().getLocalPort(); isa = new InetSocketAddress(address, port); }
public long openUdpSocket(InetSocketAddress address) throws IOException { // TODO, don't throw an exception out of here. DatagramChannel dg = DatagramChannel.open(); dg.configureBlocking(false); dg.socket().bind(address); long b = createBinding(); EventableChannel ec = new EventableDatagramChannel(dg, b, mySelector); dg.register(mySelector, SelectionKey.OP_READ, ec); Connections.put(b, ec); return b; }
public static void main(String args[]) throws Exception { final int ENOUGH_SIZE = 1024; final int SMALL_SIZE = 4; boolean isBlocked = true; int size = ENOUGH_SIZE; if (args.length > 0) { int opt = Integer.parseInt(args[0]); switch (opt) { case 1: isBlocked = true; size = ENOUGH_SIZE; break; case 2: isBlocked = true; size = SMALL_SIZE; break; case 3: isBlocked = false; size = ENOUGH_SIZE; break; case 4: isBlocked = false; size = SMALL_SIZE; break; } } DatagramChannel channel = DatagramChannel.open(); channel.configureBlocking(isBlocked); ByteBuffer buffer = ByteBuffer.allocate(size); DatagramSocket socket = channel.socket(); SocketAddress localAddr = new InetSocketAddress(8000); socket.bind(localAddr); while (true) { System.out.println("开始接收数据报"); SocketAddress remoteAddr = channel.receive(buffer); if (remoteAddr == null) { System.out.println("没有接收到数据报"); } else { buffer.flip(); System.out.println("接收到的数据报的大小为" + buffer.remaining()); } Thread.sleep(500); } }
private void init() throws IOException { messageBuffer = new ArrayList(); receiveBuffer = ByteBuffer.allocate(CAPACITY); sendBuffer = ByteBuffer.allocate(CAPACITY); buf = new byte[CAPACITY]; channel = DatagramChannel.open(); channel.configureBlocking(false); InetAddress host = null; if (getAddress() != null) { host = InetAddress.getByAddress(getAddress().getBytes()); } channel.socket().bind(new InetSocketAddress(host, getPort())); channel.socket().setReceiveBufferSize(CAPACITY); certifier = new MessageCertifier(this); extractor = new MessageExtractor(this); }