SocketProgramming 6
SocketProgramming 6
Client-server paradigm
Client:
application
initiates contact with server transport
network
data link
(“speaks first”) physical
Ports (Sockets)
Application Layer 1
Creating a socket Binding a socket
int socket(int domain, int type, int protocol) int bind (int socket, struct sockaddr *address, int addr_len)
Communication
semantics: It binds the socket to the specified address. The
SOCK_STREAM or address parameter specifies the local component
SOCK_DGRAM
of the address, e.g. IP address and UDP/TCP port
Socket Descriptors
Socket Descriptors Socket Socket Data
Descriptor Structure
Operating system maintains a set of Table proto family:
socket descriptors for each process 0: PF_INET
Address Data
– Note that socket descriptors are shared 1: service: Structure
by threads 2: SOCK_STREAM
address family:
.. local address:
Three data structures . AF_INET
Application Layer 2
TCP Client Side: Active Open Sockets: Summary
int connect (int socket, struct sockaddr *address, int *addr_len) Client:
int socket(int domain, int type, int protocol)
This call is executed by the client. *address
int connect (int socket, struct sockaddr *address, int addr_len)
contains the remote address.
new connection
int sendto (int socket, void *msg, int len, int
flags, struct sockaddr * to, Data
int tolen ); (UDP) write() read()
int write(int socket, void *msg, int len); /* TCP */
Application Layer 3
Address Data Structures
Some Other “Utility” Functions
struct sockaddr {
u_short sa_family; // type of address
inet_addr() -- convert “dotted” char sa_data[14]; // value of address
}
character string form of IP address to
internal binary form sockaddr is a generic address structure
Application Layer 4