socket receive循环读取send来的数据
//send 图片流public void SendBinary(byte[] b)
{
try
{
socket.Send(b, b.Length, 0);
}
catch (Exception)//如果有异常则退出
{
socket.Close();
}
}
//receive 图片流
public byte[] ReceiveBinary()
{
//Byte[] buffer = new Byte[socket.Available];//一个缓存区
////循环从缓冲区读取数据
//do
//{
// socket.Receive(buffer);
//}
//while (socket.Available > 0);
//return buffer;
//循环从缓冲区读取数据
string receiveData = "";
int recvCount;//每次读取的数据量
int availCount = 0;//获取从网络可以读取的数据量
Byte[] buffer = new Byte[8192];//一个8×1024bits的缓存区
do
{
availCount = socket.Available;
if (availCount < buffer.Length)
{
recvCount = socket.Receive(buffer, availCount, SocketFlags.None);
}
else
{
recvCount = socket.Receive(buffer, buffer.Length, SocketFlags.None);
}
availCount = socket.Available;
if (recvCount > 0)
{
receiveData += Encoding.UTF8.GetString(buffer, 0, recvCount);
}
}
while (availCount > 0);
byte[] b = System.Text.Encoding.UTF8.GetBytes(receiveData);
return b;
} --------------------编程问答--------------------
很好
补充:.NET技术 , C#