在 VB.NET 中通过 TCP/IP 发送数据时程序冻结
Program Freezes when sending data via TCP/IP in VB.NET
我有一个正在向服务器发送数据的程序,它在尝试发送数据时死机了。
Dim postData = "000000001119001 MY0121 020216081825S0000000001233300000000002050"
Dim client As New TcpClient(IPAddressTextbox.Text, 17476)
' Translate the passed message into ASCII and store it as a Byte array.
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(postData)
' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
Dim stream As NetworkStream = client.GetStream()
' Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length)
Console.WriteLine("Sent: {0}", postData)
' Receive the TcpServer.response.
' Buffer to store the response bytes.
data = New [Byte](256) {}
' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty
' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
Console.WriteLine("Received: {0}", responseData)
ResponseBox.Text = responseData
' Close everything.
stream.Close()
client.Close()
我做了一些故障排除,发现它冻结在这一行
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
我从 Here 获得了这部分代码,因为我刚刚开始学习 TCP 连接,并且认为 Microsoft 可能是一个很好的学习资源。我已验证服务器 IP 和端口有效并接受我的连接。
figured Microsoft may be a good resource to learn from
哈哈哈...:( MSDN 套接字代码是找到的最糟糕的套接字代码之一。
如果读取"hangs"这意味着远程端甚至没有发送一个字节。我的猜测:您的请求格式不正确或不完整。对方正在等待您发送完整的数据。
请注意,读取可能是部分的。您通常需要循环,直到获得所需的所有数据。假设每次读取将 return 只有一个字节。您的代码必须能够处理该问题。你可以试试 new StreamReader(new NetworkStream(socket)).ReadToEnd()
.
这个微软示例代码太可耻了:
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
他们确实说是 "first batch",但这仍然导致代码损坏。
我有一个正在向服务器发送数据的程序,它在尝试发送数据时死机了。
Dim postData = "000000001119001 MY0121 020216081825S0000000001233300000000002050"
Dim client As New TcpClient(IPAddressTextbox.Text, 17476)
' Translate the passed message into ASCII and store it as a Byte array.
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(postData)
' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
Dim stream As NetworkStream = client.GetStream()
' Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length)
Console.WriteLine("Sent: {0}", postData)
' Receive the TcpServer.response.
' Buffer to store the response bytes.
data = New [Byte](256) {}
' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty
' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
Console.WriteLine("Received: {0}", responseData)
ResponseBox.Text = responseData
' Close everything.
stream.Close()
client.Close()
我做了一些故障排除,发现它冻结在这一行
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
我从 Here 获得了这部分代码,因为我刚刚开始学习 TCP 连接,并且认为 Microsoft 可能是一个很好的学习资源。我已验证服务器 IP 和端口有效并接受我的连接。
figured Microsoft may be a good resource to learn from
哈哈哈...:( MSDN 套接字代码是找到的最糟糕的套接字代码之一。
如果读取"hangs"这意味着远程端甚至没有发送一个字节。我的猜测:您的请求格式不正确或不完整。对方正在等待您发送完整的数据。
请注意,读取可能是部分的。您通常需要循环,直到获得所需的所有数据。假设每次读取将 return 只有一个字节。您的代码必须能够处理该问题。你可以试试 new StreamReader(new NetworkStream(socket)).ReadToEnd()
.
这个微软示例代码太可耻了:
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
他们确实说是 "first batch",但这仍然导致代码损坏。