test/java/nio/channels/DatagramChannel/Connect.java

Print this page




  26  * @author Mike McCloskey
  27  */
  28 
  29 import java.io.*;
  30 import java.net.*;
  31 import java.nio.*;
  32 import java.nio.channels.*;
  33 import java.nio.charset.*;
  34 
  35 
  36 public class Connect {
  37 
  38     static PrintStream log = System.err;
  39 
  40     public static void main(String[] args) throws Exception {
  41         test();
  42     }
  43 
  44     static void test() throws Exception {
  45         Reactor r = new Reactor();
  46         Actor a = new Actor(r.port());
  47         invoke(a, r);
  48     }
  49 
  50     static void invoke(Sprintable reader, Sprintable writer) throws Exception {
  51 
  52         Thread writerThread = new Thread(writer);
  53         writerThread.start();
  54 
  55         Thread readerThread = new Thread(reader);
  56         readerThread.start();
  57 
  58         writerThread.join();
  59         readerThread.join();
  60 
  61         reader.throwException();
  62         writer.throwException();
  63     }
  64 
  65     public interface Sprintable extends Runnable {
  66         public void throwException() throws Exception;
  67     }
  68 
  69     public static class Actor implements Sprintable {
  70         final int port;

  71         Exception e = null;
  72 
  73         Actor(int port) {

  74             this.port = port;
  75         }
  76 
  77         public void throwException() throws Exception {
  78             if (e != null)
  79                 throw e;
  80         }
  81 
  82         public void run() {
  83             try {
  84                 DatagramChannel dc = DatagramChannel.open();
  85 
  86                 // Send a message
  87                 ByteBuffer bb = ByteBuffer.allocateDirect(256);
  88                 bb.put("hello".getBytes());
  89                 bb.flip();
  90                 InetAddress address = InetAddress.getLocalHost();
  91                 InetSocketAddress isa = new InetSocketAddress(address, port);
  92                 dc.connect(isa);
  93                 dc.write(bb);
  94 
  95                 // Try to send to some other address
  96                 address = InetAddress.getLocalHost();
  97                 InetSocketAddress bogus = new InetSocketAddress(address, 3333);
  98                 try {
  99                     dc.send(bb, bogus);
 100                     throw new RuntimeException("Allowed bogus send while connected");
 101                 } catch (IllegalArgumentException iae) {
 102                     // Correct behavior
 103                 }
 104 
 105                 // Read a reply
 106                 bb.flip();
 107                 dc.read(bb);
 108                 bb.flip();
 109                 CharBuffer cb = Charset.forName("US-ASCII").
 110                 newDecoder().decode(bb);
 111                 log.println("From Reactor: "+isa+ " said " +cb);
 112 
 113                 // Clean up
 114                 dc.disconnect();
 115                 dc.close();
 116             } catch (Exception ex) {
 117                 e = ex;
 118             }
 119         }
 120     }
 121 
 122     public static class Reactor implements Sprintable {
 123         final DatagramChannel dc;
 124         Exception e = null;
 125 
 126         Reactor() throws IOException {
 127             dc = DatagramChannel.open().bind(new InetSocketAddress(0));
 128         }
 129 
 130         int port() {
 131             return dc.socket().getLocalPort();
 132         }
 133 




 134         public void throwException() throws Exception {
 135             if (e != null)
 136                 throw e;
 137         }
 138 
 139         public void run() {
 140             try {
 141                 // Listen for a message
 142                 ByteBuffer bb = ByteBuffer.allocateDirect(100);
 143                 SocketAddress sa = dc.receive(bb);
 144                 bb.flip();
 145                 CharBuffer cb = Charset.forName("US-ASCII").
 146                 newDecoder().decode(bb);
 147                 log.println("From Actor: "+sa+ " said " +cb);
 148 
 149                 // Reply to sender
 150                 dc.connect(sa);
 151                 bb.flip();
 152                 dc.write(bb);
 153 


  26  * @author Mike McCloskey
  27  */
  28 
  29 import java.io.*;
  30 import java.net.*;
  31 import java.nio.*;
  32 import java.nio.channels.*;
  33 import java.nio.charset.*;
  34 
  35 
  36 public class Connect {
  37 
  38     static PrintStream log = System.err;
  39 
  40     public static void main(String[] args) throws Exception {
  41         test();
  42     }
  43 
  44     static void test() throws Exception {
  45         Reactor r = new Reactor();
  46         Actor a = new Actor(r.address(), r.port());
  47         invoke(a, r);
  48     }
  49 
  50     static void invoke(Sprintable reader, Sprintable writer) throws Exception {
  51 
  52         Thread writerThread = new Thread(writer);
  53         writerThread.start();
  54 
  55         Thread readerThread = new Thread(reader);
  56         readerThread.start();
  57 
  58         writerThread.join();
  59         readerThread.join();
  60 
  61         reader.throwException();
  62         writer.throwException();
  63     }
  64 
  65     public interface Sprintable extends Runnable {
  66         public void throwException() throws Exception;
  67     }
  68 
  69     public static class Actor implements Sprintable {
  70         final int port;
  71         final InetAddress address;
  72         Exception e = null;
  73 
  74         Actor(InetAddress address, int port) {
  75             this.address = address;
  76             this.port = port;
  77         }
  78 
  79         public void throwException() throws Exception {
  80             if (e != null)
  81                 throw e;
  82         }
  83 
  84         public void run() {
  85             try {
  86                 DatagramChannel dc = DatagramChannel.open();
  87 
  88                 // Send a message
  89                 ByteBuffer bb = ByteBuffer.allocateDirect(256);
  90                 bb.put("hello".getBytes());
  91                 bb.flip();

  92                 InetSocketAddress isa = new InetSocketAddress(address, port);
  93                 dc.connect(isa);
  94                 dc.write(bb);
  95 
  96                 // Try to send to some other address

  97                 InetSocketAddress bogus = new InetSocketAddress(address, 3333);
  98                 try {
  99                     dc.send(bb, bogus);
 100                     throw new RuntimeException("Allowed bogus send while connected");
 101                 } catch (IllegalArgumentException iae) {
 102                     // Correct behavior
 103                 }
 104 
 105                 // Read a reply
 106                 bb.flip();
 107                 dc.read(bb);
 108                 bb.flip();
 109                 CharBuffer cb = Charset.forName("US-ASCII").
 110                 newDecoder().decode(bb);
 111                 log.println("From Reactor: "+isa+ " said " +cb);
 112 
 113                 // Clean up
 114                 dc.disconnect();
 115                 dc.close();
 116             } catch (Exception ex) {
 117                 e = ex;
 118             }
 119         }
 120     }
 121 
 122     public static class Reactor implements Sprintable {
 123         final DatagramChannel dc;
 124         Exception e = null;
 125 
 126         Reactor() throws IOException {
 127             dc = DatagramChannel.open().bind(new InetSocketAddress(0));
 128         }
 129 
 130         int port() {
 131             return dc.socket().getLocalPort();
 132         }
 133     
 134         InetAddress address() {
 135             return dc.socket().getLocalAddress();
 136         }
 137 
 138         public void throwException() throws Exception {
 139             if (e != null)
 140                 throw e;
 141         }
 142 
 143         public void run() {
 144             try {
 145                 // Listen for a message
 146                 ByteBuffer bb = ByteBuffer.allocateDirect(100);
 147                 SocketAddress sa = dc.receive(bb);
 148                 bb.flip();
 149                 CharBuffer cb = Charset.forName("US-ASCII").
 150                 newDecoder().decode(bb);
 151                 log.println("From Actor: "+sa+ " said " +cb);
 152 
 153                 // Reply to sender
 154                 dc.connect(sa);
 155                 bb.flip();
 156                 dc.write(bb);
 157