At the top of your application, include vmci_sockets.h and declare a constant for the socket buffer size. In the example below,
BUFSIZE defines the socket buffer size. The number 4096 is a good choice for efficiency on multiple platforms. It is not based on the size of a UDP datagram.
VMCISock_GetAFValue() returns a descriptor for the VMCI sockets address family if available.
The bind() call associates the datagram socket with the network settings in the
sockaddr_vm structure, instead of the
sockaddr_in structure.
The sockaddr_vm structure contains an element for the context ID (CID) to specify the virtual machine. For the client (connector) this is the local CID. For the server (listener), it could be any connecting virtual machine.
VMADDR_CID_ANY and
VMADDR_PORT_ANY are predefined so that at bind or connection time, the appropriate CID and port number are filled in from the client.
VMADDR_CID_ANY is replaced with the CID of the virtual machine and
VMADDR_PORT_ANY provides an ephemeral port from the nonreserved range (>= 1024).
The VMCI sockets bind() function is the same as for a UDP datagram application.
The getsockname() function retrieves the local address associated with a socket.
The recvfrom() call reads data from the client application. Server and client can communicate the length of data transmitted, or the server can terminate its
recvfrom() loop when the client closes its connection.
The sendto() call optionally writes data back to the client application. See
Sendto() Function.
The close() call shuts down transmission, given the original socket descriptor obtained from the
socket() call. Some server applications close immediately after receiving client data, while others wait for additional connections. To compile on Windows, you must call the Winsock
closesocket() instead of
close().