本帖最后由 未名以律 于 2014-8-4 20:44 编辑
- import java.net.*;
- class UdpSend
- {
- public static void main(String[] args) throws Exception
- {
- DatagramSocket ds = new DatagramSocket(888);
- byte[] buf = "udp ge men lai la ".getBytes();
- DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.119"),10000);
- ds.send(dp);
- ds.close();
- }
- }
- class UdpRece
- {
- public static void main(String[] args) throws Exception
- {
- DatagramSocket ds = new DatagramSocket(10000);
- while(true)
- {
- 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(),dp.getLength());
- int port = dp.getPort();
- System.out.println(ip+"::"+data+"::"+port);
- }
- //ds.close();
- }
- }
复制代码
这个已过时是怎么回事?找api找了很久也没找出来
全是乱码啊。。。。试了N多次了,没整明白
|