1 thread is making an accept call in for(;;;) loop .On a certain condition closeSocket is called and it closes the same socket on which accept call is being made . The accept call gives error . I get EBDAF error on Solaris and EINVAL error on Linux . How should i overcome this problem . Can I check the socketnum state before making an accept call . How should I approach this problem .
2
There are 2 best solutions below
0
David Schwartz
On
You cannot close a socket in one thread while another thread is using it. The basic problem is that there is simply no way to know whether the other thread is using the socket or about to use the socket. And if it's about to use the socket, there are unavoidable race conditions. This mistake has caused real world problems, including one with serious security implications.
Instead, just don't close the socket. Signal the thread that might be using the socket any other way, and then have that thread close the socket.
Related Questions in C++
- C++ using std::vector across boundaries
- Linked list without struct
- Connecting Signal QML to C++ (Qt5)
- how to get the reference of struct soap inherited in C++ Proxy/Service class
- Why we can't assign value to pointer
- Conversion of objects in c++
- shared_ptr: "is not a type" error
- C++ template using pointer and non pointer arguments in a QVector
- C++ SFML 2.2 vectors
- Lifetime of temporary objects
- I want to be able to use 4 different variables in a select statement in c ++
- segmentation fault: 11, extracting data in vector
- How to catch delay-import dll errors (missing dll or symbol) in MinGW(-w64)?
- How can I print all the values in this linked list inside a hash table?
- Configured TTL for A record(s) backing CNAME records
Related Questions in LINUX
- How do I recursively find and replace only in files named index.php on Linux webserver?
- passing text with \n as one argument in shell
- kernel module does not print packet info
- How to send ESC/POS commands to thermal printer in Linux
- (x64 Nasm) Writeline function on Linux
- How do I set the Hive user to something different than the Spark user from within a Spark program?
- Default priority of thread with SCHED_FIFO
- Calling a python function with options from shell script
- How to split a directory into parts without compressing or archiving?
- Cross compile simple standard C program on Linux for Mac
- How to offload NAPI poll function to workqueue
- python netifaces - How to get currently used network interface
- Unexpected output from function
- mingw-64 conflicting declarations when cross-compiling
- Different behavior of async with Visual Studio 2013(Windows8.1) and GCC 4.9(Ubuntu14.10)
Related Questions in SOCKETS
- Drawing with ncurses, sockets and fork
- UDP sockets in C not working
- How can I send multiple objects over one socket in java?
- psuedo TCP multicast with os.dup2() in python?
- My get request for http is very slow
- Nodejs connect mysql socket to another host?
- HttpRequestContext vs HttpContext
- Spring based client server communication without network
- Java does not accept 2 methods with same name
- Retrieve Data From EOC(Eithernet Over coxial) device
- Ping a server without freezing the Thread
- C sockets: Exit client after all data is received
- What is the best way to send XML converted to a byte array over TCP, then translate the response back to readable XML?
- C# Winform to Connect to Device Using IP
- Java Restart a Socket
Related Questions in NETWORKING
- kernel module does not print packet info
- Packet drops in multicast when multiple instance of listner are running
- Timing packets on a traffic server
- How to use Espresso Idling Resource for network calls
- Dummynet does not match on flows
- Sending a notification from OS X to iOS
- Swift ios viewDidLoad or viewDidAppear
- Update player list on all clients on new connection
- Issues regarding multiplayer networking: input
- nmap does not show all open ports
- Getting and Sending Data between a Server and Client
- Read file from local PC from network deployed app to InputStream
- Does iOS block a URL if we couldn't connect to the URL for 'n' times?
- Is Socket.Available guaranteed to throw a SocketException on disconnect?
- android out of cell service
Related Questions in SOLARIS
- How to get Mouse support for dtterm terminal in solaris
- Tail command - follow by name on Solaris
- in Makefiles, how to test for gcc's --with-ld option?
- Configuring git and apache on solaris box
- How to find linux equivalent of solaris specific header files?
- How to match and remove the content preceding it from a file in unix
- writing device drivers for OpenIndiana
- How to convert Solaris MDB addresses into file names and line numbers?
- wheel group in OpenSolaris
- I want to update atime on large binary files
- Socket Descriptor closed from another thread on certain condition when accept() call on the same socket is going on in Linux?
- gzip and pipe to output (performance consideration)
- Setting LD_LIBRARY_PATH breaks my apache module, can rpath fix it?
- redirecting echo with undefined variable
- Can't take input from a terminal
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
accept will return with error because the socket (file descriptor) was closed. You can consider this error in your code.
An alternative technique for not getting error that is common in many applications (Thrift uses it): from a second thread you connect to this socket and send a special message, for example, just "1". When the server receives this message it finishes the loop and closes the socket.
Of course there is a risk of DoS attack if another machine begins to send "1" to your server. Then you need to check the message comes from the same machine and from a port used by your process. Or better, do what Martin James says below.