- package com.ccsu.net;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- import java.net.SocketException;
- class UDPSend1{
- public static void main(String[] args) throws IOException {
- DatagramSocket ds = new DatagramSocket();
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String line = null;
- while((line=br.readLine()) != null)
- {
- if("886".equals(line))
- break;
- byte[] buf = line.getBytes();
- DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),8888);
- ds.send(dp);
- }
- ds.close();
- }
- }
- class UDPRecevie1{
- public static void main(String[] args) throws IOException {
- DatagramSocket ds = new DatagramSocket(8888);
- while(true)
- {
- byte[] buf = new byte[1024];
- DatagramPacket dp = new DatagramPacket(buf,buf.length);
- ds.receive(dp);
- String ip = dp.getAddress().getHostAddress();
- System.out.println(ip);
- String data = new String(dp.getData(),0,dp.getLength(),"gbk");
- System.out.println("发送者说:"+ data);
- }
- }
- }
复制代码 |
|