private static Socket ConnectSocket(string server, int port)
{
Socket s = null;
IPHostEntry hostEntry = null;
hostEntry = Dns.GetHostEntry(server);
foreach (IPAddress address in hostEntry.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, port);
Socket tempSocket =
new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
tempSocket.Connect(ipe);
if (tempSocket.Connected)
{
s = tempSocket;
break;
}
else
{
continue;
}
}
return s;
}
//...
Socket s = ConnectSocket(server, port);
//...
do
{
bytes = s.Receive(bytesReceived, bytesReceived.Length, 0); // 1
page = page + Encoding.UTF8.GetString(bytesReceived, 0, bytes); // 2
}
while (bytes == 1024);这是一个“页”割礼(无结束)的数据。如果在"/ / 1“和"/ / 2”之间写入System.Threading.Thread.Sleep(100),那么一切都正常。
发布于 2010-07-09 09:17:46
我看不出那是怎么回事。没有只有三个参数的接收重载。另外,您将bytesReceived.Length放在了一个不正确的位置。
s.Receive(bytesReceived, 0, bytesReceived.Length);编辑:噢。您正在为SocketFlags使用零。别用神奇的数字。
然后没有任何信息表明每次必须到达1024字节,TCP不是这样构建的。TCP只保证所有字节都会到达,而不是何时或如何到达。
您必须知道要接收多少字节,或者在发送所有内容时断开连接。
https://stackoverflow.com/questions/3211268
复制相似问题