Solved

Graceful close of winsock?

Posted on 1998-07-05
1
288 Views
Last Modified: 2013-12-03
I'm trying to write a server. There is a button for the user to press to stop the server. Then the server will close all the client sockets that are connecting to it. Here is the code:
while (!m_connectionList.IsEmpty())
{
  CClientSock* pSocket          =(CClientSock*)m_connectionList.RemoveHead();
  pSocket->ShutDown();
  BYTE Buffer[50];
  while (pSocket->Receive(Buffer,50) > 0);
 delete pSocket;
}
However, the server 'hanged' at the statement : while(pSocket->Receive(Buffer, 50)>0);
But at that time, my clients were not intending to send anything to the server.
p.s. I'm using MFC the CSocket class. CClientSock is the derived class of CSocket
Why?
0
Comment
Question by:syyung6
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 6

Accepted Solution

by:
WxW earned 100 total points
ID: 1409427
Try the Close function , not the Shutdown . Shutdown leaves the socket open .

*and* if you really want to process the remaining data on the socket ( I do not understand why anyway ) ,

you should not check for >0 . Receive returns 0 if a socket is closed , and SOCKET_ERROR if an error occurs . After Shutdown , Receive returns SOCKET_ERROR and thats why your while loop halts .
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

690 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