| 发表于:2007-03-02 11:34:5643楼 得分:0 |
moon1(静静的黎明)老兄,你好,我按照你的说法,把我的代码改写如下: public static string receive_message(string ip_address, int port) { iphostentry lipa = dns.resolve(ip_address); ipendpoint lep = new ipendpoint(lipa.addresslist[0], port); socket s = new socket(lep.address.addressfamily, sockettype.stream, protocoltype.tcp); try { s.connect(lep); } catch (system.exception e) {string msgerr = e.message ;} byte[] send_msg = encoding.ascii.getbytes( "this is a test "); int i = s.send(send_msg, 0, send_msg.length, socketflags.none); byte[] bytes = new byte[1024]; int bytesrev = 0; string msg = " "; do { bytesrev = s.receive(bytes, 0, bytes.length, socketflags.none); msg += system.text.encoding.utf8.getstring(bytes, 0, bytesrev); } while(bytesrev > 0); return msg; s.shutdown(socketshutdown.both); s.close(); } 程序启动的时候用下面代码打开端口,如果不用下面代码打开端口,程序运行到 s.connect(lep); 发生异常:由于目机器积极拒绝,无法连接 但是,先用下面代码打开端口,程序运行到: bytesrev = s.receive(bytes, 0, bytes.length, socketflags.none); 就死机,为什么啊? 请指教! 多谢! | | |
|