日期:2014-05-17 浏览次数:20998 次
private void bt_test_Click(object sender, EventArgs e) { IPAddress[] ipAddress = Dns.GetHostAddresses("whois.verisign-grs.com"); byte[] receiveBufferStr = new byte[1024]; IPEndPoint ipep = new IPEndPoint(ipAddress[0],43); Socket socket = new Socket(ipep.AddressFamily, SocketType.Stream, ProtocolType.Tcp); string result = ""; int i = 0; socket.Connect(ipep); socket.Send(Encoding.ASCII.GetBytes("waihuiqq.com\r\n")); do { i = socket.Receive(receiveBufferStr); string temp = Encoding.ASCII.GetString(receiveBufferStr); result += temp;//此处会都多次执行到,但result的值一直是第一次的值 //rtb_result.AppendText(temp);//可以得到完整的结果 } while (i > 0); rtb_result.Clear(); rtb_result.AppendText(result); MessageBox.Show("完成"); }
string temp = ""; do { i = socket.Receive(receiveBufferStr); temp = Encoding.ASCII.GetString(receiveBufferStr); result += temp; } while (i > 0);
------解决方案--------------------
“得不到预期的效果”这个词儿太泛泛了,谁知道你得到的真实效果是什么呢?
receiveBufferStr中只有前i个字节才是接收到的数据,你为什么要把整个receiveBufferStr都进行解码呢?