日期:2014-05-17  浏览次数:20968 次

TcpClient应用。异常:远程主机强迫关闭了一个现有的连接。
 Try
            Dim client As TcpClient = New TcpClient("127.0.0.1", 2112)
            Dim stream As NetworkStream = client.GetStream()

            Dim phoneNums As String() = phone.Split(",")
            For Each phoneNum As String In phoneNums
                phoneNum = phoneNum.Trim()
               If String.IsNullOrEmpty(phoneNum) Then
                    Continue For
                End If

                Dim data As Byte() = Encoding.GetEncoding("gb2312").GetBytes("M=" + phoneNum + ",I=" + msg + vbCr)

                stream.Write(data, 0, data.Length)

                 data = New [Byte](256) {}

                 Dim responseData As [String] = [String].Empty
                 Dim bytes As Int32 = stream.Read(data, 0, data.Length)
                 responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)

                stream.Close()
client.Close()
           Next
        Catch e As Exception
            Response.Write(e.Message)
        End Try

这是原始的VB代码运行一切正常。

 try{
                TcpClient client = new TcpClient("127.0.0.1", 2112);

               phone = phone.Trim();
               if (String.IsNullOrEmpty(phone)) 
                    return; 
               Byte[] data = Encoding.GetEncoding("gb2312").GetBytes("M=" + phone + ",I=" + msg);
                NetworkStream stream = client.GetStream();