SOCKET PROGRAMMING
SOCKET
What is Socket?
Socket is an interface between an application
process and transport layer(i.e. Network)
In Unix , a socket is a file descriptor – an integer
associated with an open file.
The Problem
Two Main Issues
Network Addressing
Identifying the Destination
Standard Ports
Connections
Client/Server Concepts
Data Transport
There are two basic types of communication
Streams (TCP): Computers establish a
connection with each other and read/write data
in a continuous stream of bytes---like a file. This is the
most common.
Datagrams (UDP): Computers send discrete
packets (or messages) to each other. Each
packet contains a collection of bytes, but each
packet is separate and self-contained.
TCP CHARACTERISTICS
Connection-oriented
• Two endpoints of a virtual circuit
Reliable
• Application needs no error checking
Stream-based
• No predefined blocksize
Processes identified by port numbers
Services live at specific ports
UDP CHARACTERISTICS
Datagram-based
• Connectionless, unreliable, can broadcast
Applications usually message-based
• No transport-layer retries
• Applications handle (or ignore) errors
Processes identified by port number
Services live at specific ports
• Usually below 1024, requiring privilege
SOCKETS
Both protocols (UDP, TCP) are supported using
"sockets"
• A socket is a file-like object.
• Allows data to be sent and received across the
network like a file.
• But it also includes functions to accept and
establish connections.
• Before two machines can establish a connection,
both must create a socket object.
Why Socket?
How can I program a network application?
◦ Share data
◦ Send messages
IPC - Interprocess Communication
Sockets
THE TCP/IP LAYERING MODEL
THE TCP/IP LAYERING MODEL
Socket Description
Layering Makes it Easier
Application programmer
◦ Doesn’t need to send IP packets
◦ Doesn’t need to send Ethernet frames
◦ Doesn’t need to know how TCP
implements reliability
Only need a way to pass the data down
◦ Socket is the API to access transport layer
functions.
NETWORK BASICS: PORTS
SOCKET PROGRAMMING IN A
NUTSHELL
Server creates a socket, binds it to some
well-known port number, and starts
listening.
Client creates a socket and tries to connect
it to the server (through the above port).
Server-client exchange some data.
Close the connection (of course the server
continues to listen for more clients).
MAJOR SYSTEM CALLS
MAJOR SYSTEM CALLS (SUMMARY)
CONNECTIONLESS SERVICES
CONNECTION-ORIENTED SERVICES
Socket Basics
SOCKET FAMILY AND TYPE
Using a Socket
Server Implementation
TCP Server
TCP Server
TCP Server
TCP Server
TCP Server
TCP Server
TCP Server
TCP Server
Partial Reads/Writes
Partial Reads/Writes
UDP : Datagrams
UDP Server
UDP Client
THANK YOU!