Strumenti Utente

Strumenti Sito


lpr-b:copyin
package chatMulticast;
 
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
 
public class CopyIn extends Thread {
 
	int chatPort = 0; 
	InetAddress chatAddress = null; 
 
	final int MAXBUF = 1024; 
 
	public  CopyIn(InetAddress ia, int port) {
		chatPort = port;
		chatAddress = ia; 
	}
 
	public void run() {
		MulticastSocket ds = null;
		try {
			ds = new MulticastSocket(chatPort);
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			ds.joinGroup(chatAddress);
		} catch (IOException e) {
			e.printStackTrace();
		}
		DatagramPacket dp = null; 
		while (true) {
			try {
				dp = new DatagramPacket(new byte[MAXBUF], 0, MAXBUF);
				ds.receive(dp);
			} catch (IOException e) {
				e.printStackTrace();
			}
			String line =  new String(dp.getData());
			System.out.println("From "+dp.getAddress().getHostName()+": "+line);
		}
	}
 
}
lpr-b/copyin.txt · Ultima modifica: 23/11/2007 alle 13:55 (12 anni fa) da Marco Danelutto