home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-21 | 1.9 KB | 62 lines |
- // An object that sends text messages
-
- // Written by Bernie Roehl, January 1997
-
- package multi;
-
- import java.net.*;
- import java.io.*;
-
- public class TextSender {
-
- InetAddress host;
- int port;
- int text_ssrc;
- int seqnum = 0;
-
- DatagramSocket socket;
- DatagramPacket packet;
-
- ByteArrayOutputStream bout;
- DataOutputStream dout;
-
- public synchronized int getTextId() { return text_ssrc; }
- public synchronized void setTextId(int id) { text_ssrc = id; }
-
- public TextSender(String hostname, int prt) throws SocketException {
- try { host = InetAddress.getByName(hostname); }
- catch (UnknownHostException ex) {
- System.out.println("Unknown host \"" + hostname + "\"");
- return;
- }
- port = prt;
- bout = new ByteArrayOutputStream();
- dout = new DataOutputStream(bout);
- socket = new DatagramSocket();
- }
-
- public synchronized void sendText(String text) throws IOException {
- int msglen = text.length();
- System.out.println("In TextSender, length = " + msglen);
- byte[] message = new byte[msglen];
- text.getBytes(0, msglen, message, 0);
- bout.reset();
- // version = 2, padding = 0, extension = 0, CSRC count = 0
- dout.writeByte(0x80);
- dout.writeByte(80); // mark = 0, payload type = 80
- dout.writeShort(seqnum++); // sequence number
- dout.writeInt(0); // timestamp
- dout.writeInt(text_ssrc); // entid
- dout.writeShort(0); // type
- dout.writeShort((short) msglen);
- dout.write(message, 0, msglen);
- dout.flush();
- System.out.println("In TextSender, byte array length = " + bout.size());
- packet = new DatagramPacket(bout.toByteArray(), bout.size(), host, port);
- socket.send(packet);
- System.out.println("In TextSender, packet size = " + packet.getLength());
- }
-
- }
-
-