473,388 Members | 1,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

receiving data before Socket.EndReceive()

I am having mixed results with asynchronous socket receives.
Sometimes I get the right information back from the buffer, other
times I get some of the data that should be in the buffer printed out
to the console. That data is printed out to the console before I even
call Socket.EndReceive(). After that call, I get the rest of the data
that wasn't printed out to the console in the buffer. Anyone have
any idea what is going on?
Here's the code:
public delegate void ReceivedPacketCallBack(Packet pack);
class PacketReceiver
{
private ReceivedPacketCallBack m_newPacket;
private IOBuffer m_socketAndBuffer;
private int m_blockSize;
private uint m_packetSize;
private uint m_receivedBytes;
private List<bytem_holdBuffer;

public PacketReceiver(IOBuffer theSockBuff, int blockSize)
{
m_socketAndBuffer = theSockBuff;
m_blockSize = blockSize;
m_socketAndBuffer.ClearBuffer();
m_packetSize = 0;
m_receivedBytes = 0;
//m_holdBuffer = new List<byte>();
}

public void NeedPacket(ReceivedPacketCallBack pack)
{
m_newPacket = pack;
}

//should make sure that we receive one and only one packet
public void ReceivePacket()
{
m_socketAndBuffer.ClearBuffer();
if (m_socketAndBuffer.IsConnected)
{
AsyncCallback callMe = new
AsyncCallback(ReceiveCallBack);
m_socketAndBuffer.ResizeByteBuffer((uint)m_blockSi ze);
Console.WriteLine("The Buffer is " +
m_socketAndBuffer.m_buffer.Count + " big in bytes.");

m_socketAndBuffer.m_sockMe.BeginReceive(m_socketAn dBuffer.m_buffer,
SocketFlags.None, callMe, m_socketAndBuffer);
}
else
{
throw new Exception("The socket is no longer
connected.");
}
}

private void ReceiveCallBack(IAsyncResult ar)
{
m_socketAndBuffer = (IOBuffer)ar.AsyncState;
if (m_socketAndBuffer.IsConnected)
{
m_receivedBytes =
(uint)m_socketAndBuffer.m_sockMe.EndReceive(ar);
m_holdBuffer = new List<byte>((int)m_receivedBytes);

m_holdBuffer.AddRange(m_socketAndBuffer.m_buffer[0].Array);
m_socketAndBuffer.ClearBuffer();
m_packetSize = Packet.ReadInt32FromBytes(m_holdBuffer,
0);
Console.WriteLine("The packet size is " + m_packetSize
+ "");
UInt32 bytesLeft = m_packetSize - (uint)(m_blockSize -
4);
Console.WriteLine("We need to read " + bytesLeft + "
more bytes.");
if (bytesLeft 512000000)
{
Console.WriteLine("size to big");
}
m_socketAndBuffer.ResizeByteBuffer(bytesLeft);

AsyncCallback callme = new
AsyncCallback(CreatePacket);

m_socketAndBuffer.m_sockMe.BeginReceive(m_socketAn dBuffer.m_buffer,
SocketFlags.None, callme, m_socketAndBuffer);
}
}

private void CreatePacket(IAsyncResult ar)
{
m_socketAndBuffer = (IOBuffer)ar.AsyncState;
if (m_socketAndBuffer.IsConnected)
{
m_receivedBytes +=
(uint)m_socketAndBuffer.m_sockMe.EndReceive(ar);

m_holdBuffer.AddRange(m_socketAndBuffer.m_buffer[0].Array);
if (m_receivedBytes < (m_packetSize + 4))
{
uint leftOver = m_receivedBytes - m_packetSize +
4;
m_socketAndBuffer.ResizeByteBuffer(leftOver);
AsyncCallback callMeNow = new
AsyncCallback(CreatePacket);

m_socketAndBuffer.m_sockMe.BeginReceive(m_socketAn dBuffer.m_buffer,
SocketFlags.None, callMeNow, m_socketAndBuffer);
}
Packet thePacket = Packet.ParseBytes(m_holdBuffer,
m_blockSize);
m_newPacket(thePacket); //send the packet to
whoever needs it (delegate)
}
}
}

Aug 3 '07 #1
7 3149
da*********@gmail.com wrote:
I am having mixed results with asynchronous socket receives.
Sometimes I get the right information back from the buffer, other
times I get some of the data that should be in the buffer printed out
to the console. That data is printed out to the console before I even
call Socket.EndReceive(). After that call, I get the rest of the data
that wasn't printed out to the console in the buffer. Anyone have
any idea what is going on?
Here's the code:
That is both not enough code and too much, all at the same time. You
are missing half of the connection (the sending side) as well as the
implementation of IOBuffer, and as well you have included a fair amount
of code that is likely not necessary to reproduce the issue (in fact,
unless IOBuffer is directly related to the problem, you should post code
that doesn't even include it).

And you have not precisely described what the actual problem is. What
output do you get, and what output do you expect instead?

So, if you really want someone to spend more than a few moments with
your code, you should distill it down into something that is
concise-but-complete while reliably reproducing the problem. Post that
along with a precise problem description.

That said, as near as I can tell from my quick scan of your code, you
appear to deliver an assembled "packet" whether it's completed or not.
I would expect that to be a problem.

There are plenty of other things I would change about the code, but that
stands out as being particularly likely to produce incorrect results.

Pete
Aug 3 '07 #2
On Aug 2, 8:44 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
darthgha...@gmail.com wrote:
I am having mixed results with asynchronous socket receives.
Sometimes I get the right information back from the buffer, other
times I get some of the data that should be in the buffer printed out
to the console. That data is printed out to the console before I even
call Socket.EndReceive(). After that call, I get the rest of the data
that wasn't printed out to the console in the buffer. Anyone have
any idea what is going on?
Here's the code:

That is both not enough code and too much, all at the same time. You
are missing half of the connection (the sending side) as well as the
implementation of IOBuffer, and as well you have included a fair amount
of code that is likely not necessary to reproduce the issue (in fact,
unless IOBuffer is directly related to the problem, you should post code
that doesn't even include it).

And you have not precisely described what the actual problem is. What
output do you get, and what output do you expect instead?

So, if you really want someone to spend more than a few moments with
your code, you should distill it down into something that is
concise-but-complete while reliably reproducing the problem. Post that
along with a precise problem description.

That said, as near as I can tell from my quick scan of your code, you
appear to deliver an assembled "packet" whether it's completed or not.
I would expect that to be a problem.

There are plenty of other things I would change about the code, but that
stands out as being particularly likely to produce incorrect results.

Pete
Here is the section of code where I use the PacketReceiver:
private void EndStart(IAsyncResult ar)
{
Socket temp = (Socket)ar.AsyncState;
if (temp.Connected)
{
int sentBytes = temp.EndSend(ar);
List<ArraySegment<byte>tempBuffer = new
List<ArraySegment<byte>>();
tempBuffer.Add(new ArraySegment<byte>(new byte[255]));
receiveBuffer.m_buffer = tempBuffer;
AsyncCallback callMe = new
AsyncCallback(EndReceiveVersionString);

receiveBuffer.m_sockMe.BeginReceive(receiveBuffer. m_buffer,
SocketFlags.None, callMe, receiveBuffer);
}
}

private void EndReceiveVersion(IAsyncResult ar)
{
Encoder theEncoder = new Encoder();
IOBuffer temp = (IOBuffer)ar.AsyncState;
if (temp.m_sockMe.Connected)
{
int receivedBytes = temp.m_sockMe.EndReceive(ar);

Packet sendAlgs = new Packet();
sendAlgs.Fill(sendData);
ArraySegment<bytetoBuff = new
ArraySegment<byte>(sendAlgs.GetPacketInBytes());
sendBuffer.m_buffer.Add(toBuff);
sendBuffer.m_sockMe.BeginSend(sendBuffer.m_buffer,
SocketFlags.None, callMe, sendBuffer);
}
}
I have not experienced problems sending, just receiving. The output I
am seeing in the console is data sent from the client, but only the
first part of it. The rest of the data sent from the client is in the
buffer like it should be. The packet is assembled by first reading
the length (which should be the first of the packet), then doing
another receive call to get the rest of the packet. Since some of the
data received is sent to the console instead of the buffer, I get a
bad length when I try to do the second receive call. For example, if
the client sends a packet like this:
23somedata
the console would display the following (which it shouldn't):
23som
and the buffer would contain:
edata
Does that make sense?
Thanks for your time.

Aug 3 '07 #3
On Aug 2, 9:43 pm, darthgha...@gmail.com wrote:
On Aug 2, 8:44 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
darthgha...@gmail.com wrote:
I am having mixed results with asynchronous socket receives.
Sometimes I get the right information back from the buffer, other
times I get some of the data that should be in the buffer printed out
to the console. That data is printed out to the console before I even
call Socket.EndReceive(). After that call, I get the rest of the data
that wasn't printed out to the console in the buffer. Anyone have
any idea what is going on?
Here's the code:
That is both not enough code and too much, all at the same time. You
are missing half of the connection (the sending side) as well as the
implementation of IOBuffer, and as well you have included a fair amount
of code that is likely not necessary to reproduce the issue (in fact,
unless IOBuffer is directly related to the problem, you should post code
that doesn't even include it).
And you have not precisely described what the actual problem is. What
output do you get, and what output do you expect instead?
So, if you really want someone to spend more than a few moments with
your code, you should distill it down into something that is
concise-but-complete while reliably reproducing the problem. Post that
along with a precise problem description.
That said, as near as I can tell from my quick scan of your code, you
appear to deliver an assembled "packet" whether it's completed or not.
I would expect that to be a problem.
There are plenty of other things I would change about the code, but that
stands out as being particularly likely to produce incorrect results.
Pete

Here is the section of code where I use the PacketReceiver:
private void EndStart(IAsyncResult ar)
{
Socket temp = (Socket)ar.AsyncState;
if (temp.Connected)
{
int sentBytes = temp.EndSend(ar);
List<ArraySegment<byte>tempBuffer = new
List<ArraySegment<byte>>();
tempBuffer.Add(new ArraySegment<byte>(new byte[255]));
receiveBuffer.m_buffer = tempBuffer;
AsyncCallback callMe = new
AsyncCallback(EndReceiveVersionString);

receiveBuffer.m_sockMe.BeginReceive(receiveBuffer. m_buffer,
SocketFlags.None, callMe, receiveBuffer);
}
}

private void EndReceiveVersion(IAsyncResult ar)
{
Encoder theEncoder = new Encoder();
IOBuffer temp = (IOBuffer)ar.AsyncState;
if (temp.m_sockMe.Connected)
{
int receivedBytes = temp.m_sockMe.EndReceive(ar);

Packet sendAlgs = new Packet();
sendAlgs.Fill(sendData);
ArraySegment<bytetoBuff = new
ArraySegment<byte>(sendAlgs.GetPacketInBytes());
sendBuffer.m_buffer.Add(toBuff);
sendBuffer.m_sockMe.BeginSend(sendBuffer.m_buffer,
SocketFlags.None, callMe, sendBuffer);
}
}
I have not experienced problems sending, just receiving. The output I
am seeing in the console is data sent from the client, but only the
first part of it. The rest of the data sent from the client is in the
buffer like it should be. The packet is assembled by first reading
the length (which should be the first of the packet), then doing
another receive call to get the rest of the packet. Since some of the
data received is sent to the console instead of the buffer, I get a
bad length when I try to do the second receive call. For example, if
the client sends a packet like this:
23somedata
the console would display the following (which it shouldn't):
23som
and the buffer would contain:
edata
Does that make sense?
Thanks for your time.
Also, please feel free to make suggestions on how I can improve my
code. I'm really new to this and would appreciate any tips anyone
might have. If there are any good books on sockets or writing good
code please feel free to suggest them.
Thanks for your time.

Aug 3 '07 #4
da*********@gmail.com wrote:
[...]
Now comes the hard
part, the redesign. Would it be better to read one byte at a time
looking for line feed or read chunks looking for the same thing and
saving the rest? If anyone knows of some good examples or articles on
good ways to design this type of thing please feel free to suggest
them.
Generally speaking, you should receive larger blocks at a time, to make
most efficient use of the network i/o. Then you can parcel that data
out as needed to the actual application end of things.

As another suggestion regarding your code: IMHO, it's not a good idea to
have two different receive callbacks. I understand that you have
different things to do when receiving the data, and that's what led to
the two different callbacks. But your network i/o should be more
separated from the application logic than that (see above).

Even once you get things separated better, IMHO it will still make more
sense to have a general-purpose application data-handling method, but at
least if you don't do it that way, your actual network i/o code remains
simple.

Pete
Aug 3 '07 #5
On Aug 3, 2:58 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
darthgha...@gmail.com wrote:
[...]
Now comes the hard
part, the redesign. Would it be better to read one byte at a time
looking for line feed or read chunks looking for the same thing and
saving the rest? If anyone knows of some good examples or articles on
good ways to design this type of thing please feel free to suggest
them.

Generally speaking, you should receive larger blocks at a time, to make
most efficient use of the network i/o. Then you can parcel that data
out as needed to the actual application end of things.

As another suggestion regarding your code: IMHO, it's not a good idea to
have two different receive callbacks. I understand that you have
different things to do when receiving the data, and that's what led to
the two different callbacks. But your network i/o should be more
separated from the application logic than that (see above).

Even once you get things separated better, IMHO it will still make more
sense to have a general-purpose application data-handling method, but at
least if you don't do it that way, your actual network i/o code remains
simple.

Pete
I'll give that a shot. Thanks again for your time.

Aug 3 '07 #6
You're trying to do the same sorts of things we do in our XMPP SDK.

We read from the various socket streams, parse out XML Fragments, turn them
into packets, match them against classes, instantiate the right object,
serialize the xml into the object, and then raise the correct event.

Our SDK has source code available, and you can pull it down and look at how
we're doing everything:
http://developers.coversant.net/Down...3/Default.aspx

What you're looking for is (mostly) found in the two classes:
Coversant.SoapBox.Base.XMPPSocket
Coversant.SoapBox.Base.XMLStreamReceiver

Our approach is very scalable, and quite reliable. Our use of socket buffers
that comes out of a buffer pool for reads/write, minimizes heap
fragmentation.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

<da*********@gmail.comwrote in message
news:11*********************@e16g2000pri.googlegro ups.com...
>I am having mixed results with asynchronous socket receives.
Sometimes I get the right information back from the buffer, other
times I get some of the data that should be in the buffer printed out
to the console. That data is printed out to the console before I even
call Socket.EndReceive(). After that call, I get the rest of the data
that wasn't printed out to the console in the buffer. Anyone have
any idea what is going on?

Aug 3 '07 #7
On Aug 3, 4:29 pm, "Chris Mullins [MVP]" <cmull...@yahoo.comwrote:
You're trying to do the same sorts of things we do in our XMPP SDK.

We read from the various socket streams, parse out XML Fragments, turn them
into packets, match them against classes, instantiate the right object,
serialize the xml into the object, and then raise the correct event.

Our SDK has source code available, and you can pull it down and look at how
we're doing everything:http://developers.coversant.net/Down...3/Default.aspx

What you're looking for is (mostly) found in the two classes:
Coversant.SoapBox.Base.XMPPSocket
Coversant.SoapBox.Base.XMLStreamReceiver

Our approach is very scalable, and quite reliable. Our use of socket buffers
that comes out of a buffer pool for reads/write, minimizes heap
fragmentation.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVPhttp://www.coversant.com/blogs/cmullins

<darthgha...@gmail.comwrote in message

news:11*********************@e16g2000pri.googlegro ups.com...
I am having mixed results with asynchronous socket receives.
Sometimes I get the right information back from the buffer, other
times I get some of the data that should be in the buffer printed out
to the console. That data is printed out to the console before I even
call Socket.EndReceive(). After that call, I get the rest of the data
that wasn't printed out to the console in the buffer. Anyone have
any idea what is going on?
Thank you. I'll take a look at that over the weekend and see what I
can learn from it.
Thanks again for the time.

Aug 4 '07 #8

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

Similar topics

4
by: Pete Davis | last post by:
I've written an async socket server app and I'm having an issue with the EndReceive() call. My begin receive call is: sockState.RemoteSocket.BeginReceive(sockState.ReceiveBuffer, 0,...
4
by: Brian Rice | last post by:
I have a socket application that is sending and receiving packets asynchronously. It works great except, when I receive packets that are larger than my receive buffer which then generate several...
0
by: André Betz | last post by:
Hi, I've established a TCP-Socket Connection and now I want to receive datas. So I called BeginReceive where I set the receiving buffer and length. Now in the asynchronous CallBack-Function I...
6
by: Steve Richter | last post by:
I dont get the point of socket.BeginReceive and socket.EndReceive. As I understand it, BeginReceive will start a 2nd thread, call the ReceiveCallback delegate in the 2nd thread, then block until...
0
by: Christoph Bisping | last post by:
Hello! I'm not sure if this is the right place to ask my question... According to the docs, it is possible to receive udp datagrams asynchronous using the Socket-class and its .BeginRecive(From)...
7
by: Crirus | last post by:
Hi all! I use a webClient for requesting data from a server of mine. Should I worry about long ammount of data sent by server in the client side? Or, another way, should I send some kind of a...
2
by: Macca | last post by:
My app has an asynchronous socket server. It will have 20 clients connected to the server. Each client sends data every 500 millisecondsThe Connections once established will not be closed unless...
8
by: Dinsdale | last post by:
I am trying to write a Tcp "Server" that opens a class that wraps a tcp socket when a new connection is made (Listener.AcceptSocket()). Everything is going swimmingly except when I try to close the...
0
by: george585 | last post by:
Hello! I am new to network programming, and understand just basics. Using some sample code, and having read documentation, I managed to create a simple app in C# and VB.NET. The application is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.