本帖最后由 werewolf 于 2012-11-1 22:47 编辑
[ 本帖最后由 werewolf 于 2012-11-1 22:21 编辑 ]\n\n[ 本帖最后由 werewolf 于 2012-11-1 22:20 编辑 ]\n\n- import java.io.*;
- import java.net.*;
- class Send2
- {
- public static void main(String[] args) throws Exception
- {
-
- DatagramSocket ds = new DatagramSocket(8888);
- while (true)
- {
- byte [] buf = new byte[1024];
- BufferedReader bufr =
- new BufferedReader(new InputStreamReader(System.in));
-
- buf = bufr.readLine().getBytes();//不知道这几句有什么问题
- if ("886".equals(bufr.readLine()))//总是读取两次才发送。
- break;
- DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.137.255"),10000);
- ds.send(dp);
- }
-
- ds.close();
-
-
-
-
- }
- }
- class Receive2
- {
- 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 data = new String (dp.getData(),0,dp.getLength());
- String ip = dp.getAddress().getHostAddress();
- int port = dp.getPort();
- System.out.println(ip+"::"+data+"::"+port);
- }
-
-
- }
- }
复制代码 |
|