home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch18 / multi / sender.java < prev    next >
Encoding:
Java Source  |  1997-02-21  |  1.9 KB  |  58 lines

  1. package multi;
  2.  
  3. import java.net.*;
  4. import java.io.*;
  5.  
  6. public class sender extends Thread {
  7.     public static void main(String[] args) throws UnknownHostException, SocketException {
  8.         new SendThread(args);
  9.     }
  10. }
  11.  
  12. class SendThread extends Thread {
  13.     InetAddress address;
  14.     int port;
  15.     ByteArrayOutputStream bout;
  16.     DataOutputStream dout;
  17.     DatagramSocket socket;
  18.  
  19.     public SendThread(String[] args)
  20.             throws UnknownHostException, SocketException {
  21.         address = InetAddress.getByName(args[0]);
  22.         port = Integer.parseInt(args[1]);
  23.         bout = new ByteArrayOutputStream();
  24.         dout = new DataOutputStream(bout);
  25.         socket = new DatagramSocket();
  26.         start();
  27.      }
  28.  
  29.     public void run() {
  30.         int seq = 1;
  31.         int timestamp = 140;
  32.         while (true) {
  33.             bout.reset();
  34.             try {
  35.             dout.writeByte(0x80);  // version = 2, padding = 0, extension = 0, CSRC count = 0
  36.             dout.writeByte(79);  // mark = 0, payload type = 79
  37.             dout.writeShort(seq++);
  38.             dout.writeInt(timestamp);  timestamp += 3;
  39.             dout.writeInt(1724);
  40.             dout.writeFloat(1f);  dout.writeFloat(2f);  dout.writeFloat(3f);
  41.             dout.writeFloat(0f);  dout.writeFloat(1f);  dout.writeFloat(0f); dout.writeFloat(0);
  42.             dout.writeInt(7);      // region
  43.             dout.writeFloat(2.75f);  // size
  44.             dout.writeShort(0);    // flags
  45.             DatagramPacket packet = new DatagramPacket(bout.toByteArray(),
  46.                                       bout.size(), address, port);
  47.             socket.send(packet);
  48.             }
  49.             catch (IOException e) {
  50.                 System.out.println("Cannot build/construct packet: " + e);
  51.                 break;
  52.             }
  53.             try { sleep(3000); }
  54.             catch (InterruptedException e) { }
  55.         }
  56.     }
  57. }
  58.