473,795 Members | 2,914 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

asynchronous socket operations - question

I have a question about C#. How can I stop asynchronous read/write operation
( BeginReceive() / BeginSend() ) if timeout occurs?
The Socket class doesn't make any cancel method available.
I used CancelIo(HANDLE hFile) method (declared in Winbase.h) in C++.

Thanks for help.
Olek
Nov 15 '05 #1
5 5646
You could always spawn a new thread which then calls the synchronous
version, into which you can place a timeout parameter.

"a.kostrzew a" <aX.kostrzewaX@ remove_this_and _X.uposX.comX.p lX> wrote in
message news:bm******** **@nemesis.news .tpi.pl...
I have a question about C#. How can I stop asynchronous read/write operation
( BeginReceive() / BeginSend() ) if timeout occurs?
The Socket class doesn't make any cancel method available.
I used CancelIo(HANDLE hFile) method (declared in Winbase.h) in C++.

Thanks for help.
Olek

Nov 15 '05 #2
I might be wrong here.. but, can you call EndSend() to complete the
operation?

--
Girish Bharadwaj
"Daniel Bass" <I'm really @ sick of spam> wrote in message
news:O8******** ******@TK2MSFTN GP10.phx.gbl...
You could always spawn a new thread which then calls the synchronous
version, into which you can place a timeout parameter.

"a.kostrzew a" <aX.kostrzewaX@ remove_this_and _X.uposX.comX.p lX> wrote in
message news:bm******** **@nemesis.news .tpi.pl...
I have a question about C#. How can I stop asynchronous read/write operation ( BeginReceive() / BeginSend() ) if timeout occurs?
The Socket class doesn't make any cancel method available.
I used CancelIo(HANDLE hFile) method (declared in Winbase.h) in C++.

Thanks for help.
Olek

Nov 15 '05 #3
Yes, of course.
I call asynchronous read operation in this way:

IAsyncResult oResult = oSocket.BeginRe ceive(...);
oWH = m_oResult .AsyncWaitHandl e;
// oEventsHandler - my class object
oEventsHandler. WaitForGroupEQ( Timeout.Infinit e ,nGroupId,out nEvent);
int nNumberOfBytesR ead = ReadEnd();

But I can't break this operation.

Best regards,
Olek

Użytkownik "Girish Bharadwaj" <girishb.at.mvp s.dot.org> napisał w wiadomo¶ci
news:OM******** ******@TK2MSFTN GP09.phx.gbl...
I might be wrong here.. but, can you call EndSend() to complete the
operation?

--
Girish Bharadwaj
"Daniel Bass" <I'm really @ sick of spam> wrote in message
news:O8******** ******@TK2MSFTN GP10.phx.gbl...
You could always spawn a new thread which then calls the synchronous
version, into which you can place a timeout parameter.

"a.kostrzew a" <aX.kostrzewaX@ remove_this_and _X.uposX.comX.p lX> wrote in
message news:bm******** **@nemesis.news .tpi.pl...
I have a question about C#. How can I stop asynchronous read/write

operation
( BeginReceive() / BeginSend() ) if timeout occurs?
The Socket class doesn't make any cancel method available.
I used CancelIo(HANDLE hFile) method (declared in Winbase.h) in C++.

Thanks for help.
Olek


Nov 15 '05 #4
"a.kostrzew a" <aX.kostrzewaX@ remove_this_and _X.uposX.comX.p lX> wrote in message news:<bm******* ***@atlantis.ne ws.tpi.pl>...
Yes, of course.
I call asynchronous read operation in this way:

IAsyncResult oResult = oSocket.BeginRe ceive(...);
oWH = m_oResult .AsyncWaitHandl e;
// oEventsHandler - my class object
oEventsHandler. WaitForGroupEQ( Timeout.Infinit e ,nGroupId,out nEvent);
int nNumberOfBytesR ead = ReadEnd();

But I can't break this operation.

Olek -

From what I can tell from your code it looks like you are blocking
the main thread until the EndReceive() callback method fires. This
somewhat defeats the purpose of using an asynchronous socket call.
Instead of waiting indefinitely for an answer, why don't you put a
reasonable timeout time in the WaitForGroupEQ method. When the timeout
is reached you know there wasn't a response. If you then Close() the
Socket object, the EndReceive() method will fire, and throw an
Exception. You should catch the Exception in a try-catch block and
handle it accordingly.

Alternatively, you can use the blocking Receive() method, and set
the ReceiveTimeout Socket option property to a reasonable value. The
Receive() method will throw an Exception if the timeout is reached
before data is received. Hope this helps solve your problem.

Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyT...471433012.html
Nov 15 '05 #5
I'm grateful for help.
But in Yours solution I can't send any message after timeout on receive
(socket is closed!).
This way I would like to avoid Close() method.
Unfortunely I can't use alternatively method - blocking Receive().

Olek

Uzytkownik "Rich Blum" <ri*******@juno .com> napisal w wiadomosci
news:cc******** *************** ***@posting.goo gle.com...
"a.kostrzew a" <aX.kostrzewaX@ remove_this_and _X.uposX.comX.p lX> wrote in

message news:<bm******* ***@atlantis.ne ws.tpi.pl>...
Yes, of course.
I call asynchronous read operation in this way:

IAsyncResult oResult = oSocket.BeginRe ceive(...);
oWH = m_oResult .AsyncWaitHandl e;
// oEventsHandler - my class object
oEventsHandler. WaitForGroupEQ( Timeout.Infinit e ,nGroupId,out nEvent);
int nNumberOfBytesR ead = ReadEnd();

But I can't break this operation.

Olek -

From what I can tell from your code it looks like you are blocking
the main thread until the EndReceive() callback method fires. This
somewhat defeats the purpose of using an asynchronous socket call.
Instead of waiting indefinitely for an answer, why don't you put a
reasonable timeout time in the WaitForGroupEQ method. When the timeout
is reached you know there wasn't a response. If you then Close() the
Socket object, the EndReceive() method will fire, and throw an
Exception. You should catch the Exception in a try-catch block and
handle it accordingly.

Alternatively, you can use the blocking Receive() method, and set
the ReceiveTimeout Socket option property to a reasonable value. The
Receive() method will throw an Exception if the timeout is reached
before data is received. Hope this helps solve your problem.

Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyT...471433012.html

Nov 15 '05 #6

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

Similar topics

3
11280
by: Jacob | last post by:
I'm writing a class that communicates with a server using the TcpClient class. Most of the methods I've written are intended to be used synchronously and will block until they are completed. But these calls will be made very seldom and between operations I would like to put the client into a "listening mode" that will listen for other server messages. When the client is ready to send/receive data again, take it back out of listening...
3
2495
by: Matthew King | last post by:
Hi all I've written a asynchronous socket client class, but i've found that in order to consume it I have to use events, and cannot simply for example SocketClient client = new SocketClient(110, "some.server.com") client.Connect() client.SendData("Hello World") Instead I have to wait for the async method to raise a Connected event, and call client.SendData from there, for complex chains of operations this because a nightmare chain of...
4
5435
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the server. Data is sent and received over these connections using the asynchronous model. The server is currently in beta testing. Sporadically over the course of the day, I'll observe the thread count on the process (via perfmon) start climbing....
0
2399
by: Richard | last post by:
Hi, I'm suffering a socket race condition - I think. The code works fine at full speed on a single CPU machine. However when I run full speed on a 2 Zeon machine the socket drops data on an asynchronous receive. If I slow a connected client down by step debugging or putting Thread.Sleep() calls between sends then the code works on the Zeon machine. Below is pseudo code that removes all error checking, try catch, business logic, and...
2
2187
by: Ronodev.Sen | last post by:
the way my program needs to go is -- 1) open a socket and listen on it 2) moment a client connects to the socket - process some data (by sending it to another machine), get the result and send it back to the SAME socket - the data isnt a large value (measured in bytes rather than MB or GB) i TRIED thinking of this in the Asynchronous way - BeginReceive - then pass to the OnClient Connected handler, which calls a Wait For Data
2
6879
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 there is a problem with the connection. I need to know which client has sent the incoming data as each client has its own buffer on my "server" app. I am using the standard asynch socket code from MSDN to listen for connections and they...
0
4693
by: Macca | last post by:
Hi, I am writing an asychronous socket server to handle 20+ simulataneous connections. I have used the example in MSDN as a base. The code is shown at end of question. Each connection has a number of different types of data coming in. I have a databuffer for each type of data coming in.
1
1572
by: DaTurk | last post by:
Hi, Lets see, for arguements sake lets just say that I have a server, which site waiting to receive connections, it then has an array of sockets that are connected to it. It's receiving all of this asynchronously. Now, this is my problem, I have a seperate application that connects to the server and can disconnect any of the sockets connected to it. But, lets just say I have one client connected, and then I attempt to disconnect them...
4
3605
by: Engineerik | last post by:
I am trying to create a socket server which will listen for connections from multiple clients and call subroutines in a Fortran DLL and pass the results back to the client. The asynchronous socket client and asynchronous socket server example code provided in the .NET framework developers guide is a great start but I have not dealt with sockets before and I am struggling with something. From what I can tell the sample server code ...
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
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,...
1
10167
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
10003
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
9046
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
7544
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
6784
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();...
0
5566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2922
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.