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

Socket connection

Hi all.
I'm trying first time async socket connection.
In all the examples i've seen, the server connection is closed
when the message is complete.
Is it common to close the connection after every message?
Also, the message is complete, meaning there is no more data to read, only
when the client closes the connection.
My solution is to keep the connection open, and send a terminator / eof
at the end of every message.
Is it the right solution?
Thanks,
Sharon.
Feb 23 '06 #1
6 3512
There are a few things to think about when working with sockets and your own
protocol (ie. the messages that are sent and received.)

1. What is the message format? In other words how is the data encoded that
is being sent. Maybe it is an XML format or HTTP request, or maybe it is
some binary block of data similar to what a game would use.

2. Is there a way to determine the size of a packet or the end of it? If it
is a binary block of data maybe the first 2 bytes represent the size/length
of the packet, if it is HTTP then a CR+LF+CR+LF can signify the end of it or
a Content-Length header.

3. Is there always going to be a reply to every request? Will you ever send
something and not expect/require a reply.

4. Do you want to support persistant connections on your servers side? This
can either increase or decrease the load on your server depending on how
your system works. If you are goin to make a bunch of requests and then none
for a while then you probably want to use a persistant connection for all
the requests and then close it. If you will make one request every few
minutes then maybe you should not use persistant connections at all. This
is all up to you and how you want to implement it but keep in mind that no
matter what there will be more logic/code involved in using persistant
connections then not using them (what if the connection gets closed and you
dont expect it, can both server and client close the connection, etc.)

5. Are you going to be communicating with systems that use different
programming languages? This is just something to keep in your mind when
deciding on the protocol you will use. Something like XML is non-language
specific while something like a binary structure is usually language
specific.

Now.. To answer your question a bit more in case the above didnt help you
out. Ready for it.. here it comes...

It is up to you how your system will work in terms of opening, closing and
retaining a connection!

That is the overall answer I was getting at I guess.. It is up to you and
the system you are designing (server, clients, etc). Here are some
rule-of-thumb's that may help out though.

1. Where do persistant connections work better?
- Systems that require a login or keep track of a transaction. Each
connection is a "user" that is logged in and on the server it is handled by
a separate thread.

- If a client will make several requests in a short period of time. Like a
web browser or database client.

- If there is some load intense process with every new connection. Maybe
for every new connection the users IP address is checked against some
backend system and then some processing is performed.

- Will connection be able to communicate with each other like a chat
program? Maybe each connection is a user and they will talk with each other
via text messages.

2. Where do non-persistant connection work better?
- A server that is used to query data and does not know about the "user"
connected to it. This type of server generally handles a single request
(query) and provides the answer to the client. There is no notion of a
session or user in this setup.

- If a client only makes 1 request every few minutes. My waste server
resources for a client that only talks once in a while?
That about covers what I am trying to get at. I hope that this gives you
some help in some way. If not then let me know!
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Charles Cox
VC/VB/C# Developer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

"Sharon" <no*****@null.void> wrote in message
news:OU**************@TK2MSFTNGP15.phx.gbl...
Hi all.
I'm trying first time async socket connection.
In all the examples i've seen, the server connection is closed
when the message is complete.
Is it common to close the connection after every message?
Also, the message is complete, meaning there is no more data to read, only
when the client closes the connection.
My solution is to keep the connection open, and send a terminator / eof
at the end of every message.
Is it the right solution?
Thanks,
Sharon.

Feb 23 '06 #2
Hi Charles.
Thanks a bunch for your detailed reply.
Some details about the app:
1. the server will send a message at least every few seconds.
2. message format is XML.
3. each connection is a logged user .
4. users will be able to chat through the server.

So i guess i'll need to append eof to every message and use persistent
connection.
My problem now, is persistent connections management.
I'm thinking about a time interval maintenance routine.
When a disconnected worker socket is detected, should it wait another round
for
the socket to reconnect, before removing it from the worker sockets list?
Sharon.

"C.C. (aka Me)" <me@home.com> wrote in message
news:Ab********************@comcast.com...
There are a few things to think about when working with sockets and your
own protocol (ie. the messages that are sent and received.)

1. What is the message format? In other words how is the data encoded that
is being sent. Maybe it is an XML format or HTTP request, or maybe it is
some binary block of data similar to what a game would use.

2. Is there a way to determine the size of a packet or the end of it? If
it is a binary block of data maybe the first 2 bytes represent the
size/length of the packet, if it is HTTP then a CR+LF+CR+LF can signify
the end of it or a Content-Length header.

3. Is there always going to be a reply to every request? Will you ever
send something and not expect/require a reply.

4. Do you want to support persistant connections on your servers side?
This can either increase or decrease the load on your server depending on
how your system works. If you are goin to make a bunch of requests and
then none for a while then you probably want to use a persistant
connection for all the requests and then close it. If you will make one
request every few minutes then maybe you should not use persistant
connections at all. This is all up to you and how you want to implement
it but keep in mind that no matter what there will be more logic/code
involved in using persistant connections then not using them (what if the
connection gets closed and you dont expect it, can both server and client
close the connection, etc.)

5. Are you going to be communicating with systems that use different
programming languages? This is just something to keep in your mind when
deciding on the protocol you will use. Something like XML is non-language
specific while something like a binary structure is usually language
specific.

Now.. To answer your question a bit more in case the above didnt help you
out. Ready for it.. here it comes...

It is up to you how your system will work in terms of opening, closing and
retaining a connection!

That is the overall answer I was getting at I guess.. It is up to you and
the system you are designing (server, clients, etc). Here are some
rule-of-thumb's that may help out though.

1. Where do persistant connections work better?
- Systems that require a login or keep track of a transaction. Each
connection is a "user" that is logged in and on the server it is handled
by a separate thread.

- If a client will make several requests in a short period of time. Like
a web browser or database client.

- If there is some load intense process with every new connection. Maybe
for every new connection the users IP address is checked against some
backend system and then some processing is performed.

- Will connection be able to communicate with each other like a chat
program? Maybe each connection is a user and they will talk with each
other via text messages.

2. Where do non-persistant connection work better?
- A server that is used to query data and does not know about the "user"
connected to it. This type of server generally handles a single request
(query) and provides the answer to the client. There is no notion of a
session or user in this setup.

- If a client only makes 1 request every few minutes. My waste server
resources for a client that only talks once in a while?
That about covers what I am trying to get at. I hope that this gives you
some help in some way. If not then let me know!
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Charles Cox
VC/VB/C# Developer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

"Sharon" <no*****@null.void> wrote in message
news:OU**************@TK2MSFTNGP15.phx.gbl...
Hi all.
I'm trying first time async socket connection.
In all the examples i've seen, the server connection is closed
when the message is complete.
Is it common to close the connection after every message?
Also, the message is complete, meaning there is no more data to read,
only
when the client closes the connection.
My solution is to keep the connection open, and send a terminator / eof
at the end of every message.
Is it the right solution?
Thanks,
Sharon.


Feb 23 '06 #3
Sharon,

Glad the info helped.

One thing that I have seen done in multi-threaded communication applications
is the implementation of a "vulture". This is a separate thread that is
sleeping most of the time but when it is not then it goes through the list
of all of the current connections and determines if they should be removed
or not. It will do this by looking to see if the connection is still open
(at the socket level) and maybe how much time has passed since the last
request was made from the client - maybe it will close/remove any
connections after 10 minutes of inactivity. This is similar to what you
mentioned in terms of a time interval maintance routine.

It sounds as though you will need persistant connections since there will be
chatting between the users. Also since you are using some sort of login
method (each connection is a logged user you said) it will make it easier
because the actual socket connection can be used to identify the user
instead of having to pass a "key" with every request to the server.

Lastly, you mentioned waiting for a reconnect to occur when a connection is
lost. This is a good idea but it could be hard to implement in a
correct/good way. Depending on your requirements you may want to hold off
implementing it in the beginning and see if you really need it. To
determine if you really need this type of behavior you may want to look at
how often a connection is actually dropped (aka. lost connection) and what
benifit you will receive from implementing it - is it really that big of a
deal to just create a new worker thread and perform the login again? Is
there a lot of "session" type of information that gets created that could
benifit from a "continue where you left off" feature because that is what we
are talking about really.

Hope this helps!

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Charles Cox
VC/VB/C# Developer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

"Sharon" <no*****@null.void> wrote in message
news:uW****************@tk2msftngp13.phx.gbl...
Hi Charles.
Thanks a bunch for your detailed reply.
Some details about the app:
1. the server will send a message at least every few seconds.
2. message format is XML.
3. each connection is a logged user .
4. users will be able to chat through the server.

So i guess i'll need to append eof to every message and use persistent
connection.
My problem now, is persistent connections management.
I'm thinking about a time interval maintenance routine.
When a disconnected worker socket is detected, should it wait another
round for
the socket to reconnect, before removing it from the worker sockets list?
Sharon.

"C.C. (aka Me)" <me@home.com> wrote in message
news:Ab********************@comcast.com...
There are a few things to think about when working with sockets and your
own protocol (ie. the messages that are sent and received.)

1. What is the message format? In other words how is the data encoded
that is being sent. Maybe it is an XML format or HTTP request, or maybe
it is some binary block of data similar to what a game would use.

2. Is there a way to determine the size of a packet or the end of it? If
it is a binary block of data maybe the first 2 bytes represent the
size/length of the packet, if it is HTTP then a CR+LF+CR+LF can signify
the end of it or a Content-Length header.

3. Is there always going to be a reply to every request? Will you ever
send something and not expect/require a reply.

4. Do you want to support persistant connections on your servers side?
This can either increase or decrease the load on your server depending on
how your system works. If you are goin to make a bunch of requests and
then none for a while then you probably want to use a persistant
connection for all the requests and then close it. If you will make one
request every few minutes then maybe you should not use persistant
connections at all. This is all up to you and how you want to implement
it but keep in mind that no matter what there will be more logic/code
involved in using persistant connections then not using them (what if the
connection gets closed and you dont expect it, can both server and client
close the connection, etc.)

5. Are you going to be communicating with systems that use different
programming languages? This is just something to keep in your mind when
deciding on the protocol you will use. Something like XML is non-language
specific while something like a binary structure is usually language
specific.

Now.. To answer your question a bit more in case the above didnt help you
out. Ready for it.. here it comes...

It is up to you how your system will work in terms of opening, closing
and retaining a connection!

That is the overall answer I was getting at I guess.. It is up to you and
the system you are designing (server, clients, etc). Here are some
rule-of-thumb's that may help out though.

1. Where do persistant connections work better?
- Systems that require a login or keep track of a transaction. Each
connection is a "user" that is logged in and on the server it is handled
by a separate thread.

- If a client will make several requests in a short period of time. Like
a web browser or database client.

- If there is some load intense process with every new connection. Maybe
for every new connection the users IP address is checked against some
backend system and then some processing is performed.

- Will connection be able to communicate with each other like a chat
program? Maybe each connection is a user and they will talk with each
other via text messages.

2. Where do non-persistant connection work better?
- A server that is used to query data and does not know about the "user"
connected to it. This type of server generally handles a single request
(query) and provides the answer to the client. There is no notion of a
session or user in this setup.

- If a client only makes 1 request every few minutes. My waste server
resources for a client that only talks once in a while?
That about covers what I am trying to get at. I hope that this gives you
some help in some way. If not then let me know!
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Charles Cox
VC/VB/C# Developer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

"Sharon" <no*****@null.void> wrote in message
news:OU**************@TK2MSFTNGP15.phx.gbl...
Hi all.
I'm trying first time async socket connection.
In all the examples i've seen, the server connection is closed
when the message is complete.
Is it common to close the connection after every message?
Also, the message is complete, meaning there is no more data to read,
only
when the client closes the connection.
My solution is to keep the connection open, and send a terminator / eof
at the end of every message.
Is it the right solution?
Thanks,
Sharon.



Feb 24 '06 #4
Hi,
I already implemented a session mechanism, so i'll keep it, to let users
regain session when reconnecting, if the session hasn't timed out yet.
So now the only thing i have to do, is to implement a vulture, shouldn't be
too hard.
I really appreciate your guidance, thanks,
Sharon.

"C.C. (aka Me)" <me@home.com> wrote in message
news:Aq********************@comcast.com...
Sharon,

Glad the info helped.

One thing that I have seen done in multi-threaded communication
applications is the implementation of a "vulture". This is a separate
thread that is sleeping most of the time but when it is not then it goes
through the list of all of the current connections and determines if they
should be removed or not. It will do this by looking to see if the
connection is still open (at the socket level) and maybe how much time has
passed since the last request was made from the client - maybe it will
close/remove any connections after 10 minutes of inactivity. This is
similar to what you mentioned in terms of a time interval maintance
routine.

It sounds as though you will need persistant connections since there will
be chatting between the users. Also since you are using some sort of
login method (each connection is a logged user you said) it will make it
easier because the actual socket connection can be used to identify the
user instead of having to pass a "key" with every request to the server.

Lastly, you mentioned waiting for a reconnect to occur when a connection
is lost. This is a good idea but it could be hard to implement in a
correct/good way. Depending on your requirements you may want to hold off
implementing it in the beginning and see if you really need it. To
determine if you really need this type of behavior you may want to look at
how often a connection is actually dropped (aka. lost connection) and what
benifit you will receive from implementing it - is it really that big of a
deal to just create a new worker thread and perform the login again? Is
there a lot of "session" type of information that gets created that could
benifit from a "continue where you left off" feature because that is what
we are talking about really.

Hope this helps!

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Charles Cox
VC/VB/C# Developer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

"Sharon" <no*****@null.void> wrote in message
news:uW****************@tk2msftngp13.phx.gbl...
Hi Charles.
Thanks a bunch for your detailed reply.
Some details about the app:
1. the server will send a message at least every few seconds.
2. message format is XML.
3. each connection is a logged user .
4. users will be able to chat through the server.

So i guess i'll need to append eof to every message and use persistent
connection.
My problem now, is persistent connections management.
I'm thinking about a time interval maintenance routine.
When a disconnected worker socket is detected, should it wait another
round for
the socket to reconnect, before removing it from the worker sockets list?
Sharon.

"C.C. (aka Me)" <me@home.com> wrote in message
news:Ab********************@comcast.com...
There are a few things to think about when working with sockets and your
own protocol (ie. the messages that are sent and received.)

1. What is the message format? In other words how is the data encoded
that is being sent. Maybe it is an XML format or HTTP request, or maybe
it is some binary block of data similar to what a game would use.

2. Is there a way to determine the size of a packet or the end of it? If
it is a binary block of data maybe the first 2 bytes represent the
size/length of the packet, if it is HTTP then a CR+LF+CR+LF can signify
the end of it or a Content-Length header.

3. Is there always going to be a reply to every request? Will you ever
send something and not expect/require a reply.

4. Do you want to support persistant connections on your servers side?
This can either increase or decrease the load on your server depending
on how your system works. If you are goin to make a bunch of requests
and then none for a while then you probably want to use a persistant
connection for all the requests and then close it. If you will make one
request every few minutes then maybe you should not use persistant
connections at all. This is all up to you and how you want to implement
it but keep in mind that no matter what there will be more logic/code
involved in using persistant connections then not using them (what if
the connection gets closed and you dont expect it, can both server and
client close the connection, etc.)

5. Are you going to be communicating with systems that use different
programming languages? This is just something to keep in your mind when
deciding on the protocol you will use. Something like XML is
non-language specific while something like a binary structure is usually
language specific.

Now.. To answer your question a bit more in case the above didnt help
you out. Ready for it.. here it comes...

It is up to you how your system will work in terms of opening, closing
and retaining a connection!

That is the overall answer I was getting at I guess.. It is up to you
and the system you are designing (server, clients, etc). Here are some
rule-of-thumb's that may help out though.

1. Where do persistant connections work better?
- Systems that require a login or keep track of a transaction. Each
connection is a "user" that is logged in and on the server it is handled
by a separate thread.

- If a client will make several requests in a short period of time.
Like a web browser or database client.

- If there is some load intense process with every new connection.
Maybe for every new connection the users IP address is checked against
some backend system and then some processing is performed.

- Will connection be able to communicate with each other like a chat
program? Maybe each connection is a user and they will talk with each
other via text messages.

2. Where do non-persistant connection work better?
- A server that is used to query data and does not know about the "user"
connected to it. This type of server generally handles a single request
(query) and provides the answer to the client. There is no notion of a
session or user in this setup.

- If a client only makes 1 request every few minutes. My waste server
resources for a client that only talks once in a while?
That about covers what I am trying to get at. I hope that this gives you
some help in some way. If not then let me know!
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Charles Cox
VC/VB/C# Developer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

"Sharon" <no*****@null.void> wrote in message
news:OU**************@TK2MSFTNGP15.phx.gbl...
Hi all.
I'm trying first time async socket connection.
In all the examples i've seen, the server connection is closed
when the message is complete.
Is it common to close the connection after every message?
Also, the message is complete, meaning there is no more data to read,
only
when the client closes the connection.
My solution is to keep the connection open, and send a terminator / eof
at the end of every message.
Is it the right solution?
Thanks,
Sharon.



Feb 24 '06 #5
No need to spin another thread, just do it in the listen loop when Now >
NextCollectTime.

--
William Stacey [MVP]
Feb 26 '06 #6
| For async ones: in callback method thread can wait for specific timeout on
IAsyncResult.WaitHandle.

But that means you still need a thread blocking on Wait for some timeout
which would negate the async benefit. So you would need a thread blocking on
Wait for each socket or would need a thread pooling after some timeout.
Feb 26 '06 #7

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

Similar topics

0
by: Hameed Khan | last post by:
hi all, i am getting some problems with my first socket script. can any one of you point me why this is happening. the server script suppose to accept one connection at a time and send countdown...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
2
by: Droopy | last post by:
Hi, I try to implement a reusable socket class to send and receive data. It seems to work but I have 2 problems : 1) I rely on Socket.Available to detect that the connection is closed (no...
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...
2
by: Nuno Magalhaes | last post by:
I've got a simple problem I guess. How do I know when a connection is terminated without losing any data? I do something like the code below, but sometimes between socket.Receive and socket.Send...
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...
0
by: palmem | last post by:
I am trying to write a simple FTP server in order to learn about sockets This is my first time trying sockets This code should take a connection on port 8110, dump it to a client "thread" (not...
5
by: darthghandi | last post by:
I've created a class to listen to all interfaces and do a BeginAccept(). Once it gets a connection, it passes the connected socket off and stores it in a List. Next, it continues to listen for...
0
by: Mangabasi | last post by:
Howdy, I would like to use the Synthesis Toolkit for a demo. I downloaded the STK from http://ccrma.stanford.edu/software/stk/index.html. It seems very powerful and user friendly. There are...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
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: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.