Solved

When to close socket in server ?

Posted on 2000-03-26
10
259 Views
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)

Thanks

   Oran
0
Comment
Question by:oranm
  • 4
  • 3
  • 2
  • +1
10 Comments
 

Expert Comment

by:sgold
Comment Utility
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
sgold
0
 

Author Comment

by:oranm
Comment Utility
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

Oran
0
 
LVL 3

Expert Comment

by:ghimireniraj
Comment Utility
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..
0
 

Expert Comment

by:sgold
Comment Utility
ok there is another posibilty inarite class from CSocket (CClientSocket : public CSocket)
and implemet the
void CClientSocket::OnReceive(int nErrorCode)
{
      
      CSocket::OnReceive(nErrorCode);
            
}
this event will be called as long as there is data in the socket buffer
0
 
LVL 3

Expert Comment

by:ghimireniraj
Comment Utility
>>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.


niraj
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Accepted Solution

by:
sgold earned 50 total points
Comment Utility
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)
0
 

Author Comment

by:oranm
Comment Utility
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.

Thanks

   Oran
0
 

Expert Comment

by:sneedwt
Comment Utility
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!
sneedwt



0
 

Author Comment

by:oranm
Comment Utility
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
   Oran
0
 

Author Comment

by:oranm
Comment Utility
Thanks.
still, do you now where i can read about how the lowwer lyer is handling ?

Oran
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now