473,883 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Async Socket: How to tell when there is no more data.

The code below is what I am using to asynchronously get data from a
PocketPC device. The data comes in fine in blocks of 1024 bytes but
even when I send no data from the PocketPC constant blocks of 1024 with
all values set to Null arrive. Other than examine a block of 1024 to
see if the entire block is null, is there any other way to determine if
, say a chat message "Hi Charlie" has been received completely?


private ArrayList aryIncomingData = new ArrayList(); // pretty much a
byte[], easier to manage an ArrayList

public void WaitForData()
{
if ( pfnCallBack == null )
pfnCallBack = new AsyncCallback (OnDataReceived );

// now start to listen for any data...
m_asynResult =
sckInteractive. BeginReceive
(m_DataBuffer,0 ,m_DataBuffer.L ength,SocketFla gs.None,pfnCall Back,null);
}

public void OnDataReceived( IAsyncResult asyn)
{
//end receive...
int iRx = 0 ;
iRx = sckInteractive. EndReceive (asyn);

char[] chars = new char[iRx + 1];
System.Text.Dec oder d = System.Text.Enc oding.UTF8.GetD ecoder();
int charLen = d.GetChars(m_Da taBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(c hars);

System.Text.ASC IIEncoding asc = new ASCIIEncoding() ;
byte[] dataBytes = asc.GetBytes(sz Data);
LoadIncomingDat a(this.m_DataBu ffer);
WaitForData();
}

private bool LoadIncomingDat a(byte[] data)
{
foreach (byte b in data)
{
aryIncomingData .Add(b);
}
return true;

}

Nov 16 '05 #1
5 11738
You could create a unique terminator string and pass it at the end of each
message. Perhaps something like "<::EOM::>" . To tell the difference
between that string as part of a message and that string as the end of the
message, test for the string followed by null.

HTH

DalePres
MCAD, MCDBA, MCSE

<ms*****@osmose .com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
The code below is what I am using to asynchronously get data from a
PocketPC device. The data comes in fine in blocks of 1024 bytes but
even when I send no data from the PocketPC constant blocks of 1024 with
all values set to Null arrive. Other than examine a block of 1024 to
see if the entire block is null, is there any other way to determine if
, say a chat message "Hi Charlie" has been received completely?


private ArrayList aryIncomingData = new ArrayList(); // pretty much a
byte[], easier to manage an ArrayList

public void WaitForData()
{
if ( pfnCallBack == null )
pfnCallBack = new AsyncCallback (OnDataReceived );

// now start to listen for any data...
m_asynResult =
sckInteractive. BeginReceive
(m_DataBuffer,0 ,m_DataBuffer.L ength,SocketFla gs.None,pfnCall Back,null);
}

public void OnDataReceived( IAsyncResult asyn)
{
//end receive...
int iRx = 0 ;
iRx = sckInteractive. EndReceive (asyn);

char[] chars = new char[iRx + 1];
System.Text.Dec oder d = System.Text.Enc oding.UTF8.GetD ecoder();
int charLen = d.GetChars(m_Da taBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(c hars);

System.Text.ASC IIEncoding asc = new ASCIIEncoding() ;
byte[] dataBytes = asc.GetBytes(sz Data);
LoadIncomingDat a(this.m_DataBu ffer);
WaitForData();
}

private bool LoadIncomingDat a(byte[] data)
{
foreach (byte b in data)
{
aryIncomingData .Add(b);
}
return true;

}

Nov 16 '05 #2
Very Correct... But using a terminator like that is ONLY possible if you
have control over what the server is sending..
What if I am using real world servers like POP3 or SMTP which have no
terminators to denote that all data has been received... I have tried using
socket.Availabl e property but it is unreliable... because at times it gives 0
bytes available, but if you wait for 1-2 secs and check the property again
.... it has some bytes available for reading.. How would you get to know when
to stop reading and when to call "beginRecei ve" method again ????

I found this crappy code from MSDN.. which is ABSOLUTELY WRONG... the
program never reaches the "else" part of the code...while the socket is
connected.
private static void ReceiveCallback ( IAsyncResult ar )
{
try
{
StateObject state = (StateObject) ar.AsyncState;
Socket client = state.workSocke t;
int bytesRead = client.EndRecei ve(ar);

if (bytesRead > 0)
{
//Get more data....

state.sb.Append (Encoding.ASCII .GetString(stat e.buffer,0,byte sRead));
client.BeginRec eive(state.buff er,0,StateObjec t.BufferSize,0, new
AsyncCallback(R eceiveCallback) , state);
}
else
{
// All the data has arrived; put it in response.
if (state.sb.Lengt h > 1)
{
response = state.sb.ToStri ng();
}
// Signal that all bytes have been received.
receiveDone.Set ();
}
} catch (Exception e) {
Console.WriteLi ne(e.ToString() );
}
}

Nov 16 '05 #3
When a sender closes the socket the receiver is notified when the received
byte count is zero - this indicates that a graceful shutdown of the socket
was initiated from the other end, which means that you should close the
socket on your end to complete the shutdown. This is NOT the same as reading
the value of the Available property. Perhaps this is what the sample code
was intending.

I use a two-phase method for determining how much data to read. The 1st
phase sends a fixed size header block, with fields like this...

struct header
{
int version; // and other header stuff to detect options and/or corruption
in the sender
int size; // size of the variable length portion of the data
}

You can put anything you like in the header but you must at least put the
size there. Once you've read in the fixed size header, read the length of
the data that follows. An advantage of this mechanism is that you always
know when you've read in the entire message without needing special EOF or
formatting characters. Also, if you ever receive bad values in the header
you know that the sender has become corrupted and you should terminate the
connection.

Your code then implements a state machine, either at step 1 (reading header)
or step 2 (reading data). It's quite simple and effective.

Also, if you are using TCP then you should know that your call to receive
may not get all the data in one call...you need a buffering scheme that
reads data and adds it to a temporary buffer until the entire message has
been received by your application. In other words, the code you posted may
get partial strings because message boundaries are not respected. I use code
like this (not aysnc, I use blocking calls and separate threads per
client)...

public static void Receive(Socket s,byte[] buffer)
{
int total = 0;
int size = buffer.Length;
int dataLeft = size;
int received = 0;
int loops = 0;
while ( total < size )
{
received = s.Receive(buffe r,total,dataLef t,SocketFlags.N one);
if ( received == 0 )
return; // socket was closed by client - disconnect.
total += received;
dataLeft -= received;
loops++; // diagnostics only - this can be greater then 1
}
} // Receive

If you are using UDP then message boundaries are respected and the above
does not apply, but I don't think that is the case because then you would
not even need framing characters and you would not need to "know" the size
of the incoming message.

Hope this helps,
"Mainak Sarcar" <Ma**********@d iscussions.micr osoft.com> wrote in message
news:78******** *************** ***********@mic rosoft.com...
Very Correct... But using a terminator like that is ONLY possible if you
have control over what the server is sending..
What if I am using real world servers like POP3 or SMTP which have no
terminators to denote that all data has been received... I have tried
using
socket.Availabl e property but it is unreliable... because at times it
gives 0
bytes available, but if you wait for 1-2 secs and check the property again
... it has some bytes available for reading.. How would you get to know
when
to stop reading and when to call "beginRecei ve" method again ????

I found this crappy code from MSDN.. which is ABSOLUTELY WRONG... the
program never reaches the "else" part of the code...while the socket is
connected.
private static void ReceiveCallback ( IAsyncResult ar )
{
try
{
StateObject state = (StateObject) ar.AsyncState;
Socket client = state.workSocke t;
int bytesRead = client.EndRecei ve(ar);

if (bytesRead > 0)
{
//Get more data....

state.sb.Append (Encoding.ASCII .GetString(stat e.buffer,0,byte sRead));
client.BeginRec eive(state.buff er,0,StateObjec t.BufferSize,0,
new
AsyncCallback(R eceiveCallback) , state);
}
else
{
// All the data has arrived; put it in response.
if (state.sb.Lengt h > 1)
{
response = state.sb.ToStri ng();
}
// Signal that all bytes have been received.
receiveDone.Set ();
}
} catch (Exception e) {
Console.WriteLi ne(e.ToString() );
}
}

Nov 16 '05 #4
No David, your answer did not help me much..... Firstly the code that you
have written is a blocking type of code (synchronous). In a real world
application I can suspend the user interface in an indefinite loop till the
server sending data... Am I right. The correct approach is an Asynchronous
connection.

Your first part of the answer is correct... If the server disconnects the
socket then the client gets a received byte count of 0. But what if the
server does not disconnect the socket ... you as a client have to send some
command to the server to do that.

This is where my question arises. In the Receive Callback method how do I
get to know how much data to read. Data may be in a few bytes or it may be in
MBs..

Regards,
Mainak

"David Levine" wrote:
When a sender closes the socket the receiver is notified when the received
byte count is zero - this indicates that a graceful shutdown of the socket
was initiated from the other end, which means that you should close the
socket on your end to complete the shutdown. This is NOT the same as reading
the value of the Available property. Perhaps this is what the sample code
was intending.

I use a two-phase method for determining how much data to read. The 1st
phase sends a fixed size header block, with fields like this...

struct header
{
int version; // and other header stuff to detect options and/or corruption
in the sender
int size; // size of the variable length portion of the data
}

You can put anything you like in the header but you must at least put the
size there. Once you've read in the fixed size header, read the length of
the data that follows. An advantage of this mechanism is that you always
know when you've read in the entire message without needing special EOF or
formatting characters. Also, if you ever receive bad values in the header
you know that the sender has become corrupted and you should terminate the
connection.

Your code then implements a state machine, either at step 1 (reading header)
or step 2 (reading data). It's quite simple and effective.

Also, if you are using TCP then you should know that your call to receive
may not get all the data in one call...you need a buffering scheme that
reads data and adds it to a temporary buffer until the entire message has
been received by your application. In other words, the code you posted may
get partial strings because message boundaries are not respected. I use code
like this (not aysnc, I use blocking calls and separate threads per
client)...

public static void Receive(Socket s,byte[] buffer)
{
int total = 0;
int size = buffer.Length;
int dataLeft = size;
int received = 0;
int loops = 0;
while ( total < size )
{
received = s.Receive(buffe r,total,dataLef t,SocketFlags.N one);
if ( received == 0 )
return; // socket was closed by client - disconnect.
total += received;
dataLeft -= received;
loops++; // diagnostics only - this can be greater then 1
}
} // Receive

If you are using UDP then message boundaries are respected and the above
does not apply, but I don't think that is the case because then you would
not even need framing characters and you would not need to "know" the size
of the incoming message.

Hope this helps,
"Mainak Sarcar" <Ma**********@d iscussions.micr osoft.com> wrote in message
news:78******** *************** ***********@mic rosoft.com...
Very Correct... But using a terminator like that is ONLY possible if you
have control over what the server is sending..
What if I am using real world servers like POP3 or SMTP which have no
terminators to denote that all data has been received... I have tried
using
socket.Availabl e property but it is unreliable... because at times it
gives 0
bytes available, but if you wait for 1-2 secs and check the property again
... it has some bytes available for reading.. How would you get to know
when
to stop reading and when to call "beginRecei ve" method again ????

I found this crappy code from MSDN.. which is ABSOLUTELY WRONG... the
program never reaches the "else" part of the code...while the socket is
connected.
private static void ReceiveCallback ( IAsyncResult ar )
{
try
{
StateObject state = (StateObject) ar.AsyncState;
Socket client = state.workSocke t;
int bytesRead = client.EndRecei ve(ar);

if (bytesRead > 0)
{
//Get more data....

state.sb.Append (Encoding.ASCII .GetString(stat e.buffer,0,byte sRead));
client.BeginRec eive(state.buff er,0,StateObjec t.BufferSize,0,
new
AsyncCallback(R eceiveCallback) , state);
}
else
{
// All the data has arrived; put it in response.
if (state.sb.Lengt h > 1)
{
response = state.sb.ToStri ng();
}
// Signal that all bytes have been received.
receiveDone.Set ();
}
} catch (Exception e) {
Console.WriteLi ne(e.ToString() );
}
}


Nov 16 '05 #5

"Mainak Sarcar" <Ma**********@d iscussions.micr osoft.com> wrote in message
news:76******** *************** ***********@mic rosoft.com...
No David, your answer did not help me much..... Firstly the code that you
have written is a blocking type of code (synchronous). In a real world
application I can suspend the user interface in an indefinite loop till
the
server sending data... Am I right. The correct approach is an Asynchronous
connection.
As I said, the thread it is running on is synchronous and the socket is in
blocking mode, but it is a different thread from the main or UI thread. It
is actually far more efficient to do it that way and is more responsive to
UI events. When the thread is blocked waiting on a socket event it does not
consume any CPU cycles at all, and there is no overhead of the callback
mechanism. Internally the runtime does much the same thing - it uses a
separate thread (I think from the threadpool) on which it does its socket
processing. All you need to do is launch another thread and have it use the
socket that you created on your main or listening thread.

You should buy a book on multi-threaded socket programming. There are many
ways of achieving parallel processing. I recommned Richard Blum's book C#
Network Programming.

Your first part of the answer is correct... If the server disconnects the
socket then the client gets a received byte count of 0. But what if the
server does not disconnect the socket ... you as a client have to send
some
command to the server to do that.
All you need to do is close the socket. You can optionally shutdown the
socket first and then close the socket. There are several techniques for
closing the socket.


This is where my question arises. In the Receive Callback method how do I
get to know how much data to read. Data may be in a few bytes or it may be
in
MBs..
Read my message again about the two-phase approach. 1st read a fixed block
header that tells you how many additional bytes you need to read to get the
entire message.

Regards,
Mainak

"David Levine" wrote:
When a sender closes the socket the receiver is notified when the
received
byte count is zero - this indicates that a graceful shutdown of the
socket
was initiated from the other end, which means that you should close the
socket on your end to complete the shutdown. This is NOT the same as
reading
the value of the Available property. Perhaps this is what the sample code
was intending.

I use a two-phase method for determining how much data to read. The 1st
phase sends a fixed size header block, with fields like this...

struct header
{
int version; // and other header stuff to detect options and/or
corruption
in the sender
int size; // size of the variable length portion of the data
}

You can put anything you like in the header but you must at least put the
size there. Once you've read in the fixed size header, read the length of
the data that follows. An advantage of this mechanism is that you always
know when you've read in the entire message without needing special EOF
or
formatting characters. Also, if you ever receive bad values in the header
you know that the sender has become corrupted and you should terminate
the
connection.

Your code then implements a state machine, either at step 1 (reading
header)
or step 2 (reading data). It's quite simple and effective.

Also, if you are using TCP then you should know that your call to receive
may not get all the data in one call...you need a buffering scheme that
reads data and adds it to a temporary buffer until the entire message has
been received by your application. In other words, the code you posted
may
get partial strings because message boundaries are not respected. I use
code
like this (not aysnc, I use blocking calls and separate threads per
client)...

public static void Receive(Socket s,byte[] buffer)
{
int total = 0;
int size = buffer.Length;
int dataLeft = size;
int received = 0;
int loops = 0;
while ( total < size )
{
received = s.Receive(buffe r,total,dataLef t,SocketFlags.N one);
if ( received == 0 )
return; // socket was closed by client - disconnect.
total += received;
dataLeft -= received;
loops++; // diagnostics only - this can be greater then 1
}
} // Receive

If you are using UDP then message boundaries are respected and the above
does not apply, but I don't think that is the case because then you would
not even need framing characters and you would not need to "know" the
size
of the incoming message.

Hope this helps,
"Mainak Sarcar" <Ma**********@d iscussions.micr osoft.com> wrote in message
news:78******** *************** ***********@mic rosoft.com...
> Very Correct... But using a terminator like that is ONLY possible if
> you
> have control over what the server is sending..
> What if I am using real world servers like POP3 or SMTP which have no
> terminators to denote that all data has been received... I have tried
> using
> socket.Availabl e property but it is unreliable... because at times it
> gives 0
> bytes available, but if you wait for 1-2 secs and check the property
> again
> ... it has some bytes available for reading.. How would you get to
> know
> when
> to stop reading and when to call "beginRecei ve" method again ????
>
> I found this crappy code from MSDN.. which is ABSOLUTELY WRONG... the
> program never reaches the "else" part of the code...while the socket is
> connected.
> private static void ReceiveCallback ( IAsyncResult ar )
> {
> try
> {
> StateObject state = (StateObject) ar.AsyncState;
> Socket client = state.workSocke t;
> int bytesRead = client.EndRecei ve(ar);
>
> if (bytesRead > 0)
> {
> //Get more data....
>
> state.sb.Append (Encoding.ASCII .GetString(stat e.buffer,0,byte sRead));
> client.BeginRec eive(state.buff er,0,StateObjec t.BufferSize,0,
> new
> AsyncCallback(R eceiveCallback) , state);
> }
> else
> {
> // All the data has arrived; put it in response.
> if (state.sb.Lengt h > 1)
> {
> response = state.sb.ToStri ng();
> }
> // Signal that all bytes have been received.
> receiveDone.Set ();
> }
> } catch (Exception e) {
> Console.WriteLi ne(e.ToString() );
> }
> }
>

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
9238
by: dream machine | last post by:
Hi all , with BegeinReceive I can build async method of Socket Class that Receive the data from the Socket Client . My question is , if I have this code that create 3 Receive Async Call : <code> mySocket.BeginReceive(param1,param2,param3.....); mySocket.BeginReceive(param1,param2,param3......); mySocket.BeginReceive(param1,param2,param3......);
0
2100
by: whizpop | last post by:
Hi, First of all, thanks for a great starter kit, now If I could just get it to work (fully). I am trying to compile and run the solution/services all on a local dev box. I am able to successfully run most of the SampleApp features (Register,Feedback,Error report, version check) The only method I can not execute is the BuyNow example. I have tried leveraging the Sharewarestarterkit.com web service to rule out locality, to no avail.
6
3831
by: Shak | last post by:
Hi all, Three questions really: 1) The async call to the networkstream's endread() (or even endxxx() in general) blocks. Async calls are made on the threadpool - aren't we advised not to cause these to block? 2) You can connect together a binaryreader to a networkstream:
7
2874
by: Shak | last post by:
Hi all, I'm trying to write a thread-safe async method to send a message of the form (type)(contents). My model is as follows: private void SendMessage(int type, string message) { //lets send the messagetype via async NetworkStream ns = client.GetStream(); //assume client globally accessible
11
8635
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it completely async, supporting timeouts and some scheduling mechanisms. I use a non-blocking approach for this, using the call to 'poll' to support the async mechanism - rather than the 'begin' and 'end' functions. I already found that connecting doesn't set the "isconnected" variable correctly...
10
4521
by: Frankie | last post by:
It appears that System.Random would provide an acceptable means through which to generate a unique value used to identify multiple/concurrent asynchronous tasks. The usage of the value under consideration here is that it is supplied to the AsyncOperationManager.CreateOperation(userSuppliedState) method... with userSuppliedState being, more or less, a taskId. In this case, the userSuppliedState {really taskId} is of the object type,...
3
2691
by: Ryan Liu | last post by:
Will TcpClient.GetStream().Read()/ReadByte() block until at least one byte of data can be read? In a Client/Server application, what does it mean at the end of stream/no more data available? Client could send data once few seconds of minutes. Is there an "end" at all? In a C/S application, if server side call BeginginRead() again in EndRead() to create a endless loop to get message from client, is this a better approach than "one...
1
20668
by: Ryan Liu | last post by:
Hi, I have a 100 clients/ one server application, use ugly one thread pre client approach. And both side user sync I/O. I frequently see the error on server side(client side code is same, but I don't see the error): "System.IO.IOException: Unable to read data from the transport connection:A blocking operation was interrupted by a call to WSACancelBlockingCall"
10
8077
by: ColoradoGeiger | last post by:
I have a fairly standard server application that I am using asynchronous socket calls to make connections, send and receive data, and all of that jazz. No blocking methods was my goal in writing this and it's gone very well so far. But coming from the blocking world, I want to have some timeouts on my receive routines and I after reading a lot on MSDN and here, I get the impression that once I set one of these Begin... methods a running...
0
9793
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11151
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10858
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10420
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9582
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7974
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7134
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4619
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.