Solved

Graceful close of winsock?

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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