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

Canceling beginreceive without closing the socket

I am having an issue canceling a beginReceive call on a timeout. For
example, the following function:

public int ReadBytes(Socket theSock, byte[] arr, int
startByte, int length, int timeout)
{
IAsyncResult result = theSock.BeginReceive(arr, startByte,
length, SocketFlags.None, null, null);

if (result.AsyncWaitHandle.WaitOne(timeout, false) ==
false)
{
throw new Exception("Timeout reading from socket!");
}

return theSock.EndReceive(result);
}

If the wait handle.waitOne returns false, the function throws the
exception as desired. However, if the timeout was 1000 ms and data
came in at 1001 ms, I would detect a timeout, but the bytes would
still have been read, and I would have no way of knowing about it. In
this case if there is a timeout, I will simply call this function
again. However I will have missed the bytes that got read the last
time because I had no way of canceling the endReceive, or no way of
being sure that between the end of the waitOne and throwing of the
exception, data didn't make it into the array.

Basically I would want to add a function like:

if (result.AsyncWaitHandle.WaitOne(timeout, false) ==
false)
{
int numRead =
theSock.endReadAndReturnMeTheNumberReadIfAny();
if (numRead == 0) {
throw new Exception("Timeout reading from
socket!");
}
else {
return numread;
}

}

It would be nice if I could just call theSock.endReceive() but that
will block until data is received, and I need it to be non blocking.

I hope I'm missing something trivial. Perhaps I should just not call
my ReadBytes function unless theSock.Available returns 0?

Thanks,
Ryan

Oct 23 '07 #1
0 1616

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

Similar topics

3
by: TP-Software | last post by:
Hi, This code doesn't seem to work it always says "there is more data" and also this method is only called once private void AsyncReadCallBack(IAsyncResult asyncResult) {
2
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 : ...
0
by: ?lafur Helgi R?gnvaldsson | last post by:
I'm building a server application which accepts socket connections and I ran into some problems. The socket is asynchronous and therefore uses the BeginXXX and EndXXX methods in the Socket class...
0
by: J Brad | last post by:
Hi every body I wrote a Asynch Server using ManualResetEvent.Reset(), ManualResetEvent.Set(), ManualResetEvent.WaitOne() events I'm receiving a Message (Example: "Hello Word") without Terminator...
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...
1
by: Marty | last post by:
Hi, I have a socket that always seek for incoming data. Between Point A and Point B, the socket (mySocket is closed and assigned to nothing in another part of my program (happen when a...
9
by: semedao | last post by:
Hi, I am using sync and async operations on the same socket. generally I want the socket to wait on BeginReceive and to not block the object thread. but in some cases I want to stop the...
22
by: semedao | last post by:
Hi , I am using asyc sockets p2p connection between 2 clients. when I debug step by step the both sides , i'ts work ok. when I run it , in somepoint (same location in the code) when I want to...
2
by: O.B. | last post by:
In the following code snippet, the thread successfully makes it to the line where it waits for data to be received. Then the client closes the connection. The thread wakes up and returns from the...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
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,...

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.