Professional Documents
Culture Documents
Chapter 2 - Standard Unix API
Chapter 2 - Standard Unix API
Chapter 2 - Standard Unix API
By
En. Mohd Nizam bin Osman
(Senior Lecturer)
Department of Computer Science
Faculty of Computer and Mathematical Science, UiTM Perlis
Course Outline
• Socket Functions
bind
listen
accept
connect
recv/recvfrom
send/sendto
close
htons/ntohs/htonl/ntohl
ined_addr/ined_aton/inet_ntoa
Recap
(Client-Server Model)
1. Client sends request
Client/sever model
Client Server
process process Resource
2. Server Processes
4. Client request
3. Server sends response
processes
response
Client-Server transaction
Note: clients and servers are processes running on hosts (can be the same
or different hosts).
Client Server
• Running on end host • Running on end host
• Request service • Provides service
• e.g.: Web browser • e.g.: Web server
Recap
(Client-Server Model)
TCP/IP allows two application programs to pass data
Client/sever model
back and forth:
The applications can execute on the same machine
or on different machines.
How to organize the application programs?
Client-server paradigm is the most popular model
The communication applications are divided into
two categories: client and server
Client-server paradigm uses the direction of initiation
to categorize whether a program is a client or server.
Recap
(Client-Server Model)
An application that initiates the communication is called a client.
Client/sever model
E.g.: Web browser, FTP client, Telnet client, Email client
The client contacts a server, sends a request, and awaits a
response
An application that waits for incoming communication requests
from clients is called a server.
E.g.: Web server, FTP server, Telnet server, SMTP server
The server receives a client’s request, performs the necessary
computation, and returns the result to the client
Remark: A server is usually designed to provide service to
multiple clients.
How ???
Client-Server Communication
Client/sever model
Client Server
• Sometimes on • Always on
• Initiates a request to • Server services to
the server when many clients
interested • e.g.:
• e.g.: Web browser www.uitm.edu.my
• Needs to know the • Not initiate contact
server’s address with the clients
• Need a fixed address
Socket
Means for inter-process communication (IPC)
Server
Client
Connection socket pair (port 80)
(128.2.194.242:3479, 208.216.181.15:80)
Bytes stream
u o y e r e w o H 11 ! i H 3
Sockets Parameters
A socket connection has five (5) general parameters:
The Local
The Local
The and Remote
and Remote
protocol Port
Address
Number
Some ports
are reserved
(e.g.: 80 for
HTTP)
Example: TCP Example:
and UDP 128.100.3.40
Root access
require to
listen on port
numbers
below 1024
Sockets Functions
Function Description
socket() Creates a new socket endpoint. This needs to know the protocol
family and the protocol type.
connect() A client uses this to build a connection with a server.
write() Send data through a socket.
read() Receive data through a socket.
close() Closes a socket endpoint
bind() Allows server to bind a local address to the socket
listen() Permits incoming connections for a server socket
accept() Accepts a new client connection for a server socket.
Prepare to communicate
• Create a socket
• Determine server address and port number
• Initiate the connection to the server
CREATE
BIND
SEND
RECEIVE
SEND
CLOSE
History of Socket
InHistory
earlyof 1980s,
Sockets the Advanced Research Projects Agency
(ARPA) funded a group at UC Berkeley to transport TCP/IP
software to the UNIX operating system.
As part of the project, the designers created an interface
that applications use for network communication.
The interface used an abstraction known as a socket, and
the API became known as socket API.
Many computer vendors adopted the Berkeley UNIX
operating system, and the socket interface became very
popular.
The socket interface has become a de facto standard
throughout the computer industry.
23
How to Write Network Application?
TCP/IP is implemented inside the OS
OS exports the TCP/IP functionalities through SOCKET
APIs
API: Application Program Interface
A set of functions
Programmers develop network applications by using
SOCKET APIs
To learn SOCKET programming
Learn some data structures
Learn the socket APIs
Learn multi-threading 24
Create a Socket
Necessary “includes”
#include <netinet/in.h> • Prototype & define (htons, etc)
struct in_addr {
uint32_t s_addr;
};
Little
Endian
Big
Endian
Byte Order
Example: Storing 32-bit 0X0A0B0C0D
A s s u m in g 8 - b it ( o n e b y t e ) a t o m ic e le m e n t s …
… b i g e n d ia n :
- the most significant byte (MSB), 0x0A, is stored on the
lowest memory address
- the least significant byte (LSB), 0x0D, is stored on the
highest memory address
increasing memory addresses
… 0x0A 0x0B 0x0C 0x0D …
… l i t t l e e n d i a n :
- 0x0A is stored on the highest memory address
- 0x0D is stored on the lowest memory address
increasing memory addresses
… 0x0D 0x0C 0x0B 0x0A …
Byte Order
Example: Storing 32-bit 0X0A0B0C0D
I P v 4 h o s t a d d r e s s : r e p r e s e n t s a 3 2 - b it a d d r e s s
- written on paper (”dotted decimal notation”): 129.240.71.213
- b in a r y in b it s : 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 0 0 0 1 1 1 1 0 0 0 1 0 1 1
B i g - e n d i a n ( ” n o r m a l ” l e f t t o r i g h t ) :
- one 4 byte int on PowerPC, POWER, Sparc, …: 0x81f0478b
L i t t l e - e n d i a n :
- one 4 byte int on x86, StrongARM, XScale, …: 0x8b47f081
M i d d l e / m i x e d / P D P e n d ia n :
- one 4 byte int on PDP-11: 0xf0818b47
N e t w o r k b y t e o r d e r : 0 x 8 1 f0 4 7 8 b
Byte Order
Translation
Byte order translation makes communication over several platforms possible:
htons() / htonl()
host-to-network short / long
translate a 16 / 32-bit integer value to network format
ntohs() / ntohl()
network-to-host short/long
translate a 16 / 32-bit integer value to host format
Example:
ntohl(0x81f0478b) == 0x8b47f081
Little-endian (x86 etc.):
sd = accept(request_sd,
struct sockaddr*)&clientaddr,
&clientaddrlen);
Details about the previous slides
bind()
int bind(int sfd, struct sockaddr *a, int addrlen);
int recvfrom( int sockfd, const char *buf, int len, int flag,
struct sockaddr *addr, socklen_t *len);
bind() Initialization
Client
listen() socket()