Solved

Graceful close of winsock?

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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 how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

789 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