05-11-2012, 03:28 PM
Socket Programming in C/C++
Socket Programming.pdf (Size: 351.49 KB / Downloads: 56)
Introduction
Sockets are a protocol independent method of creating a
connection between processes. Sockets can be either
connection based or connectionless: Is a connection
established before communication or does each packet
describe the destination?
packet based or streams based: Are there message boundaries
or is it one stream?
reliable or unreliable. Can messages be lost, duplicated,
reordered, or corrupted?
Socket characteristics
Socket are characterized by their domain, type and transport
protocol. Common domains are:
AF UNIX: address format is UNIX pathname
AF INET: address format is host and port number
Common types are:
virtual circuit: received in order transmitted and reliably
datagram: arbitrary order, unreliable
Each socket type has one or more protocols. Ex:
TCP/IP (virtual circuits)
UDP (datagram)
Use of sockets:
Connection–based sockets communicate client-server: the
server waits for a connection from the client
Connectionless sockets are peer-to-peer: each process is
symmetric.
Socket APIs
socket: creates a socket of a given domain, type, protocol
(buy a phone)
bind: assigns a name to the socket (get a telephone number)
listen: specifies the number of pending connections that
can be queued for a server socket. (call waiting allowance)
accept: server accepts a connection request from a client
(answer phone)
connect: client requests a connection request to a server
(call)
send, sendto: write to connection (speak)
recv, recvfrom: read from connection (listen)
shutdown: end the call
Pro and Con of Accept styles
Iterating server is basically a low performance technique since
only one connection is open at a time.
Forking servers enable using multiple processors. But they
make sharing state difficult, unless performed with threads.
Threads, however present a very fragile programming
environment.
Concurrent single server: reduces context switches relative to
forking processes and complexity relative to threads. But does
not benefit from multiprocessors.