?
Solved

Graceful close of winsock?

Posted on 1998-07-05
1
Medium Priority
?
294 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 200 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

801 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