| 发表于: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); } |
|
|
|
|