When to close socket in server ?

Posted on 2000-03-26
Last Modified: 2013-11-20
I spawn a server thread and let it block.
whnever accept is succcessul I go through a loop - create a client thread - give it the new client socket and return to accept again.

The problem is When to kill the spawn thread and close this socket ?
Please give answe to the case that the clients close the socket at the end of communication, and when they don't.
(how to decide that this client don't want to send me anything anymore)

By the way, I still don't use CAsyncSocket, but if it wil be any help, maybe i will not have a choice)


Question by:oranm
  • 4
  • 3
  • 2
  • +1

Expert Comment

ID: 2660699
first if u deside on the protocol u
can deside that the claint have to send u on the first packet the size of buffer
that he is sending secound you can
add timer to cheack if the socket is open to long close it (i even think there is a time out property on CSocket but i am not sure)
hope its help

Author Comment

ID: 2661173
Thanks sgold.
I know that if i can decide on the protocol, i can decide that the client should close the socket or tell me how much is the buffer length.

The problem is that i am not in kind of position, so i am looking for a way to know that the client finish to send the message.
i don't want to analyze the message in a low level, in order to know if i read all its bytes.

I can close socket after a timeout, but i would like to know that there has been silence on the line for this period of time.

I also don't want to make problems to the client if i close the socket before he finish to send all his messages.

Maybe it can't be done, but i would like to wait for another idea.

Thanks again


Expert Comment

ID: 2661557
You expection is beyond the client server application model.

It is like client --server---client  model.

I too want to know more what others say..
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


Expert Comment

ID: 2663136
ok there is another posibilty inarite class from CSocket (CClientSocket : public CSocket)
and implemet the
void CClientSocket::OnReceive(int nErrorCode)
this event will be called as long as there is data in the socket buffer

Expert Comment

ID: 2664058
>>this event will be called as long as there is data in the socket buffer

Yes that is right but that do you mean he should close his  connection when there is no data.
That is not the soln.

And even if he waits for a silence for an amount of time at recieving port he won't be sure whether to close the socket.

I think the only way you can be sure is buy encapsulating your data in some protocol , this protocol may be one that you define your self and you must manipulate the data in Low level and see for a connection close request by the client.


Accepted Solution

sgold earned 50 total points
ID: 2664362
You should build an modal of listening socket and chiled one
the listening is wllayes open for connections when a call is made to the server he creates child socket and append it to the connecttion
this socket is used only for one call when he recive the message he
can store it in a list ans close the chiled connection, it finesed his job.
he offcourse can deside on aprotocol that will send back to
the caller an "resived ok" and then close
 in any case the claints obay
to the server rulls and not viseversa
 unless the protocol is inside the app and desided by VP R&D
or usinfg a Known protocol (like in the web)

Author Comment

ID: 2665314
Hello sgold && niraj

Thanks a lot for your responds.
I read it very carefully, and i think that you made me realize that i will have to determine something for the protocol and force customers to live with my adaptations.

In your letters i saw two things that will help me close this question (it about time, isn't it)
those are:
"see for a connection close request by the client"   and   "send back to the caller an received ok and then close".

I would like to work with Win API Berkeley sockets, or in the "worst" case the CAsyncSocket, and not the CSocket.

Can you explain how i do those two thing you told me to do, using stantard Win API ?

also, i am not familiar with the Protocol that runs bellows the API so f someone knows where to read about it, i will be gratefull.



Expert Comment

ID: 2675226
I'm not sure I understand all of your requests, but I understand the following:

You want to know when the client has finshed sending all of his data so you can close the socket.

You have a few options
1) Develop a standard message structure for the data transfer, where the first unsigned long of the data is the length of the data.  Then, you will be able to tell when all the data has been received, so you can close the socket.
2) Analyize the data in some other fasishon to determine if the socket has been closed.
3) Develop a standard message system, where the first long is the length of the message, the second is a key, and anything that follows is a key-dependent data.  In this fashion, the client can send a series of mesages to the server, one of which can be a close attempt.
4) Use CAsyncSocket, have the client close his socket, or program when the data has been sent.  In the mean time, on the server, you'll be processing the OnReceive messages.  When the client closes the socket, you'll get an OnClose message, you know you've received all the data.

Hope this helps.  BTW, I'd recommend using CAsyncSocket, as it encapsulates a lot of the BSD stuff for you, you just have to be carefull about your thread model (the thread that creates the socket must be the thread that handles all of the call-back messages).  Personally, I use create a UserInterface thread, have my socket a member of the CWinThread derived UI thread, and post messages to it when I want the thread to perform actions.

Best of luck!


Author Comment

ID: 2686886
Thanks a lot sneedwt

I think you gave a good answer, and I would like to give you some points, but i think sgold invested a lot of time, so i will give hime the poiints (beside i tink that most of the things you said, he also said)

Thanks anyway

Author Comment

ID: 2686894
still, do you now where i can read about how the lowwer lyer is handling ?


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Prevent this page from creating additional dialogs. 3 624
noX challenge 17 130
Work with App store 7 69
How do I get Window Title of all opened process? 4 15
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question