日期:2014-05-20 浏览次数:20898 次
import java.net.*; public class MainClass4 { public static void main(String[] args) { PacketSender ps=new PacketSender(); PacketReceiver pc=new PacketReceiver(); } } class PacketReceiver { public static void main(String[] args) throws Exception { byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); DatagramSocket socket = new DatagramSocket(5002); socket.receive(packet); System.out.println(packet.getSocketAddress()); System.out.println(new String(packet.getData(),0,packet.getLength())); socket.close(); } } class PacketSender { public static void main(String[] args) throws Exception { byte[] buffer = "data".getBytes(); InetAddress addr=InetAddress.getByName("localhost"); DatagramPacket packet = new DatagramPacket(buffer, buffer.length, addr, 5002); DatagramSocket socket = new DatagramSocket(); socket.send(packet); socket.close(); } }
PacketSender ps=new PacketSender();
PacketReceiver pc=new PacketReceiver();