package net;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
public class socket {
public static final int PORT = 80;
public static final String ADDR = "127.0.0.1";
private MulticastSocket sock;
private InetAddress netAddr;
public socket()
{
try {
// 组播地址
netAddr = InetAddress.getByName(ADDR);
sock = new MulticastSocket(PORT);
sock.joinGroup(netAddr);
}
catch (UnknownHostException ex)
{
}
catch (IOException ex)
{
}
}
public void send(byte[] b)
{
DatagramPacket dp = new DatagramPacket(b,0,b.length,netAddr,PORT);
try {
sock.send(dp);
System.out.println("sent");
byte[] buf = new byte[1024];
DatagramPacket rcv = new DatagramPacket(buf,buf.length);
sock.receive(rcv);
System.out.println("receive:" + new String(rcv.getData()));
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
socket sender = new socket();
sender.send("yhaha".getBytes());
}
}