Problem with synchronous Socket



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.

.



Relevant Pages

  • Re: [patch 01/03] USB: USB/IP: add common functions needed
    ... This adds the common functions needed by both the host and client side ... +int setnodelay(struct socket *socket) ... This header adds a lot of symbols which have fairly generic-sounding ...
    (Linux-Kernel)
  • [patch 01/03] USB: USB/IP: add common functions needed
    ... This adds the common functions needed by both the host and client side ... * GNU General Public License for more details. ... +static void usbip_dump_pipe(unsigned int p) ... +int setquickack(struct socket *socket) ...
    (Linux-Kernel)
  • TCP/IP - Sockets appear to be restricted to maximum 65,535 byte transfers
    ... A very simple pair of programs (Server and Client) are setup ... vanilla UCX v3.3 (without ECO), ... The Server process establishes a socket, listens for an accept on the ... main (int argc, char ** argv) ...
    (comp.os.vms)
  • Re: Wait on a server socket for 10 seconds
    ... I read the following code which open a server socket for client ... // Create socket for listening for client connection requests. ... int reliable_recvfrom(int fd, void *data, int size, int flags, ...
    (comp.unix.programmer)
  • Re: out of socket!!!
    ... I have a programe act as client to get some info from server via ... polling. ... a new socket will be created first ...
    (comp.unix.programmer)