When to close socket in server ?

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)


Who is Participating?
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)
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
oranmAuthor Commented:
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

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.

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

oranmAuthor Commented:
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.


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!

oranmAuthor Commented:
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
oranmAuthor Commented:
still, do you now where i can read about how the lowwer lyer is handling ?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.