Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / January 2007

Tip: Looking for answers? Try searching our database.

Multiple asynchronous sockets

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ryan - 24 Jan 2007 14:31 GMT
Hello all,

This is my first post so I thanks in advance.  I'm writing a windows
service application that connects to four different servers (to
elaborate they are machinery equipment) to retrieve information via
tcp.  I wrote a "controller" class that manages the connection and
communcation for each individual machine.  My reads are asynchronous
because my application needs to be able to "handshake" with the machine
when getting the data.  Sends are synchronous.  I was working with
TcpClient object and got it working however I need to be able to detect
when I'm not connected to the machine so I switch to using the lower
level Socket class.

My dilemma is this.  If the object is not connected to a machine for
any reason (i.e. machine is off for service or off hours), I want to be
able to keep trying to re-connect to the machine.  But I don't want the
"re-connect" to block communication with the other machines.  Any ideas?
Mubashir Khan - 24 Jan 2007 14:36 GMT
arent u using BeginConnect,BeginReceive,BeginSend etc ....... I guess these
are executed on thread pool
> Hello all,
>
[quoted text clipped - 13 lines]
> able to keep trying to re-connect to the machine.  But I don't want the
> "re-connect" to block communication with the other machines.  Any ideas?
Ryan - 24 Jan 2007 15:04 GMT
I'm using BeginReceive and I thought about using BeginConnect, but what
do I need to do tell that I'm waiting for async callback.  The process
is that I connect to the machine, then send a command.  So I need to be
able to wait for the connect before I send my data.  Thought about
using socket.Connect() but that will block all thread right?

> arent u using BeginConnect,BeginReceive,BeginSend etc ....... I guess these
>
[quoted text clipped - 15 lines]
> > able to keep trying to re-connect to the machine.  But I don't want the
> > "re-connect" to block communication with the other machines.  Any ideas?- Hide quoted text -- Show quoted text -
Henning Krause [MVP - Exchange] - 24 Jan 2007 15:21 GMT
Hello,

when you use BeginConnect, you can pass along a callback delegate, which
will be called, once the connection has been established or when the
connection failed. In this callback, you call EndConnect. After that, you
can send data using the Send method.

This way, you won't block the base thread.

Best regards,
Henning Krause

> I'm using BeginReceive and I thought about using BeginConnect, but what
> do I need to do tell that I'm waiting for async callback.  The process
[quoted text clipped - 23 lines]
>> > "re-connect" to block communication with the other machines.  Any
>> > ideas?- Hide quoted text -- Show quoted text -
Ryan - 24 Jan 2007 16:29 GMT
Thanks for the help thus far.

BeginConnect is halting the main thread.  Let me clarify some things.
Since I have to manually handshake with the device, I wrote a
controller class to handle communication for each machine.  In my
service app, I have a collection of "controller" objects to connect to
each machine.  When it starts, I iterate through each object to
connect, then send data to start the data retrieval from the machine.
I'm using a network stream to handle the data, but its being called
before the async call back.  I tried a ManualResetEvent object but that
didn't work.  But my issue still is how can say (for each controller
object) connect to your machine and send the data but only if the
connection has been made.  If I can get that working, then I should be
able to figure out how to reconnect to the machine if its turned off
without affecting the "controller" objects

On Jan 24, 10:21 am, "Henning Krause [MVP - Exchange]"
<newsgroups_rem...@this.infinitec.de> wrote:
> Hello,
>
[quoted text clipped - 35 lines]
> >> > "re-connect" to block communication with the other machines.  Any
> >> > ideas?- Hide quoted text -- Show quoted text -- Hide quoted text -- Show quoted text -
Ryan - 24 Jan 2007 16:30 GMT
Sorry, meant to say BeginConnect is NOT halting my main thread in my
service app.

> Thanks for the help thus far.
>
[quoted text clipped - 54 lines]
> > >> > "re-connect" to block communication with the other machines.  Any
> > >> > ideas?- Hide quoted text -- Show quoted text -- Hide quoted text -- Show quoted text -- Hide quoted text -- Show quoted text -
Henning Krause [MVP - Exchange] - 24 Jan 2007 16:47 GMT
Of course it's not blocking your thread - that's what it's for.

As I said: pass a callback delegate to BeginConnect and move all your
sending code to the delegate.

Sync:
socket.Connect(...);

socket.Send(...);

Async:

void main() {
socket.beginconnect(endpoint, mycallback, socket);
}

void mycallback(IAsyncResult result) {
   Socket socket;

  socket = (Socket) result.AsyncState;
   socket.EndConnect(result);

   socket.Send(...);
}

Best regards,
Henning Krause
}

> Sorry, meant to say BeginConnect is NOT halting my main thread in my
> service app.
[quoted text clipped - 73 lines]
>> > >> > ideas?- Hide quoted text -- Show quoted text -- Hide quoted
>> > >> > text -- Show quoted text -- Hide quoted text -- Show quoted text -
Ryan - 24 Jan 2007 18:12 GMT
Thanks Henning,

I moved my initial command (that tells the machine start sending me
data) to the connect method and thats working.  Can't believe it was
that simple.  However, I do want to try to reconnect if the socket
connection failed because the host didn't respond.  I know that I can
put a socket.BeginConnect in my SocketException catch but my fear is
that I'll overload the stack by doing (n) socket.BeginConnect calls.
Any thoughts?

Thanks again.

On Jan 24, 11:47 am, "Henning Krause [MVP - Exchange]"
<newsgroups_rem...@this.infinitec.de> wrote:
> Of course it's not blocking your thread - that's what it's for.
>
[quoted text clipped - 101 lines]
> >> > >> > ideas?- Hide quoted text -- Show quoted text -- Hide quoted
> >> > >> > text -- Show quoted text -- Hide quoted text -- Show quoted text -- Hide quoted text -- Show quoted text -
Henning Krause [MVP - Exchange] - 24 Jan 2007 21:28 GMT
Hello,

no problem with stack overflows - after the BeginConnect you are leaving the
method and thereby the current stack. The next call of your delegate is on
another thread with a brand new stack :-)

Best regards,
Henning Krause

> Thanks Henning,
>
[quoted text clipped - 125 lines]
>> >> > >> > text -- Show quoted text -- Hide quoted text -- Show quoted
>> >> > >> > text -- Hide quoted text -- Show quoted text -
Mubashir Khan - 24 Jan 2007 15:27 GMT
beginconnect is your choice ... it will return in the callback function u
provide .... with the status of the connection...... call EndConnect there
to finialize async call ..... Afterwards send data if the socket has been
connected sucessfully .... otherwise another beginconnect request ......
> I'm using BeginReceive and I thought about using BeginConnect, but what
> do I need to do tell that I'm waiting for async callback.  The process
[quoted text clipped - 23 lines]
>> > "re-connect" to block communication with the other machines.  Any
>> > ideas?- Hide quoted text -- Show quoted text -

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.