socket.Available
does not work. Why?
Instead of size
I need to use socket.Available
. But it does not work. So I need a method that returns the number of bytes in socket.
void Receive(Socket socket, byte[] buffer, int offset, int size)
{
//int sockAvail = socket.Available; Here socke开发者_StackOverflow中文版t.Available always asigns 0 to sockAvail
int received = 0;
do
{
try
{
received += socket.Receive(buffer, offset + received, size - received, SocketFlags.None);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
} while (received < size);
MessageBox.Show(socket.Available.ToString());
}
Pass MSG_PEEK
to recv()
.
Socket.Available
has to be called before the receive()
into the buffer, otherwise it's always 0.
socket.Available
is for blocking sockets. With asynchronous reads, you just read what you can and then go back into a select to be told when it's ok to read more.
It does work. It just doesn't meet your expectations. Your expectations are wrong. Adjust them.
精彩评论