473,750 Members | 2,541 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3551
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.v oid> wrote in message
news:OU******** ******@TK2MSFTN GP15.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******** ************@co mcast.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.v oid> wrote in message
news:OU******** ******@TK2MSFTN GP15.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.v oid> wrote in message
news:uW******** ********@tk2msf tngp13.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******** ************@co mcast.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.v oid> wrote in message
news:OU******** ******@TK2MSFTN GP15.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******** ************@co mcast.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.v oid> wrote in message
news:uW******** ********@tk2msf tngp13.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******** ************@co mcast.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.v oid> wrote in message
news:OU******** ******@TK2MSFTN GP15.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.Wa itHandle.

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
1602
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 to client connected to it from a function through a separate thread. and echo all the connections opened and closed. the client script is working fine. it echoes the countdown it recieve from server.
4
18132
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 as I find appropriate. I am using the socket asynchronously by calling the BeingSend and BeginReceive calls. I would like to be able to call shutdown and close asynchronously if possible.
2
702
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 more data to expect). Sometimes, Socket.Available returns 0 but the other end of the connection did not close it ! 2) This class will be used by many other classes so I have to use the
2
2879
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 is to make a server, none of us know nothing about socket programming in C#, but we founds some guides for this, and now ,got a server running, but we have some problems though. We have 2 scenario, one where we use a telnet connection and one...
2
4153
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 I get the last chunk of data and am not able to retrieve it anymore cause the socket will be dead. Loop: { socket.Receive <----------- data arrives
4
3604
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
4622
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 a thread yet), print "Test\n" to the thread, and close everything. It fails on creating the client thread with error 106: 'Transport endpoint is already connected'
5
12802
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 more incoming connections and does the BeginAccpet() again. It does an infinite loop this way. My question is: What is the best way to stop this? I thought about putting a boolean in there, but then what if it's still waiting for an incoming...
0
1775
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 bindings for socket connections and TCL gui examples. I would like to get one of the demo samples work with Python/wxPython. I am including the TCL code for the drums demo (this was the shortest one). Has anybody converted this to wxPython? ...
0
8838
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,...
0
9396
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9256
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
8263
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...
0
6081
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
4716
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2226
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.