本帖最后由 魑_魅 于 2012-2-5 09:22 编辑
- import java.net.*;
- import java.io.*;
- class send
- {
- public static void main(String[] args)throws Exception
- {
- DatagramSocket socket = new DatagramSocket();
- BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
- String line = null;
- while ((line = bufr.readLine()) != null)
- {
- if(line.equals("over"))
- break;
- DatagramPacket packet = new DatagramPacket(line.getBytes(),line.length(),InetAddress.getByName("192.168.1.105"),7878);
- socket.send(packet);
- }
- bufr.close();
- socket.close();
- }
- }
- class rec
- {
- public static void main(String[] args)throws Exception
- {
- DatagramSocket socket = new DatagramSocket(7878);
- while (true)
- {
- byte[] buf = new byte[1024];
- DatagramPacket packet = new DatagramPacket
- (buf,buf.length);
- socket.receive(packet);
- String ip = packet.getAddress().getHostAddress();
- String data = new String(packet.getData(),0,packet.getLength());
- int port = packet.getPort();
- System.out.println("from " + ip + " :" + port +"\t" + data );
- }
- }
- }
- public class Demo
- {
- public static void main(String[] args)throws Exception
- {
- }
- }
复制代码 |