473,382 Members | 1,692 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,382 software developers and data experts.

Socket State Question

Hi all,
Excuse me if this is wrong forum for this but I've implemented a
protocol handler for my socket server using the state pattern and it
works fine until I attempt to introduce heartbeats which are sent
independently of the client process on the same socket to the server.
Now the state can be processing commands and finds that it has received
a message for the client and this contravenes the protocol and state
transition is inconsistent.

So much for trying something new. What have other people used to solve
this?

It has been suggested that I use a seperate port on the same machine to
send heartbeats but, well, I want to know that that command socket is
responsive and alive.

Erm. thats it.

Ta

CJ

Nov 15 '06 #1
3 2093
"Court Jester" <co************@hotmail.co.ukwrote in message
news:11*********************@m7g2000cwm.googlegrou ps.com...
Hi all,
Excuse me if this is wrong forum for this but I've implemented a
protocol handler for my socket server using the state pattern and it
works fine until I attempt to introduce heartbeats which are sent
independently of the client process on the same socket to the server.
Now the state can be processing commands and finds that it has received
a message for the client and this contravenes the protocol and state
transition is inconsistent.

So much for trying something new. What have other people used to solve
this?
Most people don't bother with "heartbeats". You try sending, and if it
works, everything is okay. If you aren't sending, you don't care whether
the connection is valid at that moment. It could become valid by the next
time you actually need it, and so detecting that it's invalid at that moment
is counter-productive.

This oversimplifies a bit, but the bottom line is that it usually doesn't
make sense to intentionally cause an error to occur when none would
otherwise happen.

As far as your specific issue goes, well...the problem is entirely
architectural, within your own code. If you insist on using heartbeats,
then you need to build logic into your network code that can tolerate extra
communications occuring while you are "processing commands". The answer to
that has nothing to do with .NET or C#.

Pete
Nov 15 '06 #2
Are you communicating on your socket synchronously or asynchronously?
If you are using a synchronous pattern, one thing I have done in the
past is use the .DataAvailable attribute of the NetworkStream object
derived from the socket (I know, getting complicated). The idea was, if
there is no information available, go ahead and test the connection. To
test the connection I simply tried to send a predetermined "test
packet" over the connection, and if this attempt is placed in a
try/catch block then you'll know when the connection has been lost.

That said, as someone mentioned in this thread before, if you're not
using a socket you might not care if it's connected until you need it.
I guess it depends on what you're trying to do. Often times with
wireless networks, connections will be lost for a couple of seconds and
then regained. If during that time your app wasn't doing anything,
there's no reason to go through a bunch of connection lost code if the
connection will just be re-established before you're done dealing with
the fact you lost connection. Again, depends on your goals.

Hope this helps. If you think you need to see some sample code I could
do that with a better idea of your goals. Good luck!

Peter Duniho wrote:
"Court Jester" <co************@hotmail.co.ukwrote in message
news:11*********************@m7g2000cwm.googlegrou ps.com...
Hi all,
Excuse me if this is wrong forum for this but I've implemented a
protocol handler for my socket server using the state pattern and it
works fine until I attempt to introduce heartbeats which are sent
independently of the client process on the same socket to the server.
Now the state can be processing commands and finds that it has received
a message for the client and this contravenes the protocol and state
transition is inconsistent.

So much for trying something new. What have other people used to solve
this?

Most people don't bother with "heartbeats". You try sending, and if it
works, everything is okay. If you aren't sending, you don't care whether
the connection is valid at that moment. It could become valid by the next
time you actually need it, and so detecting that it's invalid at that moment
is counter-productive.

This oversimplifies a bit, but the bottom line is that it usually doesn't
make sense to intentionally cause an error to occur when none would
otherwise happen.

As far as your specific issue goes, well...the problem is entirely
architectural, within your own code. If you insist on using heartbeats,
then you need to build logic into your network code that can tolerate extra
communications occuring while you are "processing commands". The answer to
that has nothing to do with .NET or C#.

Pete
Nov 15 '06 #3
heartbeats should not effect any state in your client protocol. So process
them, but ignore them as far as your state machine. You heartbeat state can
be done on another thread and/or using timercallbacks, etc.

--
William Stacey [C# MVP]

"Court Jester" <co************@hotmail.co.ukwrote in message
news:11*********************@m7g2000cwm.googlegrou ps.com...
| Hi all,
| Excuse me if this is wrong forum for this but I've implemented a
| protocol handler for my socket server using the state pattern and it
| works fine until I attempt to introduce heartbeats which are sent
| independently of the client process on the same socket to the server.
| Now the state can be processing commands and finds that it has received
| a message for the client and this contravenes the protocol and state
| transition is inconsistent.
|
| So much for trying something new. What have other people used to solve
| this?
|
| It has been suggested that I use a seperate port on the same machine to
| send heartbeats but, well, I want to know that that command socket is
| responsive and alive.
|
| Erm. thats it.
|
| Ta
|
| CJ
|
Nov 15 '06 #4

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

Similar topics

5
by: mscirri | last post by:
The code below is what I am using to asynchronously get data from a PocketPC device. The data comes in fine in blocks of 1024 bytes but even when I send no data from the PocketPC constant blocks of...
2
by: Rene Sørensen | last post by:
We are 4 students working on a assignment, that our teacher gave use, normally we do this is C++, but the 4 of us, use C# more often that C++ so… We made a small games called reversi, now our job...
7
by: Colin | last post by:
I'm writing a little console socket server but I'm having some difficulty. Can I ask your advice - where is the best place to get some help on that topic? It would be nice if some people who knew...
2
by: Marty | last post by:
Hi, I hope you can help me with this one (my code is below my message). I have many clients connecting to my server. When any of them disconnect, something happen. This user get disconnect...
9
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so...
2
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...
0
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...
4
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...
6
by: billiejoex | last post by:
Hi there. I'm setting up test suite for a project of mine. situations, if the socket is closed on the other end or not. I noticed that I can "detect" such state if a call to socket.read() returns...
2
by: manasap | last post by:
Hi all! I've written a server and a client application using asynchronous sockets.The client sends data packets for every 7 seconds.The server receives the packets. This process proceeds...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.