9.0

Table Of Contents
VMCI Sockets Programming Guide
26 VMware, Inc.
Microsoft Winsock
TheWinsockProgrammer’sFAQisanexcellentintroductiontoWindowssockets.Currentlyitishostedbythe
http://tangentsoft.netWebsite.
ForcompletereferenceinformationaboutWinsock,refertothepublicMSDNWebsite.
Short Introduction to Sockets
NetworkI/OissimilartofileI/O,althoughnetworkI/Orequiresnotonlyafiledescriptorsufficientfor
identifyingafile,butalsosufficientinformationfornetworkcommunication.
BerkeleysocketssupportbothUNIXdomainsockets(onthesamesystem)andInternetdomainsockets,also
calledTCP/IP(transmissioncontrolprotocol)orUDP/IP(userdata
gramprotocol).
Socket Addresses
Thesocketaddressspecifiesthecommunicationfamily.UNIXdomain socketsaredefinedassockaddr_un.
Internetdomainsocketsaredefinedassockaddr_inorsockaddr_in6forIPv6.
struct sockaddr_in {
short sin_family; /* AF_INET */
u_short sin_port; /* port number */
struct in_addr sin_addr; /* Internet address */
char sin_zero[8]; /* unused */
};
Socket() System Call
Thesocket()systemcallcreatesoneendofthesocket.
int socket(int <family>, int <type>, int <protocol>);
Thefirstparameterspecifiesthecommunicationfamily,AF_UNIXorAF_INET.
Thesecondparameterspecifiesthesockettype,SOCK_STREAMorSOCK_DGRAM.
Thethirdparameterisusuallyzerobecausecommunicationfamiliesusuallyhaveonlyoneprotocol.
Thesocket()systemcallreturnsthesocketdescriptor,asmallintegerthatissimilartothefiledescriptorused
inothersystemcalls.Forexample:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int sockfd;
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
Bind() System Call
Thebind()systemcallassociatesanaddresswiththesocketdescriptor.
int bind(int sockfd, struct sockaddr *myaddr, int addrlen);
Thefirstparameteristhesocketdescriptorfromthesocket()call,sockfd.
Thesecondparameterisapointertothesocketaddressstructure,whichisgeneralizedfordifferent
protocols.Thesockaddrstructureisdefinedin<sys/socket.h>.
Thethirdparameteristhelengthofthesockaddrstructure,becauseitcanvary.