socket Function

 < Day Day Up > 



socket Function

The purpose of the socket function is to create a new socket endpoint for communication. Calling the socket function to retrieve a new socket is the first step in building a Sockets application. Only one other function returns a new socket, the accept function, which is discussed in the accept Function section later in this chapter.

The socket function has the following prototype:

#include <sys/types.h> #include <sys/socket.h> int socket( int domain, int type, int protocol );

The domain argument specifies the protocol family that is desired for the new socket. In this book, we focus solely on the IPv4 protocol, so we see the use of the AF_INET constant (Address Family Internet) in all examples. Other possible arguments include AF_INET6 for IPv6 and AF_ROUTE for the creation of a routing socket (used to add or delete rows in the routing table).

The type argument specifies the semantics of communication within the context of the previously defined domain. Some of the common types are shown in Table 3.2, though SOCK_SEQPACKET and SOCK_RDM are available only in specialized stacks.

Table 3.2: SOCKET TYPES USED AS THE TYPE ARGUMENT FOR SOCKET

Socket Type

Description

SOCK_STREAM

Stream socket (connection-based, reliable)

SOCK_DGRAM

Datagram socket (connectionless, unreliable)

SOCK_SEQPACKET

Sequenced stream socket (connection-based, reliable)

SOCK_RAW

Raw socket (raw socket access)

SOCK_RDM

Reliable datagram socket (unordered, reliable)

Finally, the protocol argument identifies a particular protocol to use within the domain and type previously specified. Many times, this argument is specified as zero, because only one protocol exists for the given domain and type. When using specialized protocols (such as SCTP), we’ll see this argument used. Examples of the socket function for a number of different protocols are providing in Listing 3.1.

Listing 3.1 Example usage of the socket function.

start example
tcpSocket = socket( AF_INET, SOCK_STREAM, 0 ); tcpSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); udpSocket = socket( AF_INET, SOCK_DGRAM, 0 ); udpSocket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP ); ipSocket = socket( AF_INET, SOCK_RAW, 0 ); ipSocket = socket( AF_INET, SOCK_RAW, IPPROTO_IP ); sctpSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_SCTP ); sctpSocket = socket( AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP ); sctpSocket = socket( AF_INET, SOCK_DGRAM, IPPROTO_SCTP ); rdmSocket = socket( AF_INET, SOCK_RDM, 0 );
end example

Therefore, the socket function not only creates a new socket endpoint, but it also defines the semantics of communication that will be used through the socket. The return value of the socket function is a simple integer, though this may be different depending upon your particular Sockets implementation and language. If an error occurs within the socket function, a negative value is returned indicating this.

From Listing 3.1, we can see the creation of a number of different types of sockets. A TCP stream socket can be created either as the default of the SOCK_STREAM type, or by explicitly defining the protocol as IPPROTO_TCP. Creating a UDP datagram socket and raw IP socket is performed similarly. The SCTP socket is created depending on the style of socket requested. SCTP can operate in a datagram-style model or a stream-style model [Stewart01]. Finally, a reliable datagram socket is illustrated last with SOCK_RDM.

Tip 

One interesting difference between stream and datagram sockets is what’s known as framing. When data is sent through a datagram socket, the receiver always receives the data in the units that were provided by the sender. For example, if the sender sent 100 bytes and then sent another 50 bytes, the receiver would receive two datagrams of 100 bytes and 50 bytes (through two separate read calls). Stream sockets operate differently, in that the boundaries defined by the sender through the write calls are not preserved. In the stream socket, using the previous example, the receiver might very well end up reading 150 bytes. This concept is important, and makes datagram sockets useful for message-based communication.



 < Day Day Up > 



BSD Sockets Programming from a Multi-Language Perspective
Network Programming for Microsoft Windows , Second Edition (Microsoft Programming Series)
ISBN: 1584502681
EAN: 2147483647
Year: 2003
Pages: 225
Authors: Jim Ohlund

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net