//接受程序是
public class Text2 {
public static void main(String[] args) throws IOException{
DatagramSocket ds = new DatagramSocket(10008);
while(true)
{
byte[] b = new byte[1024];
DatagramPacket dp = new DatagramPacket(b,b.length);
ds.receive(dp);
String s = dp.getAddress().getHostAddress();
String data = new String(dp.getData(),0,dp.getLength());
int port = dp.getPort();
System.out.println(s+".."+data+".."+port);
}
}
}
//第一个发送程序
import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.*;
import java.util.Map.Entry;
public class Udpf {
public static void main(String[] args) throws IOException{
DatagramSocket ds = new DatagramSocket();
byte[] b = "dfgfgh".getBytes();
DatagramPacket dp =
new DatagramPacket(b,b.length,
InetAddress.getByName("192.168.0.103"),10008);
ds.send(dp);
ds.close();
}
}
/*这个事时候接收程序运行,方法程序运行一次输出一次192.168.0.103..dfgfgh..3774
*/
//第二个发送程序
class Text{
public static void main(String[] args) throws IOException{
DatagramSocket ds = new DatagramSocket();
BufferedReader fr = new BufferedReader(new InputStreamReader(System.in));
String line;
while((line=fr.readLine())!=null)
{
if("886".equals(line))
break;
byte[] b = line.getBytes();
DatagramPacket dp = new DatagramPacket(b,b.length,InetAddress.getByName("192.168.0.103"),10008);
ds.send(dp);
}
ds.close();
}
/*第二个发送程序想写一句打印一局,但是这个程序发送一次就结束了怎么回事*/ |