您的位置:程序门 -> .net技术 -> c#



socket的udp方式接收完数据后如何获得 对方发来的ip地址和端口,在回发


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


socket的udp方式接收完数据后如何获得 对方发来的ip地址和端口,在回发[已结贴,结贴人:lifeixie]
发表于:2007-08-14 10:11:00 楼主
我   用   udp接收   对方发来的数据,     如果是tcp方式就得到对方的   client对象,然后   回发   数据回去

但现在是udp   可能没有连接,所以也得不到client对象,那得到对方的   ip,和端口号怎么获得,怎么回发回去。
接收函数在   readcallback里面,在   readcallback里,如何获得   对方的ip,端口号在回发给   可户端!代码怎么写应该?

代码如下!·!!!!!!!!!!!!!!!!!!!!!


    ///   <summary>
                ///   启动服务器程序,开始监听客户端请求
                ///   </summary>
                public   virtual   void   start()
                {
                        if   (_isrun)
                        {
                                throw   (new   applicationexception( "tcpsvr已经在运行. "));
                        }

                        //缓冲大小
                        _recvdatabuffer   =   new   byte[defaultbuffersize];
                        //初始化socket
                        _svrsock   =   new   socket(addressfamily.internetwork,
                                sockettype.dgram,   protocoltype.udp);
                        //绑定端口  
                        iphostentry   iphostinfo   =   dns.resolve(dns.gethostname());
                        ipaddress   ipaddress   =   iphostinfo.addresslist[0];
                        ipendpoint   iep   =   new   ipendpoint(ipaddress,   _port);
                        _svrsock.bind(iep);

                        tempremoteep   =   (endpoint)iep;
                     

                        _svrsock.beginreceivefrom(_recvdatabuffer,   0,   _recvdatabuffer.length,   0,   ref   tempremoteep,   new   asynccallback(readcallback),   null);

                        _isrun   =   true;

                }
                //接收函数委托(异步回调,本身带线程,不需要使用线程)
                public   void   readcallback(iasyncresult   ar)
                {

                        //得到可户端发来的   数据,然后怎么把   在把数据回发给可户端
                       
                        byte[]   ai   =   _recvdatabuffer;
                        //byte[]   bi   =   {   1,2,3,4,5,6,76,6,7,254,182,128,7,0};
                      //这里我想把   可户端发来的数据,在发给可户端应该如何做?获得   ip,端口号怎么获得后回发
                        //_svrsock.sendto(bi,   rmt);

                        _svrsock.beginreceivefrom(_recvdatabuffer,   0,   _recvdatabuffer.length,   0,   ref   tempremoteep,   new   asynccallback(readcallback),   null);
       
                }
发表于:2007-08-14 10:16:341楼 得分:50
up
发表于:2007-08-14 10:27:062楼 得分:0
没人知道吗?
发表于:2007-08-14 10:29:353楼 得分:0
可以将ip   port也作为数据报头来发送

例如   数据结构可以如下  

报头   ip     port   filesize   curtblock   ……//filesize标示共有多少个包   curtblock标示当前包号

报文   data

当然具体的数据结构   可根据自己的实际情况不同而不同
发表于:2007-08-14 10:35:134楼 得分:0
ls的,你说的我知道,但我只想问,怎么自动获得!?应该可以的!
发表于:2007-08-14 10:41:285楼 得分:0
udp是短连接的。可以吗?还是自己定义一个报文的格式吧。比如说吧ip   和port   加上去。
发表于:2007-08-14 11:00:096楼 得分:0
udp是无连接协议……
发表于:2007-08-14 11:09:597楼 得分:0
无连接   不代表没法获得   ip,和端口啊。只是无法获得     client   对象!
你肯定   udp无法自动获得   端口和ip??
发表于:2007-08-14 13:11:428楼 得分:0
udpclient   udp=new   udpclient(new   ipendpoint(ipaddress,startport+i));
发表于:2007-08-14 14:04:169楼 得分:0
//接收函数委托(异步回调,本身带线程,不需要使用线程)
                public   void   readcallback(iasyncresult   ar)
                {

                        //得到可户端发来的   数据,然后怎么把   在把数据回发给可户端
                       
}
这里如何获得。
发表于:2007-08-14 15:07:3610楼 得分:0
爱迪生爱迪生法按时地方按时反对士大夫盛大士大夫似的似的发疯生生世世事实
发表于:2007-08-14 15:39:5811楼 得分:0
把要发的数据组织成对象,对象有sendtoip,sendtoport,sendfromip等属性,序列化为2进制,然后发送。接收端收到后,把2进制反序列化为对象,然后就可以取出sendfromip,sendfromport属性,用socket.sendto()方法。
发表于:2007-08-15 11:21:5612楼 得分:0
//接收函数委托(异步回调,本身带线程,不需要使用线程)
                public   void   readcallback(iasyncresult   ar)
                {
                        byte[]   ai   =   _recvdatabuffer;
                       
                        int   cnt=0;
                        cnt   =   _svrsock.endreceivefrom(ar,   ref   this.rmtep);

                        if   (cnt   >   0)
                        {
                                ipendpoint   iep   =   (ipendpoint)this.rmtep;
                                if   (recvdata   !=   null)
                                {  

                                }
                                string   a   =   iep.address.tostring();
                                string   b   =   iep.port.tostring();
 
                        }


这样终于可以了!!!!!!!!!!!!!!!!!


快速检索

最新资讯
热门点击