B
billa
Hi,
I need to implement a socket server. I am using the following code to
read from the socket.
private int Read(ref byte[] buffer)
{
if (sapRequest.WaitForRequestBytes(socket) == 0)
return 0;
int numBytes = socket.Available;
int numReceived = 0;
buffer = new byte[numBytes];
if (numBytes > 0)
{
numReceived = socket.Receive(buffer, 0, numBytes,
SocketFlags.None);
}
if (numReceived < numBytes)
{
byte[] tempBuffer = new byte[numReceived];
if (numReceived > 0)
{
Buffer.BlockCopy(buffer, 0, tempBuffer, 0, numReceived);
}
buffer = tempBuffer;
}
DWLog.Write(DWLogLevel.Information,"Number of bytes received: " +
numReceived);
return numReceived;
}
As you can see I am also using a method called WaitForRequestBytes.
Here it is:
public int WaitForRequestBytes(Socket socket)
{
int availBytes = 0;
try
{
DWLog.Write(DWLogLevel.Information,"Socket.Available: " +
socket.Available);
if (socket.Available == 0)
{
// poll until there is data
// DWLog.Write(DWLogLevel.Information, "Waiting for
more data. Polling 100ms");
// socket.Poll(10000 /* 100ms */,
SelectMode.SelectRead);
DWLog.Write(DWLogLevel.Information, "Waiting for
more data. Polling 100ms");
socket.Poll(10000 /* 100ms */,
SelectMode.SelectRead);
if (socket.Available == 0 && socket.Connected)
{
DWLog.Write(DWLogLevel.Information,"Waiting for more data.
Polling 1s");
socket.Poll(100000 /* 1s */, SelectMode.SelectRead);
// DWLog.Write(DWLogLevel.Information, "Waiting
for more data. Polling 10sec");
// socket.Poll(10000000 /* 10sec */,
SelectMode.SelectRead);
}
}
availBytes = socket.Available;
DWLog.Write(DWLogLevel.Information,"Bytes available: " +
availBytes);
}
catch
{
}
return availBytes;
}
My problem now is the following. The bytes don´t arrive continuously
from the client. Therefore I introduced the method WaitForRequestBytes
which polls for data. The polling does its job. The polling time is
just enough to give the client enough time to send more data. Now this
approach proves to be a drawback on performance since the polling is
always executed even if there is no more data available from the
client.
For some reason and under certain circumstances socket.Available return
0 even when the client has not yet sent all its data.
Can anyone explain thsi behaviour to me?
Is there a reliable way to determine if all bytes where received from a
client?
I think asyncronous sockets are no solution for me, because I need
control the order of the processing of the sockets.
Thanks for your help.
I need to implement a socket server. I am using the following code to
read from the socket.
private int Read(ref byte[] buffer)
{
if (sapRequest.WaitForRequestBytes(socket) == 0)
return 0;
int numBytes = socket.Available;
int numReceived = 0;
buffer = new byte[numBytes];
if (numBytes > 0)
{
numReceived = socket.Receive(buffer, 0, numBytes,
SocketFlags.None);
}
if (numReceived < numBytes)
{
byte[] tempBuffer = new byte[numReceived];
if (numReceived > 0)
{
Buffer.BlockCopy(buffer, 0, tempBuffer, 0, numReceived);
}
buffer = tempBuffer;
}
DWLog.Write(DWLogLevel.Information,"Number of bytes received: " +
numReceived);
return numReceived;
}
As you can see I am also using a method called WaitForRequestBytes.
Here it is:
public int WaitForRequestBytes(Socket socket)
{
int availBytes = 0;
try
{
DWLog.Write(DWLogLevel.Information,"Socket.Available: " +
socket.Available);
if (socket.Available == 0)
{
// poll until there is data
// DWLog.Write(DWLogLevel.Information, "Waiting for
more data. Polling 100ms");
// socket.Poll(10000 /* 100ms */,
SelectMode.SelectRead);
DWLog.Write(DWLogLevel.Information, "Waiting for
more data. Polling 100ms");
socket.Poll(10000 /* 100ms */,
SelectMode.SelectRead);
if (socket.Available == 0 && socket.Connected)
{
DWLog.Write(DWLogLevel.Information,"Waiting for more data.
Polling 1s");
socket.Poll(100000 /* 1s */, SelectMode.SelectRead);
// DWLog.Write(DWLogLevel.Information, "Waiting
for more data. Polling 10sec");
// socket.Poll(10000000 /* 10sec */,
SelectMode.SelectRead);
}
}
availBytes = socket.Available;
DWLog.Write(DWLogLevel.Information,"Bytes available: " +
availBytes);
}
catch
{
}
return availBytes;
}
My problem now is the following. The bytes don´t arrive continuously
from the client. Therefore I introduced the method WaitForRequestBytes
which polls for data. The polling does its job. The polling time is
just enough to give the client enough time to send more data. Now this
approach proves to be a drawback on performance since the polling is
always executed even if there is no more data available from the
client.
For some reason and under certain circumstances socket.Available return
0 even when the client has not yet sent all its data.
Can anyone explain thsi behaviour to me?
Is there a reliable way to determine if all bytes where received from a
client?
I think asyncronous sockets are no solution for me, because I need
control the order of the processing of the sockets.
Thanks for your help.