Sockets Winsock async blocking Read Write simultaneously - multithreading

I have a client server arch and I am using blocking win sockets. I have a read and a write thread both on the server and on the client side.
Say Client is waiting (blocked) on a read() call for server to write stuff to,
can The client write something to that socket while it is blocked on a read() call from another thread.
So does block affect full duplex bidirectonal sockets ? I understand that I will block on the read but why would it also block on the write()?
or in order for this to work do I have to use select() or poll()?
Thanks

can The client write something to that socket while it is blocked on a read() call from another thread
Yes, this is no problem at all.

Related

Multi-threading using a share socket in Haskell

In Haskell, can the same socket be shared between two threads? Essentially, what I am trying to do is create a thread to write to a socket and another to read from the socket.
The read thread would be a loop that constantly wait, monitor and pick up any messages generated by the socket.
Another thread (the write thread) would send code to the socket.
Please advise.
Yes, this is allowed.

Client hangs after pthread_exit() in server thread in C

I have a server where I handle multiple clients. Each client that connects to it is serviced in its own thread. Now, if any errors occur on the server side, I want to exit that thread by calling pthread_exit, and terminate the client that was being serviced by that thread. However; when I try to do so, my client is hanging. Also, this causes other clients that are in different threads to hang as well. I called pthread_exit in a random spot to test it...
Most likely the problem is that you are not calling close(newsockfd) before you call pthread_exit(). If so, then your server-thread goes away, but the socket that it was using to communicate with the client remains open, even though the server is no longer doing anything with it. Then the client's outgoing TCP buffer fills up, and the client waits indefinitely for the server to recv() more data from the socket, which never happens.

Can I send() from one thread and recv() from another on the same ZeroMQ REQ/REP Socket?

As the documents say, ZMQ sockets are not thread-safe. So I assume the answer to the title is 'No'.
Still, I can't figure out how to implement a non-blocking request-reply pattern using ZMQ:
Specifically, the client has a main thread which goes on about its business processing messages from a (thread-safe) message queue. The messages come from various sources, such as network, timers, I/O etc.
Occasionally the main thread wishes to send a request to a remote server, but it does not want to wait for a response (which may take a while to arrive).
Normally, I would use two threads:
The main message-processing loop thread. This will send() request on the REQ/REP socket
An auxiliary listener thread which will wait for the response from the server. This will use a blocking recv() on the socket, and push the responses to the main thread's queue.
How would I achieve this using ZeroMQ? Should the auxilary thread open an inproc socket and listen to messages from the main thread?
Actually, single thread is enough. Just send request to the server and poll messages with zmq_poll().
This model is fine if one request at a time is suffucient. If you need so send multiple requests and read replies asynchronously, use DEALER socket instead of REQ. Just send some requestId as the first frame, then add empty delimiter frame, then send actual request.
Chapter 3 of the guide has more details about REQ/REP message envelopes: http://zguide.zeromq.org/php:chapter3
Please let me know if this isn't clear enough, I will probably extend my answer with few code samples.

non-blocking socket client connection

ALL,
I am looking for a simple example of non-blocking socket connection that will run on Windows.
I tried to Google, but all samples are either for *nix (POSIX) or blocking sockets on Windows.
Looking thru msdn I see that it is easy to make a socket non-blocking and issue a connect(), but then you need some preparation in order to put the socket back.
So, all in all I need something on a non-blocking socket that will connect and then put it back to be blocking.
The read and write operation should be performed on the blocking socket.
The reason for a non-blocking socket is that I need a connection timeout and there is no other way than non-blocking socket. Or is there?
Thank you.
Use ACE socket wrapper facade and apply your Linux knowledge in full.

Socket fd in multithreading

Is it ok to use same socket fd in multiple threads ? (over linux)
It depends on what you expect to happen, but yes you can. If you are reading UDP packets that are each a complete message this can work well. If you are reading data streams from TCP this will probably not work well.
You can have multiple threads accepting incoming connections on a socket and that can work pretty well as each thread gets one connection.
You will run into concurrency problems trying to send() or recv() (SOCK_STREAM) from multiple threads. You should use a critical section or some other means of creating serial access to send() and recv().
One standard thing to do with threads is to have the master listen() & accept(), then pass off the socket to a client thread that sends/receives/processes from that one socket. The client thread is responsible for calling close() or shutdown().

Resources