www.pudn.com > jnp-src.rar > MixedcastServer.java
/*
* Java Network Programming, Second Edition
* Merlin Hughes, Michael Shoffner, Derek Hamner
* Manning Publications Company; ISBN 188477749X
*
* http://nitric.com/jnp/
*
* Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner;
* all rights reserved; see license.txt for details.
*/
import java.io.*;
import java.net.*;
public class MixedcastServer {
public static void main (String[] args) throws IOException {
if ((args.length != 2) || (args[1].indexOf (":") < 0))
throw new IllegalArgumentException
("Syntax: MixedcastServer :");
int serverPort = Integer.parseInt (args[0]);
int idx = args[1].indexOf (":");
InetAddress group = InetAddress.getByName (args[1].substring (0, idx));
int port = Integer.parseInt (args[1].substring (idx + 1));
init (serverPort, group, port);
while (true) {
relay ();
}
}
protected static DatagramSocket inSocket;
protected static MulticastSocket outSocket;
protected static DatagramPacket incoming, outgoing;
protected static void init (int serverPort, InetAddress group, int port) throws IOException {
inSocket = new DatagramSocket (serverPort);
outSocket = new MulticastSocket ();
outSocket.setTimeToLive (1);
byte[] buffer = new byte[65508];
incoming = new DatagramPacket (buffer, buffer.length);
outgoing = new DatagramPacket (buffer, buffer.length, group, port);
}
protected static void relay () throws IOException {
incoming.setLength (incoming.getData ().length);
inSocket.receive (incoming);
outgoing.setLength (incoming.getLength ());
outSocket.send (outgoing);
}
}