Sockets are just like "worm holes" in science fiction. When things go into one end, they (should) come out of the other.
Sockets are either connection-oriented or connectionless . Connection-oriented sockets allow for data to flow back and forth as needed, while connectionless sockets (also known as datagram sockets) allow only one message at a time to be transmitted, without an open connection.
There are also different socket families.
The SOCKET API
The interface between an application program and the communication protocols in an OS os known as Application program Interface or API.Socket is an API used for network communication.This API is developed by BSD UNIX for VAX architecture .System V UNIX has different interface called the Transport Layer Interface(TLI)for solaris 2.x .Socket applications treat network connection or to be exact sockets are endpoint for communications.When an application calls one of the socket procedured ,control passes to a library roooutine that makes one or more calls to the underlying OS to implement socket function.
Overview
The BSD socket interface supports the following communicccation protocols
Unix domain sockets use the local workstation filesystem to provide communication between programs running on the same workstation only.Internet domain sockets use the Internet protovol (IP) suite to communicate over the network.
System Calls
int socket(int domain,int type,int protocol);
Socket creates an endpoint for communication and returns a descriptor. The domain parameter specifies a communications domain within which communication will take place; this selects the protocol family which should be used. These families are defined in the include file sys/socket.h .The currently understood formats are
The socket has the indicated type, which specifies the semantics of communication. Currently defined types are:
A SOCK_STREAM type provides sequenced, reliable, two-way connection based byte streams. An out-of-band data trans- mission mechanism may be supported. A SOCK_DGRAM socket supports datagrams (connectionless, unreliable messages of a fixed (typically small) maximum length). A SOCK_SEQ- PACKET socket may provide a sequenced, reliable, two-way connection-based data transmission path for datagrams of fixed maximum length; a consumer may be required to read an entire packet with each read system call. This facility is protocol specific, and presently implemented only for AF_NS. SOCK_RAW sockets provide access to internal network protocols and interfaces. The types SOCK_RAW, which is available only to the super-user, and SOCK_RDM, which is planned, but not yet implemented, are not described here.
The protocol specifies a particular protocol to be used with the socket. Normally only a single protocol exists to support a particular socket type within a given protocol family. However, it is possible that many protocols may exist, in which case a particular protocol must be specified in this manner. The protocol number to use is particular to the "communication domain" in which communication is to take place.
Sockets of type SOCK_STREAM are full-duplex byte streams, similar to pipes. A stream socket must be in a connected state before any data may be sent or received on it. A connection to another socket is created with a connect call. Once connected, data may be transferred using read and write calls or some variant of the send and receive calls. When a session has been completed a close may be performed. Out-of-band data may also be transmitted as described in send and received as described in receive. The communications protocols used to implement a SOCK_STREAM insure that data is not lost or duplicated. If a piece of data for which the peer protocol has buffer space cannot be successfully transmitted within a reasonable length of time, then the connection is considered broken and calls will indicate an error with -1 returns and with ETIMEDOUT as the specific code in the global variable errno. The protocols optionally keep sockets wam by forcing transmissions roughly every minute in the absence of other activity. An error is then indicated if no response can be elicited on an otherwise idle connection for a extended period .
TopSocketpair - create a pair of connected sockets
int socketpair(int family,int type,int protcol,int sockvec[2]);
The call creates an unnamed pair of connected sockets in the specified domain , of the specified type, and using the optionally specified protocol. The descriptors used in referencing the new sockets are returned in sockvec[0] and sockvec[1] which are bidirectional.The two sockets are indistinguishable. On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
Topbind -assigns a name to unnamed socket.
int bind(int sockfd,struct sockaddr *myaddr,int addrlen);
Second argument is a pointer to a protocol specific address and third argument is the size of thsi address structure.Uses of bind are
Connect - initiate a connection on a socket
int connect(int sockfd,int struct sockaddr *servaddr,int addrlen);
The parameter sockfd is a socket. If it is of type SOCK_DGRAM, this call specifies the peer with which the socket is to be associated; this address is that to which datagrams are to be sent, and the only address from which datagrams are to be received. If the socket is of type SOCK_STREAM,this call attempts to make a connection to another socket. The other socket is specified by servaddr, which is an address in the communications space of the socket. Each communications space interprets the servaddr, parameter in its own way. Generally, stream sockets may successfully connect only once; datagram sockets may use connect multiple times to change their association. Datagram sockets may dissolve the association by connecting to an invalid address, such as a null address. If the connection or binding succeeds, zero is returned. On error, -1 is returned, and errno is set appropriately. The socket is non-blocking and the connection cannot be completed immediately. It is possible to select for completion by selecting the socket for writing.
Toplisten - listen for connections on a socket
int listen(int sockfd,int backlog);
To accept connections, a socket is first created with socket a willingness to accept incoming connections and a queue limit for incoming connections are specified with listen, and then the connections are accepted with accept. The listenn call applies only to sockets of type SOCK_STREAM or Sock_SEQPACKET. The backlog parameter defines the maximum length the queue of pending connections may grow to. If a connection request arrives with the queue full the client may receive an error with an indication of ECONNREFUSED, or, if the underlying protocol supports retransmission, the request may be ignored so that retries may succeed. On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
TopAccept - accept a connection on a socket
int accept(int sockfd,struct sockaddr *peer,int *addrlen);
The argument sockfd is a socket that has been created with socket, bound to an address with bind, and is lis- tening for connections after a listen. The accept function extracts the first connection request on the queue of pending connections, creates a new socket with the same properties of and allocates a new file descrip- tor for the socket. If no pending connections are present on the queue, and the socket is not marked as non-block- ing, accept blocks the caller until a connection is pre- sent. If the socket is marked non-blocking and no pending connections are present on the queue, accept returns an error as described below. The accepted socket may not be used to accept more connections. The original sockets remains open. The argument sockaddr is a result parameter that is filled in with the address of the connecting entity, as known to the communications layer. The exact format of the addr param- eter is determined by the domain in which the communication is occurring. The addrlen is a value-result parame- ter; it should initially contain the amount of space pointed to by _addr; on return it will contain the actual length (in bytes) of the address returned. This call is used with connection-based socket types, currently with SOCK_STREAM.
TopSend ,sendto,recv,recvfrom
These system calls are similar to read and writew system calls,but additional arguments are required.Flag is zero.All system calls return the length of the data that was written or read.
Topclose - close a file descriptor
int close(int fd) ;
close closes a file descriptor, so that it no longer refers to any file and may be reused. Any locks held on the file it was associated with, and owned by the process, are removed (regardless of the file descriptor that was used to obtain the lock). If fd is the last copy of a particular file descriptor the resources associated with it are freed; if the descriptor was the last reference to a file which has been removed using unlink the file is deleted.
Shutdown - bring the system down in a secure way.
int shutdown(int sockfd,int howto);
All logged in users are notified that the system is going down, and login is blocked. It is possible to shut the system down immideately, or after a delay. All processes are first notified that the system is going down by the signal SIGTERM. This gives programs like vi the time to save the file being editted, mail and news processing programs a chance to exit cleanly, etc. SHUTDOWN does it's job by signalling the init process, asking it to change the runlevel.
Topgetpeername - get name of connected peer
int getpeername(int sockfd,struct sockaddr *peer,int *addrlen );
getpeername returns the name of the peer connected to sockets . The addrlen parameter should be initialized to indicate the amount of space pointed to by *peer. On return it contains the actual size of the name returned (in bytes). The name is truncated if the buffer provided is too small. On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
getsockname - get socket name
int getsockname(int sockfd,struct sockaddr *peer,int addrlen);
getsockname returns the current name for the specified socket. The namelen parameter should be initialized to indicate the amount of space pointed to by name. On return it contains the actual size of the name returned (in bytes). On success, zero is returned. On error, -1 is returned, and errno is set appropriately. A 0 is returned if the call succeeds, -1 if it fails.
select waits for a number of file descriptors to change status.
int select(int maxfdpl,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,struct timeval *timeout);
Three independent sets of descriptors are watched. Those listed in readfds will be watched to see if characters become available for reading, those in writefds will be watched to see if it is ok to immediately write on them, and those in exceptfds will be watched for exceptions. On exit, the sets are modified in place to indicate which descriptors actually changed status. On success, select returns the number of descriptors con- tained in the descriptor sets, which may be zero if the timeout expires before anything interesting happens. On error, -1 is returned, and errno is set appropriately.
Top