473,405 Members | 2,171 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,405 software developers and data experts.

Problem with Socket.EndReceive Returning Invalid Value

I have a C# Socket configured for streaming TCP. Upon making a valid
connection, I have an asynchronous callback configured using
BeginReceive. Within the BeginReceive declaration, I set the socket
flags to SocketFlags.Partial to allow my callback to be invoked as
soon as it arrives rather than waiting for the receive buffer to fill
up.

The problem I am having is that when I invoke the
Socket.EndReceive(asyncResult) operation within the callback, it
sometimes returns a value less than what is stored in the receive
buffer. For example, the receive buffer of 2048 bytes has 76 bytes of
real data in the buffer. However, the Socket.EndReceive operation
returned 74 when it was invoked.

Is this a known bug with .NET 2.0? Or am I doing something wrong
syntactically?

Sep 27 '07 #1
2 3124
O.B. wrote:
I have a C# Socket configured for streaming TCP. Upon making a valid
connection, I have an asynchronous callback configured using
BeginReceive. Within the BeginReceive declaration, I set the socket
flags to SocketFlags.Partial to allow my callback to be invoked as
soon as it arrives rather than waiting for the receive buffer to fill
up.

The problem I am having is that when I invoke the
Socket.EndReceive(asyncResult) operation within the callback, it
sometimes returns a value less than what is stored in the receive
buffer. For example, the receive buffer of 2048 bytes has 76 bytes of
real data in the buffer. However, the Socket.EndReceive operation
returned 74 when it was invoked.

Is this a known bug with .NET 2.0? Or am I doing something wrong
syntactically?
Without seeing the code, it's hard to say. However, some suggestions:

* As long as the next call to Begin/EndReceive returns the correct
subsequent bytes, I wouldn't not worry too much if it _looks_ like you
have valid data beyond that. It might just be coincidence, or data left
from a previous receive (depending on how you manage the buffer).

* I don't understand why you are setting SocketFlags.Partial.
That's for message-oriented communications, and should be entirely
irrelevant for a TCP socket. With or without the flag, TCP will return
some data as soon as it can, whether or not there was enough to fill the
buffer you've provided.

Pete
Sep 27 '07 #2
On Sep 27, 5:41 pm, "O.B." <funkj...@bellsouth.netwrote:
I have a C# Socket configured for streaming TCP. Upon making a valid
connection, I have an asynchronous callback configured using
BeginReceive. Within the BeginReceive declaration, I set the socket
flags to SocketFlags.Partial to allow my callback to be invoked as
soon as it arrives rather than waiting for the receive buffer to fill
up.

The problem I am having is that when I invoke the
Socket.EndReceive(asyncResult) operation within the callback, it
sometimes returns a value less than what is stored in the receive
buffer. For example, the receive buffer of 2048 bytes has 76 bytes of
real data in the buffer. However, the Socket.EndReceive operation
returned 74 when it was invoked.

Is this a known bug with .NET 2.0? Or am I doing something wrong
syntactically?

Dang. I hate it when this happens ... I was doing something wrong.
EndReceive() is correct; my code was referencing a bad buffer.

Sep 27 '07 #3

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...
6
by: Bruce Vander Werf | last post by:
I am using the asynchronous send/receive methods of the Socket class. When the remote end closes the socket, the callback for receive is called and EndReceive returns 0. Socket.Connected still...
0
by: Adam Clauss | last post by:
I have a server written that upon getting a connection, immediately does: Packet packet = new Packet(); clientSocket.BeginReceive(packet.buffer, 0, packet.buffer.Length, 0, new...
1
by: Andre | last post by:
Hi, I'm implementing an asynchron socket Receiving method and I have a question about EndReceive. Read() { Socket.BeginReceive(buf,0,buf.Length,0,new AsyncCallback(ReadCallback),dp); }
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: Ben | last post by:
I modified the logmonitor sdk example so it would work over a network. It works great when the client and server are running on the same PC and have administrator privileges. So I have two...
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...
10
by: Clayton | last post by:
Hi all, I'm trying to develop a server that listens to incoming calls using the asycnhronous methods BeginAccept / EndAccept. I start the server (till this point it is ok) and few seconds later...
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: 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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
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...
0
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,...
0
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...

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.