Solved

Graceful close of winsock?

Posted on 1998-07-05
1
273 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
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
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…
This video discusses moving either the default database or any database to a new volume.

708 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

10 Experts available now in Live!

Get 1:1 Help Now