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()).equals("886"))//弊端是不能发”886“
{
byte[] buf=line.getBytes();
DatagramPacket dp=
new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.255"),10008);
ds.send(dp);
}
ds.close();
}
}
class UdpReceive2
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds=new DatagramSocket(10008);
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(buf,0,dp.getLength());
System.out.println(ip+"-----"+data);
}
}
}
接收端是怎样确认自己要接受哪个ip给自己的数据,此函数只是在接收端有个端口10008,考虑这是ip标志
但是端口只有六万多,而用户比它多得多,接收端是怎样确认自己想要的发送端? |
|