黑马程序员技术交流社区

标题: 毕老师视频23天里面关于网络编程的程序 [打印本页]

作者: Surprise    时间: 2015-9-13 15:01
标题: 毕老师视频23天里面关于网络编程的程序
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标志
                     但是端口只有六万多,而用户比它多得多,接收端是怎样确认自己想要的发送端?
作者: boboyuwu    时间: 2015-9-13 20:55
这是UDP协议  服务端不管收到的是哪个发送端  只接受到收到包,只要收到包就行了是谁发的无所谓




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2