问题如题,下面是用Socket和TcpListener接收数据的代码,2个都接收不完,唯一的区别就是Socket比TcpListener接收数据要多一些.(局域网内没问题,在公网上才会出现).请帮忙解答下.先谢谢了!(最后20分了,没分了)//TcpListener
int bytes = 0;
byte[] byteMessage = new byte[2048];
TcpClient client = null;
NetworkStream stream = null;
TcpListener server = new TcpListener(iep);
server.Start(500);
while (true)
{
StringBuilder sb = new StringBuilder();
try
{
client = server.AcceptTcpClient();
stream = client.GetStream();
do
{
bytes = stream.Read(byteMessage, 0, byteMessage.Length);
sb.Append(System.Text.Encoding.UTF32.GetString(byteMessage, 0, bytes));
} while (stream.DataAvailable);
}
catch (SocketException ex)
{
Console.WriteLine(ex.ErrorCode);
}
finally
{
if (stream != null)
stream.Close();
if (client != null)
client.Close();
}
}//Socket
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(iep);
socket.Listen(5000);
byte[] byteMessage = new byte[1024];
Socket socket1 = null;
while (true)
{
StringBuilder sb = new StringBuilder();
try
{
int bytes = 0;
socket1 = socket.Accept();
do
{
bytes = socket1.Receive(byteMessage, byteMessage.Length, 0);
sb.Append(Encoding.UTF32.GetString(byteMessage, 0, bytes));
} while (bytes > 0);
socket1.Shutdown(SocketShutdown.Both);
socket1.Close();
}
catch (SocketException ex)
{
Console.WriteLine(ex.ErrorCode);
}
}
int bytes = 0;
byte[] byteMessage = new byte[2048];
TcpClient client = null;
NetworkStream stream = null;
TcpListener server = new TcpListener(iep);
server.Start(500);
while (true)
{
StringBuilder sb = new StringBuilder();
try
{
client = server.AcceptTcpClient();
stream = client.GetStream();
do
{
bytes = stream.Read(byteMessage, 0, byteMessage.Length);
sb.Append(System.Text.Encoding.UTF32.GetString(byteMessage, 0, bytes));
} while (stream.DataAvailable);
}
catch (SocketException ex)
{
Console.WriteLine(ex.ErrorCode);
}
finally
{
if (stream != null)
stream.Close();
if (client != null)
client.Close();
}
}//Socket
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(iep);
socket.Listen(5000);
byte[] byteMessage = new byte[1024];
Socket socket1 = null;
while (true)
{
StringBuilder sb = new StringBuilder();
try
{
int bytes = 0;
socket1 = socket.Accept();
do
{
bytes = socket1.Receive(byteMessage, byteMessage.Length, 0);
sb.Append(Encoding.UTF32.GetString(byteMessage, 0, bytes));
} while (bytes > 0);
socket1.Shutdown(SocketShutdown.Both);
socket1.Close();
}
catch (SocketException ex)
{
Console.WriteLine(ex.ErrorCode);
}
}
socket.Blocking = true;
private void down(ref NetworkStream stream)
{
int length = 1024;
byte[] bye = new byte[1024];
int tt = stream.Read(bye, 0, length);
//下面循环读取网络流并写进文件
while (tt > 0)
{
string ss = System.Text.Encoding.ASCII.GetString(bye);
int x = ss.IndexOf("<EOF>");
if (x != -1)
{
filestream.Write(bye, 0, x);
filestream.Flush();
break;
}
else
{
filestream.Write(bye, 0, tt);
filestream.Flush();
}
tt = stream.Read(bye, 0, length);
}
filestream.Close();
stream.Close();
this.stsLable.Text = "升级包已正确接受完毕,请注意查收!";
FillFileList();
}
do
{
bytes = stream.Read(byteMessage, 0, byteMessage.Length);
sb.Append(System.Text.Encoding.UTF32.GetString(byteMessage, 0, bytes));
} while (stream.DataAvailable);
我给你分析一下你这个循环亚
stream = client.GetStream();//这时候stream第一次被读取过来,如果这次有数据的话
那么
while (stream.DataAvailable)永远成立,你的程序陷入了死循环,你肯定一辈子读不完了