Download introduction to Sockets Programming and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!
Introduction to
Sockets Programming in C
using TCP/IP
Professor: Panagiota Fatourou
TA: Eleftherios Kosmas
CSD - May 2012
Introduction
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas
Computer Network
hosts, routers,communication channels
Hosts run applications
Routers forward information
Packets: sequence of bytes
contain control information
e.g. destination host
Protocol is an agreement
meaning of packets
structure and size of packets
e.g. Hypertext Transfer Protocol
(HTTP)
Host
Router
Communication
channel
TCP/IP
- image is taken from “http://en.wikipedia.org/wiki/TCP/IP_model”
Communication
Channels
Network Layer
IP
Transport Layer
TCP or UDP
FTP, SMTP, …
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas
Internet Protocol (IP)
provides a datagram service
packets are handled and deliveredindependently
best-effort protocol
may loose, reorder or duplicatepackets
each packet must contain an IPaddress of its destination
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas
Local Area Network Addresses - IPv
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas
TCP vs UDP
Both use port numbers
application-specific construct serving as a communication endpoint
16-bit unsigned integer, thus ranging from 0 to 65535
to provide end-to-end transport
UDP: User Datagram Protocol
no acknowledgements
no retransmissions
out of order, duplicates possible
connectionless, i.e., app indicates destination for each packet
TCP: Transmission Control Protocol
reliable byte-stream channel (in order, all arrive, no duplicates)
similar to file I/O
flow control
connection-oriented
bidirectional
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas
Berkley Sockets
Universally known as Sockets
It is an abstraction through which anapplication may send and receive data
Provide generic access to interprocesscommunication services
e.g. IPX/SPX, Appletalk, TCP/IP
Standard API for networking
Host
Application^ Socket
TCP
IP
Host
Application
Socket
TCP
IP
Router
IP
Channel Channel CS556 - Distributed Systems Tutorial by Eleftherios Kosmas
Sockets
Uniquely identified by
an internet address
an end-to-end protocol (e.g. TCP or UDP)
a port number
Two types of (TCP/IP) sockets
Stream sockets (e.g. uses TCP)
provide reliable byte-stream service
Datagram sockets (e.g. uses UDP)
provide best-effort datagram service
messages up to 65.500 bytes
Socket extend the convectional UNIX I/O facilities
file descriptors for network communication
extended the read and write system calls
internal datastructure for file 1
Family: PF_INET Service: SOCK_STREAM Local_IP: Remote_IP: Remote_Port:
Local_Port: Descriptor Table CS556 - Distributed Systems Tutorial by Eleftherios Kosmas
Socket Programming
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas
Client-Server communication
Server passively waits for and responds to clients passive socket
Client initiates the communication must know the address and the port of the server active socket CS556 - Distributed Systems Tutorial by Eleftherios Kosmas
Client - Server Communication
- Unix
socket()
bind() listen()
accept()
recv() send() close()
Server
socket() connect()^ send()
Client^ recv()^ close()
synchronization point Stream (e.g. TCP)
socket()
bind()
recvfrom()
Server sendto()^ close()
socket() sendto()
recvfrom()
Client^ close()
Datagram(e.g. UDP)
bind()
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas
Socket creation in C:
socket()
int
sockid
socket(family,
type,
protocol);
sockid: socket descriptor, an integer (like a file-handle)
family: integer, communication domain, e.g.,
PF_INET, IPv4 protocols, Internet addresses (typically used) PF_UNIX, Local communication, File addresses
type: communication type
SOCK_STREAM - reliable, 2-way, connection-based service SOCK_DGRAM - unreliable, connectionless, messages of maximum length
protocol: specifies protocol
IPPROTO_TCP IPPROTO_UDP usually set to 0 (i.e., use default protocol)
upon failure returns -
NOTE: socket call does not specify where data will be coming from,nor where it will be going to – it just creates the interface!
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas
Socket close in C:
close()
When finished using a socket, the socket should be closed
status
close(sockid);
sockid: the file descriptor (socket being closed)
status: 0 if successful, -1 if error
Closing a socket
closes a connection (for stream socket)
frees up the port used by the socket
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas
Specifying Addresses
Socket API defines a generic data type for addresses:
Particular form of the sockaddr used for TCP/IP addresses:
Important: sockaddr_in can be casted to a sockaddr
struct sockaddr
unsigned
short
sa_family;
/* Address family (e.g. AF_INET) */
char
sa_data[14];
/* Family-specific address information */
} struct
in_addr
unsigned
long
s_addr;
/* Internet address (32 bits) */
} struct sockaddr_in
unsigned
short
sin_family;
/* Internet protocol (AF_INET) */
unsigned
short
sin_port;
/* Address port (16 bits) */
struct
in_addr
sin_addr;
/* Internet address (32 bits) */
char
sin_zero[8];
/* Not used */
CS556 - Distributed Systems Tutorial by Eleftherios Kosmas