Cancel blocking socket operation in WinSock 2.0

Posted on 2001-08-14
Last Modified: 2008-02-01
For some reason, I use blocking socket in my program. Each of my working thread receive UDP data package from a certain port. During the application running, everyting is ok. But when I need to exit the program and tell all the working thread to shut down, the problem occurs. I can't use closesock(...) directly (from other thread), because if I do like this, it will also block (WSARecvFrom(...) is still waiting for the next package). After checking MSDN, I found a WinSock API WSACancelBlockingCall(), but it is only supported by WinSock 1.1. Now my temporary solution is terminate the waiting thread directly, then close the socket. But I feel it not so good, have you a better solution?
Question by:xutao
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

Expert Comment

ID: 6411402

Expert Comment

ID: 6411407

Expert Comment

ID: 6411662
Don't use blocking socket calls use ansynchronous calls instead.

This way you can pass a shutdown handle as a parameter to each new thread you start.

Then in your code you can wait for the shutdownhandle to be signalled while waiting for socket events. If it is signalled do your clean up and allow the thread to exit gracefully rather than trying to terminate the thread.


Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 6789692

You have asked 21 questions here at EE, and you have only resolved 3 of them. Here is a list of your open questions. I will be posting this comment in all of them.

To assist you in your cleanup, I'm providing the following guidelines:

1.  Stay active in your questions and provide feedback whenever possible. Likewise, when feedback has not been provided by the experts, commenting again makes them receive an email notification, and they may provide you with further information. Experts have no other method of searching for questions in which they have commented, except manually.

2.  Award points by hitting the Accept Comment As Answer button located above and to the left of that expert's comment.

3.  When grading, be sure to read:
to ensure that you understand the grading system here at EE. If you grade less than an A, you must explain why.

4.  Questions that were not helpful to you should be PAQ'd (stored in the database for their valuable content?even if not valuable to you) or deleted. To PAQ or delete a question, you must first post your intent in that question to make the experts aware. Then, if no experts object after three full days, you can post a zero-point question at community support to request deletion or PAQ. Please include the link(s) to the question(s).
At that point, a moderator can refund your points and PAQ or delete the question for you. The delete button does not work.

5.  If you fail to respond to this cleanup request, I must report you to the Community Support Administrator for further action.

Our intent is to get the questions cleaned up, and not to embarrass or shame anyone. If you have any questions or need further assistance at all, feel free to ask me in this question or post a zero-point question at CS. We are very happy to help you in this task!

community support moderator


Author Comment

ID: 6791630
All your suggestions is not to use blocking socket. But what I want to know is how to make it in blocking mode.

Expert Comment

ID: 6805771
Note sent today to Admin re: remaining open questions.

Community Support Moderator
Experts Exchange

Expert Comment

ID: 6834937
Points refunded and question closed.

Community Support Moderator
Experts Exchange

Accepted Solution

Netminder earned 0 total points
ID: 6834941
Admin notified of user neglect. Points refunded and question closed.

Community Support Moderator
Experts Exchange

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

Suggested Solutions

Title # Comments Views Activity
Wireshark question 1 43
How to connect/ping from virtual (VMware) system to host system also host network ? 2 109
Low ampere 10 119
How to separate mgmt & production vm network 8 79
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
If you're not part of the solution, you're part of the problem.   Tips on how to secure IoT devices, even the dumbest ones, so they can't be used as part of a DDoS botnet.  Use PRTG Network Monitor as one of the building blocks, to detect unusual…
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

739 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