数据传输这一块,在命令行中打开两个窗口,可以一个发送一个接受,那如果在eclipse里面该怎么运行下面的代码,需要做什么改动,
或者说eclipse里面只能完成在一个窗口中进行数据交流的代码,
- import java.net.*;
- import java.io.*;
- class UdpSend2
- {
- public static void main(String[] args) throws Exception
- {
- DatagramSocket ds = new DatagramSocket();
-
- BufferedReader bufr =
- new BufferedReader(new InputStreamReader(System.in));
- String line = null;
- while((line=bufr.readLine())!=null)
- {
- if("886".equals(line))
- break;
- byte[] buf = line.getBytes();
- DatagramPacket dp =
- new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.102"),10001);
- ds.send(dp);
- }
- ds.close();
- }
- }
- class UdpRece2
- {
- public static void main(String[] args) throws Exception
- {
- DatagramSocket ds = new DatagramSocket(10001);
- 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(),0,dp.getLength());
- System.out.println(ip+"::"+data);
- }
- }
- }
复制代码
|
|