本帖最后由 奋发吧小白 于 2014-8-31 09:03 编辑
UDP接收端:
- import java.net.*;
- class Rec
- {
- public static void main(String[] args) throws Exception
- {
- DatagramSocket ds = new DatagramSocket(10000);
- byte buf[] = new byte[1024];
- DatagramPacket dp = new DatagramPacket(buf,buf.length);
- ds.receive(dp);
- String ip = dp.getAddress().getHostAddress();
- String data = new String(dp.getData(),0,dp.getLength());
- int port = dp.getPort();
- System.out.println(ip+"::"+data+"::"+port);
- ds.close();
- }
- }
复制代码 UDP发送端:- import java.net.*;
- class Send
- {
- public static void main(String [] args) throws Exception
- {
- DatagramSocket ds = new DatagramSocket();
- byte [] buf = "nihaowolaole".getBytes();
- DatagramPacket dp =
- new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.104"),10000);
- ds.close();
- }
- }
复制代码
|
|