Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nonblocking sockets: Are messages queued?

As I understand, it is possible to create a nonblocking network socket in PHP 5.x.

But what happens if a script sends several long messages using the same nonblocking socket as follow:

socket_write($socket, $string1, $length);
socket_write($socket, $string2, $length);
socket_write($socket, $string3, $length);
socket_write($socket, $string4, $length);

Are these messages queued (on the sender/receiver side?) or is it possible that the receiver gets parts of different messages because they sent "parallel"?

For example: Is is possible that the receiver gets 10 bytes of $string1, then 30 bytes of $string2, then another 25 bytes of $string1 ... and so on....

like image 686
Mike Avatar asked Mar 16 '12 15:03

Mike


People also ask

Is Message Queue a Websocket?

The two are totally different in the sense that; Socket allows connection between clients who know themselves whiles (eg between a client and a backend service or between backend services). Message queue mostly acts as an interface between different backend services in a message-driven system.

What is blocking and nonblocking socket?

A socket can be in "blocking mode" or "nonblocking mode." The functions of sockets in blocking (or synchronous) mode do not return until they can complete their action. This is called blocking because the socket whose function was called cannot do anything — is blocked — until the call returns.

Is socket listen blocking?

Listen causes a connection-oriented Socket to listen for incoming connection attempts. The backlog parameter specifies the number of incoming connections that can be queued for acceptance. To determine the maximum number of connections you can specify, retrieve the MaxConnections value. Listen does not block.

What is blocking vs nonblocking?

Blocking refers to operations that block further execution until that operation finishes while non-blocking refers to code that doesn't block execution. Or as Node. js docs puts it, blocking is when the execution of additional JavaScript in the Node. js process must wait until a non-JavaScript operation completes.


1 Answers

It depends on the protocol that the socket is using. See socket_create for the possible types of sockets. The main types are UDP and TCP:

udp The User Datagram Protocol is a connectionless, unreliable, protocol with fixed record lengths. Due to these aspects, UDP requires a minimum amount of protocol overhead.

tcp The Transmission Control Protocol is a reliable, connection based, stream oriented, full duplex protocol. TCP guarantees that all data packets will be received in the order in which they were sent. If any packet is somehow lost during communication, TCP will automatically retransmit the packet until the destination host acknowledges that packet. For reliability and performance reasons, the TCP implementation itself decides the appropriate octet boundaries of the underlying datagram communication layer. Therefore, TCP applications must allow for the possibility of partial record transmission.

To answer your question directly, TCP sockets will guarantee in-order delivery, whereas UDP sockets will not.

like image 101
Justin Ethier Avatar answered Sep 21 '22 05:09

Justin Ethier