Socket Programming
Download
Report
Transcript Socket Programming
Socket Programming
Introduction
Socket Definition
• A network socket is one endpoint in a two-way
communication flow between two programs running
over a network.
• A socket is bound to a port number so that the TCP
layer can identify the application that data is destined
to be sent to.
• Sockets are created and used with a set of
programming requests or "function calls" sometimes
called the sockets application programming interface
(API). The most common sockets API is the Berkeley
UNIX C interface for sockets.
• Sockets can also be used for communication between
processes within the same computer.
Socket Definition (Cont)
• An endpoint is a combination of an IP address and a port number. Every
TCP connection can be uniquely identified by its two endpoints. That
way you can have multiple connections between your host and the
server.
Socket Types
• A socket is created address domain specified
socket type specified
• Two processes communicate iff same sockets type and same
domain.
Adress Domains:
– Unix domain:
• Processes share a common file system.
• The address is a character string which is basically an entry in the file system.
– Internet domain:
•
•
•
•
Each process has its own address format.
The address is the host machine (IP address, port number).
Port numbers are 16 bit unsigned integers.
The lower numbers are reserved in Unix for standard services
Socket Types
Port
Service
1
TCP port service multiplexer
7
Echo server
21
FTP server
23
Telnet server
80
HTTP server
Socket Types
Socket types:
– stream sockets:
•
•
•
•
treat communications as a continuous stream of characters
use TCP (Transmission Control Protocol),
which is a reliable.
stream oriented protocol.
– datagram sockets:
•
•
•
•
read entire messages at once.
use UDP (Unix Datagram Protocol).
which is unreliable.
message oriented.
Socket Programming
• TCPSocket =
socket(PF_INET,
SOCK_STREAM, 0)
• UDPSocket =
socket(PF_INET,
SOCK_DGRAM, 0)
• send()
• recv()
• sendto()
• recvfrom()