SlideShare ist ein Scribd-Unternehmen logo
1 von 7
CliClieenntt--sseerrvveerr ppaarraaddiigmgm
SSoocckketet PrProoggrarammmiminngg
Srinidhi Varadarajan
Client:
initiates contact with server
(“speaks first”)
typically requests service
from server,
for Web, client is
implemented in browser; for
e-mail, in mail reader
Server:
provides requested service
to client
e.g., Web server sends
requested Web page, mail
server delivers e-mail
application
transport
network
data link
physical
request
reply
application
transport
network
data link
physical
ApplicationApplication LLaayyerer PrProoggrarammmmiinngg
API: application programming interface
defines interface between application and
transport layer
sockets: Internet API
– two processes communicate by sending
data into socket, reading data out of socket
SSoocckketet IInnterface.terface. WWhhatat isis it?it?
Gives a file system like abstraction to the
capabilities of the network.
Each transport protocol offers a set of
services. The socket API provides the
abstraction to access these services
The API defines function calls to create,
close, read and write to/from a socket.
SSoocckketet AAbbstractistractioonn The socket is the basic abstraction for network
communication in the socket API
– Defines an endpoint of communication for a process
Application Layer
1
– Operating system maintains information about the
socket and its connection
– Application references the socket for sends, receives,
etc.
WhatWhat dodo youyou nneeeded forfor sosocckketet ccoommmmuunicationnication ??
Basically 4 parameters
– Source Identifier (IP address)
– Source Port
– Destination Identifier
– Destination Port
Process
A
Network Process
B
In the socket API, this information is
communicated by binding the socket.
Ports (Sockets)
Application Layer
2
CreatiCreatinngg aa ssoocckketet
int socket(int domain, int type, int protocol)
BiBindndiinngg aa sosocckketet
int bind (int socket, struct sockaddr *address, int addr_len)
Protocol Family:
PF_INET or
PF_UNIX
Usually
UNSPEC
This call is executed by:
– Server in TCP and UDP
Communication
semantics:
SOCK_STREAM or
SOCK_DGRAM
The call returns a integer identifier called a
handle
It binds the socket to the specified address. The
address parameter specifies the local component
of the address, e.g. IP address and UDP/TCP
port
SSoocckketet DescriDescrippttororssSSoocckketet DescriDescrippttororss
Operating system maintains a set of
socket descriptors for each process
– Note that socket descriptors are shared
by threads
Three data structures
– Socket descriptor table
– Socket data structure
– Address data structure
Socket
Descriptor
Table
0:
1:
2:
.
.
Socket Data
Structure
proto
family:
PF_INETservice:
SOCK_STRE
AMlocal
address:
remote
address:
.
.
Address Data
Structure
address family:
AF_INET
host IP:
128.173.88.85
port:
80
TCPTCP ServerServer Side:Side: LiLissttenen
int listen (int socket, int backlog)
This server side call specifies the number
of pending connections on the given
socket.
When the server is processing a
connection, “backlog” number of
connections may be pending in a queue.
TCPTCP ServerServer Side:Side: PasPasssiveive OpenOpen
int accept (int socket, struct sockaddr *address, int *addr_len)
This call is executed by the server.
The call does not return until a remote
client has established a connection.
When it completes, it returns a new socket
handle corresponding to the just-
established connection
TCTCPP CClielienntt SiSidde:e: AAccttivivee OpOpenen
int connect (int socket, struct sockaddr *address, int *addr_len)
This call is executed by the client.
*address contains the remote address.
The call attempts to connect the socket to a
server. It does not return until a connection
has been established.
When the call completes, the socket “socket”
is connected and ready for communication.
SSoocckkets:ets: SSuummmmaryary
Client:
int socket(int domain, int type, int protocol)
int connect (int socket, struct sockaddr *address, int addr_len)
Server:
int socket(int domain, int type, int protocol)
int bind (int socket, struct sockaddr *address, int addr_len)
int listen (int socket, int backlog)
int accept (int socket, struct sockaddr *address, int *addr_len)
MessageMessage PassingPassing
SSuummmmararyy ooff BasiBasicc SoSocckkeett CCaallslls
int send (int socket, char *message, int msg_len, int
flags) (TCP)
int sendto (int socket, void *msg, int len, int
CLIENT
connect()
Connect
(3-way handshake)
SERVER
new connection
accept()
int tolen ); (UDP)
int write(int socket, void *msg, int len); /* TCP */
write() read()
int recv (int socket, char *buffer, int buf_len, int
flags) (TCP)
int recvfrom(int socket, void *msg, int len, int
read() Data write()
flags, struct sockaddr *from, int
*fromlen); (UDP)
int read(int socket, void *msg, int len); (TCP)
close()
close()
NNeettwoworrkk BByyttee OrdOrderer
Network byte order is most-significant
byte first
Byte ordering at a host may differ
Utility functions
– htons(): Host-to-network byte order for a
short word (2 bytes)
– htonl(): Host-to-network byte order for a
long word (4 bytes)
– ntohs(): Network-to-host byte order for a
short word
– ntohl(): Network-to-host byte order for a
long word
SSoommee OOtthherer ““UUtitillitityy”” FFununcctitiononss
gethostname() -- get name of local host
getpeername() -- get address of remote
host
getsockname() -- get local address of
socket
getXbyY() -- get protocol, host, or service
number using known number, address, or
port, respectively
getsockopt() -- get current socket options
setsockopt() -- set socket options
ioctl() -- retrieve or set socket information
SSoommee OOtthherer ““UUtitillitityy”” FFununcctitiononss
inet_addr() -- convert “dotted”
character string form of IP address to
internal binary form
inet_ntoa() -- convert internal binary
form of IP address to “dotted”
character string form
AAdddressdress DataData StrStruuctctuurreses
struct sockaddr {
u_short sa_family; // type of address
char sa_data[14]; // value of address
}
sockaddr is a generic address structure
struct sockaddr_in {
u_short sa_family; // type of address (AF_INET)
u_short sa_port; // protocol port number
struct in_addr sin_addr; // IP address
char sin_zero[8]; // unused (set to zero)
}
sockaddr_in is specific instance for the Internet address
family

Weitere Àhnliche Inhalte

Was ist angesagt?

Socket programming
Socket programmingSocket programming
Socket programmingAnurag Tomar
 
Elementary TCP Sockets
Elementary TCP SocketsElementary TCP Sockets
Elementary TCP SocketsSaksham Khurana
 
Socket Programming
Socket ProgrammingSocket Programming
Socket ProgrammingVisualBee.com
 
Socket programming using C
Socket programming using CSocket programming using C
Socket programming using CAjit Nayak
 
Socket Programming Tutorial
Socket Programming TutorialSocket Programming Tutorial
Socket Programming TutorialJignesh Patel
 
Socket programming
Socket programmingSocket programming
Socket programmingUjjwal Kumar
 
IPC SOCKET
IPC SOCKETIPC SOCKET
IPC SOCKETSanoj Kumar
 
Programming TCP/IP with Sockets
Programming TCP/IP with SocketsProgramming TCP/IP with Sockets
Programming TCP/IP with Socketselliando dias
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket ProgrammingVipin Yadav
 
Socket programming in c
Socket programming in cSocket programming in c
Socket programming in cMd. Golam Hossain
 
Networking & Socket Programming In Java
Networking & Socket Programming In JavaNetworking & Socket Programming In Java
Networking & Socket Programming In JavaAnkur Agrawal
 
Socket programming in C
Socket programming in CSocket programming in C
Socket programming in CDeepak Swain
 
A Short Java Socket Tutorial
A Short Java Socket TutorialA Short Java Socket Tutorial
A Short Java Socket TutorialGuo Albert
 
Socket programming using java
Socket programming using javaSocket programming using java
Socket programming using javaUC San Diego
 

Was ist angesagt? (20)

Java sockets
Java socketsJava sockets
Java sockets
 
Socket programming
Socket programming Socket programming
Socket programming
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Elementary TCP Sockets
Elementary TCP SocketsElementary TCP Sockets
Elementary TCP Sockets
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Socket programming using C
Socket programming using CSocket programming using C
Socket programming using C
 
Socket Programming Tutorial
Socket Programming TutorialSocket Programming Tutorial
Socket Programming Tutorial
 
Lecture10
Lecture10Lecture10
Lecture10
 
Socket programming
Socket programmingSocket programming
Socket programming
 
IPC SOCKET
IPC SOCKETIPC SOCKET
IPC SOCKET
 
Programming TCP/IP with Sockets
Programming TCP/IP with SocketsProgramming TCP/IP with Sockets
Programming TCP/IP with Sockets
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket Programming
 
Socket programming in c
Socket programming in cSocket programming in c
Socket programming in c
 
Networking & Socket Programming In Java
Networking & Socket Programming In JavaNetworking & Socket Programming In Java
Networking & Socket Programming In Java
 
Socket System Calls
Socket System CallsSocket System Calls
Socket System Calls
 
Sockets
Sockets Sockets
Sockets
 
Socket programming in C
Socket programming in CSocket programming in C
Socket programming in C
 
Socket programming
Socket programmingSocket programming
Socket programming
 
A Short Java Socket Tutorial
A Short Java Socket TutorialA Short Java Socket Tutorial
A Short Java Socket Tutorial
 
Socket programming using java
Socket programming using javaSocket programming using java
Socket programming using java
 

Ähnlich wie Client-server programming with socket API

Ähnlich wie Client-server programming with socket API (20)

Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
Basics of sockets
Basics of socketsBasics of sockets
Basics of sockets
 
Raspberry pi Part 23
Raspberry pi Part 23Raspberry pi Part 23
Raspberry pi Part 23
 
Os 2
Os 2Os 2
Os 2
 
Np unit2
Np unit2Np unit2
Np unit2
 
Mail Server Project Report
Mail Server Project ReportMail Server Project Report
Mail Server Project Report
 
Byte Ordering - Unit 2.pptx
Byte Ordering - Unit 2.pptxByte Ordering - Unit 2.pptx
Byte Ordering - Unit 2.pptx
 
Networking in Java
Networking in JavaNetworking in Java
Networking in Java
 
Socket Programming TCP:IP PPT.pdf
Socket Programming TCP:IP PPT.pdfSocket Programming TCP:IP PPT.pdf
Socket Programming TCP:IP PPT.pdf
 
L5-Sockets.pptx
L5-Sockets.pptxL5-Sockets.pptx
L5-Sockets.pptx
 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
 
Python networking
Python networkingPython networking
Python networking
 
CN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjj
CN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjjCN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjj
CN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjj
 
Network Prog.ppt
Network Prog.pptNetwork Prog.ppt
Network Prog.ppt
 
Lecture25
Lecture25Lecture25
Lecture25
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
introduction to security
introduction to securityintroduction to security
introduction to security
 
security
securitysecurity
security
 
Networking
NetworkingNetworking
Networking
 

KĂŒrzlich hochgeladen

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 

KĂŒrzlich hochgeladen (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

Client-server programming with socket API

  • 1. CliClieenntt--sseerrvveerr ppaarraaddiigmgm SSoocckketet PrProoggrarammmiminngg Srinidhi Varadarajan Client: initiates contact with server (“speaks first”) typically requests service from server, for Web, client is implemented in browser; for e-mail, in mail reader Server: provides requested service to client e.g., Web server sends requested Web page, mail server delivers e-mail application transport network data link physical request reply application transport network data link physical ApplicationApplication LLaayyerer PrProoggrarammmmiinngg API: application programming interface defines interface between application and transport layer sockets: Internet API – two processes communicate by sending data into socket, reading data out of socket SSoocckketet IInnterface.terface. WWhhatat isis it?it? Gives a file system like abstraction to the capabilities of the network. Each transport protocol offers a set of services. The socket API provides the abstraction to access these services The API defines function calls to create, close, read and write to/from a socket. SSoocckketet AAbbstractistractioonn The socket is the basic abstraction for network communication in the socket API – Defines an endpoint of communication for a process Application Layer 1
  • 2. – Operating system maintains information about the socket and its connection – Application references the socket for sends, receives, etc. WhatWhat dodo youyou nneeeded forfor sosocckketet ccoommmmuunicationnication ?? Basically 4 parameters – Source Identifier (IP address) – Source Port – Destination Identifier – Destination Port Process A Network Process B In the socket API, this information is communicated by binding the socket. Ports (Sockets) Application Layer 2
  • 3. CreatiCreatinngg aa ssoocckketet int socket(int domain, int type, int protocol) BiBindndiinngg aa sosocckketet int bind (int socket, struct sockaddr *address, int addr_len) Protocol Family: PF_INET or PF_UNIX Usually UNSPEC This call is executed by: – Server in TCP and UDP Communication semantics: SOCK_STREAM or SOCK_DGRAM The call returns a integer identifier called a handle It binds the socket to the specified address. The address parameter specifies the local component of the address, e.g. IP address and UDP/TCP port SSoocckketet DescriDescrippttororssSSoocckketet DescriDescrippttororss Operating system maintains a set of socket descriptors for each process – Note that socket descriptors are shared by threads Three data structures – Socket descriptor table – Socket data structure – Address data structure Socket Descriptor Table 0: 1: 2: . . Socket Data Structure proto family: PF_INETservice: SOCK_STRE AMlocal address: remote address: . . Address Data Structure address family: AF_INET host IP: 128.173.88.85 port: 80 TCPTCP ServerServer Side:Side: LiLissttenen int listen (int socket, int backlog) This server side call specifies the number of pending connections on the given socket. When the server is processing a connection, “backlog” number of connections may be pending in a queue. TCPTCP ServerServer Side:Side: PasPasssiveive OpenOpen int accept (int socket, struct sockaddr *address, int *addr_len) This call is executed by the server. The call does not return until a remote client has established a connection. When it completes, it returns a new socket handle corresponding to the just-
  • 5. TCTCPP CClielienntt SiSidde:e: AAccttivivee OpOpenen int connect (int socket, struct sockaddr *address, int *addr_len) This call is executed by the client. *address contains the remote address. The call attempts to connect the socket to a server. It does not return until a connection has been established. When the call completes, the socket “socket” is connected and ready for communication. SSoocckkets:ets: SSuummmmaryary Client: int socket(int domain, int type, int protocol) int connect (int socket, struct sockaddr *address, int addr_len) Server: int socket(int domain, int type, int protocol) int bind (int socket, struct sockaddr *address, int addr_len) int listen (int socket, int backlog) int accept (int socket, struct sockaddr *address, int *addr_len) MessageMessage PassingPassing SSuummmmararyy ooff BasiBasicc SoSocckkeett CCaallslls int send (int socket, char *message, int msg_len, int flags) (TCP) int sendto (int socket, void *msg, int len, int CLIENT connect() Connect (3-way handshake) SERVER new connection accept() int tolen ); (UDP) int write(int socket, void *msg, int len); /* TCP */ write() read() int recv (int socket, char *buffer, int buf_len, int flags) (TCP) int recvfrom(int socket, void *msg, int len, int read() Data write() flags, struct sockaddr *from, int *fromlen); (UDP) int read(int socket, void *msg, int len); (TCP) close() close() NNeettwoworrkk BByyttee OrdOrderer Network byte order is most-significant byte first Byte ordering at a host may differ Utility functions – htons(): Host-to-network byte order for a short word (2 bytes) – htonl(): Host-to-network byte order for a long word (4 bytes) – ntohs(): Network-to-host byte order for a short word – ntohl(): Network-to-host byte order for a long word SSoommee OOtthherer ““UUtitillitityy”” FFununcctitiononss gethostname() -- get name of local host getpeername() -- get address of remote host getsockname() -- get local address of socket getXbyY() -- get protocol, host, or service number using known number, address, or port, respectively getsockopt() -- get current socket options setsockopt() -- set socket options ioctl() -- retrieve or set socket information
  • 6. SSoommee OOtthherer ““UUtitillitityy”” FFununcctitiononss inet_addr() -- convert “dotted” character string form of IP address to internal binary form inet_ntoa() -- convert internal binary form of IP address to “dotted” character string form AAdddressdress DataData StrStruuctctuurreses struct sockaddr { u_short sa_family; // type of address char sa_data[14]; // value of address } sockaddr is a generic address structure struct sockaddr_in { u_short sa_family; // type of address (AF_INET) u_short sa_port; // protocol port number struct in_addr sin_addr; // IP address char sin_zero[8]; // unused (set to zero)
  • 7. } sockaddr_in is specific instance for the Internet address family