Multiple asynchronous sockets

R

Ryan

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?
 
M

Mubashir Khan

arent u using BeginConnect,BeginReceive,BeginSend etc ....... I guess these
are executed on thread pool
 
R

Ryan

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?
 
H

Henning Krause [MVP - Exchange]

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
 
M

Mubashir Khan

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 ......
 
R

Ryan

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
 
H

Henning Krause [MVP - Exchange]

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
}
 
R

Ryan

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.

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.
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]"
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
message
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
message
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?- Hide quoted text -- Show quoted text -- Hide quoted
text -- Show quoted text -- Hide quoted text -- Show quoted text -- Hide quoted text -- Show quoted text -
 
H

Henning Krause [MVP - Exchange]

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


Ryan said:
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.

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

}
message

Sorry, meant to say BeginConnect is NOT halting my main thread in my
service app.
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]"
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
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
messagenews:[email protected]...
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?- Hide quoted text -- Show quoted text -- Hide quoted
text -- Show quoted text -- Hide quoted text -- Show quoted
text -- Hide quoted text -- Show quoted text -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top