Information Technology Reference
In-Depth Information
SOCKET socket (int af, int type, int protocol)
where
af
A value of PF_INET specifies the ARPA Internet address format specifica-
tion (others include AF_IPX for SPX/IPX and AF_APPLETALK for Apple-
Talk).
type
Socket specification, which is typically either SOCK_STREAM or SOCK_DGRAM .
The SOCK_STREAM uses TCP and provides a sequenced, reliable, two-way,
connection-based stream. SOCK_DGRAM uses UDP and provides for connec-
tionless datagrams. This type of connection is not recommended. A third
type is SOCK_RAW , for types other than UDP or TCP, such as for ICMP.
protocol
Defines the protocol to be used with the socket. If it is zero then the caller
does not wish to specify a protocol.
If the socket function succeeds then the return value is a descriptor referencing the new
socket. Otherwise, it returns SOCKET_ERROR , and the specific error code can be tested with
WSAGetLastError .
An example creation of a socket is given next:
SOCKET s;
s=socket(PF_INET,SOCK_STREAM,0);
if (s == INVALID_SOCKET)
{
cout << Socket error
}
bind()
The bind() function associates a local address with a socket. It is called before the connect
or listen function. When a socket is created with socket , it exists in a name space (address
family), but it has no name assigned. The bind function gives the socket a local association
(host address/port number). Its syntax is:
int bind(SOCKET s, const struct sockaddr FAR * addr, int namelen)
where
s
A descriptor identifying an unbound socket.
The length of addr .
namelen
addr
The address to assign to the socket. The sockaddr structure is
defined as follows:
struct sockaddr
{
u_short sa_family;
char sa_data[14];
};
In the Internet address family, the sockadd_in structure is used by Windows Sockets to spec-
ify a local or remote endpoint address to which to connect a socket. This is the form of the
sockaddr structure specific to the Internet address family and can be cast to sockaddr . This
Search WWH ::




Custom Search