Вы находитесь на странице: 1из 3

IPC Sockets

Soockets provide point-to-point communication between two processes.Sockets are very


versatile and are a basic component of interprocess and inter-system communication.
A socktet is an endpoint of communication to which a name can be found. It has a type and one
or more associated processes.
1. Socket Domains(<sys/socket.h>): Unix Domain, Internet Domain etc.
A socket domain is an abstraction that provides an addressing structure and a set of
protocols.
Internet domain communication uses TCP/IP protocol suite.
2. Socket types:
Stream Socket: Provides two-way, sequencial, unreliable and unduplicated flow of data
with no-record boundaries. Socket type is SOCK_STREAM, which, in the internet domain
used Transmission Control Protocol (TCP).
Datagram Socket: Supports two-way flow of messages. A datagram socket may receive
messages in different order from the sequence in which the messages were sent. Record
boundaries in the data are preserved. The socket type is SOCK_DGRAM, which, in the
internet domain uses User Datagram Protocol(UDP).
Sequencial packet socket: SOCK_SEQPACKET
Raw Socket:
3. Socket creation and Naming:
int socket(int domain, int type, int protocol) is called to create socket in the specified
domain and specified type.
If the protocol is not specified, the system defaults to a protocol that supports the
specified socket type. The socket handle (a descriptor) is returned. A remote process has
no way to identify a socket unitl an address is bound to it. Communicating processes
connect throght addresses.
In the Unix domain connection is composed of one or two paths names.
In the Internet domain connection is composed of local and remote address and local
and remort ports.

int bind(int s, const struct sockaddrr *name, int namelen) is called to bind a path or
internet address to a socket.
-Unix Domain:
- #include <sys/socket.h>
-------------int bind(int s, (struct sockaddr *)&addr, int namelen)
-

#inlcude <sys/socket.h>
-------------int bind(int s, (struct sockaddr_un *)&addr, length)

-Internet Domain
- #include <netinet/in.h>
-------------bind(sd, (struct sockaddr *)&addr, length)

4. Connecting Stream Sockets:


Connecting sockets is usually not symmetric. One process acts as a server and other
process is the client. The server binds its socket to previously agreed path or address. It then
blocks the socket.
-

For a SOCK_STREAM socket, the sever call int listen(int s, int backlog) , which specifies
how many connection requests can be queued.
A client initiates connection to the servers socket by a call to int connect(int s, struct
sockaddr *name, int namelen).

Unix Domain call:


struct sockaddr_un server;
------------------------connect (sd, (struct sockaddr_un *)&server, length);
Internet domain call:
struct sockaddr_in server;
------------------------------

connect(sd, (struct sockaddr_in *)&server, length);

If the clients socket is unbound, at the time of connect call, the system automatically selects
and binds a name to the socket. For a SOCK_STREAM socket, the server calls accept(3N) to
complete the connection.

returns a new socket


descriptor which is valid only for the particular connection. A server can have
multiple SOCK_STREAM connections active at one time.
int accept(int s, struct sockaddr *addr, int *addrlen)

5. Stream Data Transfer and Closing:


- write()
- read ()
- int send(int s, const char *msg, int len, int flags)
- int recv(int s, char *buf, int len, int flags)
- Flags:
o MSG_OOB (Out-of-band data)
o MSG_DONTROUTE
o MSG_PEEK

6. Datagram Sockets
Datagram socket does not require that a connection ne established. Each message carries
the destination address. If a particular local address is needed a call to bind() must precede
data transfer.
- Data is sent through sendto() and sendmsg().
- To receive messages recvfrom() and recvmsg().
- Note: recv() requires one buffer for receiving the message. recvfrom() requires two
buffers, one for receiving message and other to receive source address.
- Datagram sockets can also use connect() to connect to a specified address.
When this is done, send() and recv() are used to send and receive message.
7. Socket options:
- getsocketopt()
- setsocketopt()
- level = SOL_SOCKET

Вам также может понравиться