SlideShare a Scribd company logo
Basics of sockets
Socket basics
 Socket basics
 Socket details (TCP and UDP)
 Socket options
Socket Basics (1 of 2)
 An end-point for an Internet network connection
 what the application layer “plugs into”
User Application
Socket
Operating System
Transport Layer
Internet Protocol Layer
 User sees “descriptor” - integer index or object handle
 like: FILE *, or file index from open()
 returned by socket() call (more later)
 programmer cares about Application Programming Interface (API)
Socket Basics (2 of 2)
 End point determined by two things:
 Host address: IP address is Network Layer
 Port number: is Transport Layer
 Two end-points determine a connection: socket pair
 ex: 206.62.226.35,p21 + 198.69.10.2,p1500
 ex: 206.62.226.35,p21 + 198.69.10.2,p1499
Ports
 Numbers (typical, since vary by OS):
 0-1023 “reserved”, must be root
 1024 - 5000 “ephemeral”
 Above 5000 for general use
 (50,000 is specified max)
 Well-known, reserved services (see /etc/services in
Unix):
 ftp 21/tcp
 telnet 23/tcp
 finger 79/tcp
 snmp 161/udp
Transport Layer
 UDP: User Datagram Protocol
 no acknowledgements
 no retransmissions
 out of order, duplicates possible
 connectionless
 TCP: Transmission Control Protocol
 reliable (in order, all arrive, no duplicates)
 flow control
 Connection-based
 While TCP ~95% of all flows and packets, much UDP
traffic is games!
Socket Details Outline
 Structure to hold address information
 Functions pass address from user to OS
bind()
connect()
sendto()
 Functions pass address from OS to user
accept()
recvfrom()
Process to Kernel
Kernel to Process
Socket Address Structure
struct in_addr {
in_addr_t s_addr; /* 32-bit IPv4
addresses */
};
struct sockaddr_in {
unit8_t sin_len; /* length of structure
*/
sa_family_t sin_family; /* AF_INET */
in_port_t sin_port; /* TCP/UDP Port
num */
struct in_addr sin_addr; /* IPv4 address
(above) */
char sin_zero[8]; /* unused */
}
 Are also “generic” and “IPv6” socket structures
Byte Ordering
Byte Ordering Functions
TCP Client-Server
socket()
bind()
listen()
accept()
Server
socket()
connect()
send()
recv()
Client
(Block until connection)
“Handshake”
recv()
send()
Data (request)
Data (reply)
close()End-of-File
recv()
close()
“well-known”
port
socket()
int socket(int family, int type, int protocol);
Create a socket, giving access to transport layer service.
 family is one of
 AF_INET (IPv4), AF_INET6 (IPv6), AF_LOCAL (local Unix),
 AF_ROUTE (access to routing tables), AF_KEY (new, for encryption)
 type is one of
 SOCK_STREAM (TCP), SOCK_DGRAM (UDP)
 SOCK_RAW (for special IP packets, PING, etc. Must be root)
 setuid bit (-rws--x--x root 1997 /sbin/ping*)
 protocol is 0 (used for some raw socket options)
 upon success returns socket descriptor
 Integer, like file descriptor
 Return -1 if failure
bind()
 sockfd is socket descriptor from socket()
 myaddr is a pointer to address struct with:
 port number and IP address
 if port is 0, then host will pick ephemeral port
 not usually for server (exception RPC port-map)
 IP address != INADDR_ANY (unless multiple nics)
 addrlen is length of structure
 returns 0 if ok, -1 on error
 EADDRINUSE (“Address already in use”)
int bind(int sockfd, const struct sockaddr *myaddr,
socklen_t addrlen);
Assign a local protocol address (“name”) to a socket.
listen()
 sockfd is socket descriptor from socket()
 backlog is maximum number of incomplete
connections
 historically 5
 rarely above 15 on a even moderate Web server!
 Sockets default to active (for a client)
 change to passive so OS will accept connection
int listen(int sockfd, int backlog);
Change socket state for TCP server.
accept()
 sockfd is socket descriptor from socket()
 cliaddr and addrlen return protocol address from
client
 returns brand new descriptor, created by OS
 note, if create new process or thread, can create
concurrent server
int accept(int sockfd, struct sockaddr cliaddr, socklen_t
*addrlen);
Return next completed connection.
close()
 sockfd is socket descriptor from socket()
 closes socket for reading/writing
 returns (doesn’t block)
 attempts to send any unsent data
 socket option SO_LINGER
 block until data sent
 or discard any remaining data
 returns -1 if error
int close(int sockfd);
Close socket for use.
TCP Client-Serversocket()
bind()
listen()
accept()
Server
socket()
connect()
send()
recv()
Client
(Block until connection)
“Handshake”
recv()
send()
Data (request)
Data (reply)
close()End-of-File
recv()
close()
“well-known”
port
connect()
 sockfd is socket descriptor from socket()
 servaddr is a pointer to a structure with:
 port number and IP address
 must be specified (unlike bind())
 addrlen is length of structure
 client doesn’t need bind()
 OS will pick ephemeral port
 returns socket descriptor if ok, -1 on error
int connect(int sockfd, const struct
sockaddr *servaddr, socklen_t addrlen);
Connect to server.
Sending and Receiving
int recv(int sockfd, void *buff, size_t
mbytes, int flags);
int send(int sockfd, void *buff, size_t
mbytes, int flags);
UDP Client-Server
socket()
bind()
recvfrom()
Server
socket()
sendto()
recvfrom()
Client
(Block until receive datagram)
sendto()
Data (request)
Data (reply)
close()
“well-known”
port
- No “handshake”
- No simultaneous close
- No fork()/spawn() for concurrent servers!
Sending and Receiving
int recvfrom(int sockfd, void *buff, size_t mbytes, int
flags, struct sockaddr *from, socklen_t *addrlen);
int sendto(int sockfd, void *buff, size_t mbytes, int
flags, const struct sockaddr *to, socklen_t addrlen);
 Same as recv() and send() but for addr
 recvfrom fills in address of where packet came
from
 sendto requires address of where sending packet
to
connect() with UDP
 Record address and port of peer
 datagrams to/from others are not allowed
 does not do three way handshake, or connection
 “connect” a misnomer, here. Should be setpeername()
 Use send() instead of sendto()
 Use recv() instead of recvfrom()
 Can change connect or unconnect by repeating connect()
call
 (Can do similar with bind() on receiver)
Why use connected UDP?
• Send two
datagrams
unconnected:
– connect the socket
– output first dgram
– unconnect the socket
– connect the socket
– ouput second dgram
– unconnect the socket
• Send two
datagrams
connected:
– connect the socket
– output first dgram
– ouput second dgram
Socket Options
 setsockopt(), getsockopt()
 SO_LINGER
 upon close, discard data or block until sent
 SO_RCVBUF, SO_SNDBUF
 change buffer sizes
 for TCP is “pipeline”, for UDP is “discard”
 SO_RCVLOWAT, SO_SNDLOWAT
 how much data before “readable” via select()
 SO_RCVTIMEO, SO_SNDTIMEO
 timeouts
fcntl()
 ‘File control’ but used for sockets, too
 Signal driven sockets
 Set socket owner
 Get socket owner
 Set socket non-blocking
flags = fcntl(sockfd, F_GETFL, 0);
flags |= O_NONBLOCK;
fcntl(sockfd, F_SETFL, flags);
 Beware not getting flags before setting!
Concurrent Servers
 Close sock in child, newsock in parent
 Reference count for socket descriptor
Text segment
sock = socket()
/* setup socket */
while (1) {
newsock = accept(sock)
fork()
if child
read(newsock)
until exit
}
Parent
int sock;
int newsock;
Child
int sock;
int newsock;

More Related Content

What's hot (20)

PPT
Np unit iii
vamsitricks
 
PDF
Programming TCP/IP with Sockets
elliando dias
 
PPT
Socket programming
harsh_bca06
 
PDF
Sockets
Indrasena Reddy
 
PPT
Socket programming in C
Deepak Swain
 
PPTX
Socket programming
Ujjwal Kumar
 
PPTX
IPC SOCKET
Sanoj Kumar
 
PPT
Sockets
sivindia
 
PDF
Socket programming using C
Ajit Nayak
 
PPT
Np unit iv ii
vamsitricks
 
PDF
Sockets
Rajesh Kumar
 
PPT
Java Socket Programming
Vipin Yadav
 
PDF
Socket programming using java
UC San Diego
 
PPT
A Short Java Socket Tutorial
Guo Albert
 
PPT
Np unit1
vamsitricks
 
DOC
socket programming
prashantzagade
 
PPT
Ports & sockets
myrajendra
 
PDF
Networking lab
Ragu Ram
 
PPT
Socket programming
chandramouligunnemeda
 
DOCX
Lab manual cn-2012-13
Sasi Kala
 
Np unit iii
vamsitricks
 
Programming TCP/IP with Sockets
elliando dias
 
Socket programming
harsh_bca06
 
Socket programming in C
Deepak Swain
 
Socket programming
Ujjwal Kumar
 
IPC SOCKET
Sanoj Kumar
 
Sockets
sivindia
 
Socket programming using C
Ajit Nayak
 
Np unit iv ii
vamsitricks
 
Sockets
Rajesh Kumar
 
Java Socket Programming
Vipin Yadav
 
Socket programming using java
UC San Diego
 
A Short Java Socket Tutorial
Guo Albert
 
Np unit1
vamsitricks
 
socket programming
prashantzagade
 
Ports & sockets
myrajendra
 
Networking lab
Ragu Ram
 
Socket programming
chandramouligunnemeda
 
Lab manual cn-2012-13
Sasi Kala
 

Viewers also liked (20)

PDF
Jnp
hj43us
 
PPT
Networking Java Socket Programming
Mousmi Pawar
 
PPT
Network programming in Java
Tushar B Kute
 
PPT
Java Basics
shivamgarg_nitj
 
PPT
Networking Chapter 4
mlrbrown
 
PPT
Networking Chapter 5
mlrbrown
 
PDF
Profinet Design
rodrigo_the_one
 
PDF
Profinet system design - Andy Verwer
PROFIBUS and PROFINET InternationaI - PI UK
 
PDF
Profibus commissioning and maintenance - Richard Needham
PROFIBUS and PROFINET InternationaI - PI UK
 
PPT
Tcp sockets
babak danyal
 
PPT
Easy Steps to implement UDP Server and Client Sockets
babak danyal
 
PDF
W4 profinet frame analysis, peter thomas
PROFIBUS and PROFINET InternationaI - PI UK
 
PDF
Profibus and Profinet shield currents - Peter Thomas
PROFIBUS and PROFINET InternationaI - PI UK
 
PDF
Introduction to PROFINET - Derek Lane of Wago
PROFIBUS and PROFINET InternationaI - PI UK
 
PDF
Introduction to Profibus & Profinet - Mark Freeman
PROFIBUS and PROFINET InternationaI - PI UK
 
PPTX
Network programming in java - PPT
kamal kotecha
 
PPT
Wireshark - presentation
Kateryna Haskova
 
PPTX
Network Packet Analysis with Wireshark
Jim Gilsinn
 
PPT
Wireshark Basics
Yoram Orzach
 
PPT
Ppt of socket
Amandeep Kaur
 
Jnp
hj43us
 
Networking Java Socket Programming
Mousmi Pawar
 
Network programming in Java
Tushar B Kute
 
Java Basics
shivamgarg_nitj
 
Networking Chapter 4
mlrbrown
 
Networking Chapter 5
mlrbrown
 
Profinet Design
rodrigo_the_one
 
Profinet system design - Andy Verwer
PROFIBUS and PROFINET InternationaI - PI UK
 
Profibus commissioning and maintenance - Richard Needham
PROFIBUS and PROFINET InternationaI - PI UK
 
Tcp sockets
babak danyal
 
Easy Steps to implement UDP Server and Client Sockets
babak danyal
 
W4 profinet frame analysis, peter thomas
PROFIBUS and PROFINET InternationaI - PI UK
 
Profibus and Profinet shield currents - Peter Thomas
PROFIBUS and PROFINET InternationaI - PI UK
 
Introduction to PROFINET - Derek Lane of Wago
PROFIBUS and PROFINET InternationaI - PI UK
 
Introduction to Profibus & Profinet - Mark Freeman
PROFIBUS and PROFINET InternationaI - PI UK
 
Network programming in java - PPT
kamal kotecha
 
Wireshark - presentation
Kateryna Haskova
 
Network Packet Analysis with Wireshark
Jim Gilsinn
 
Wireshark Basics
Yoram Orzach
 
Ppt of socket
Amandeep Kaur
 
Ad

Similar to Basics of sockets (20)

PPT
Introduction to sockets tcp ip protocol.ppt
MajedAboubennah
 
PPT
Sockets
Gopaiah Sanaka
 
PPTX
L5-Sockets.pptx
ycelgemici1
 
PPT
sockets_intro.ppt
AnilGupta681764
 
PDF
socketProgramming-TCP-and UDP-overview.pdf
Shilpachaudhari10
 
PPT
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
senthilnathans25
 
PPT
Sockets intro
AviNash ChaVhan
 
PPT
Socket System Calls
Avinash Varma Kalidindi
 
PPTX
Lecture 1 Socket programming elementary tcp sockets.pptx
MonaSayed27
 
PDF
lab04.pdf
SaidiCalala
 
PPTX
Socket programming
Anurag Tomar
 
PPTX
Byte Ordering - Unit 2.pptx
RockyBhai46825
 
PPT
Multiplayer Game Programming Berkeley Socket API Chapter 3.ppt
MoissFreitas13
 
PDF
Network Programming Assignment Help
HelpWithAssignment.com
 
DOC
socket programming
prashantzagade
 
PPT
Basic socket programming
Kristian Arjianto
 
PPTX
Sockets
sikhinamnagamani
 
PDF
Socket programming
Rajivarnan (Rajiv)
 
PDF
sockets
AbhinavRapartiwar
 
PPT
03-socketprogramming for college students.ppt
SoumabhaRoy
 
Introduction to sockets tcp ip protocol.ppt
MajedAboubennah
 
Sockets
Gopaiah Sanaka
 
L5-Sockets.pptx
ycelgemici1
 
sockets_intro.ppt
AnilGupta681764
 
socketProgramming-TCP-and UDP-overview.pdf
Shilpachaudhari10
 
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
senthilnathans25
 
Sockets intro
AviNash ChaVhan
 
Socket System Calls
Avinash Varma Kalidindi
 
Lecture 1 Socket programming elementary tcp sockets.pptx
MonaSayed27
 
lab04.pdf
SaidiCalala
 
Socket programming
Anurag Tomar
 
Byte Ordering - Unit 2.pptx
RockyBhai46825
 
Multiplayer Game Programming Berkeley Socket API Chapter 3.ppt
MoissFreitas13
 
Network Programming Assignment Help
HelpWithAssignment.com
 
socket programming
prashantzagade
 
Basic socket programming
Kristian Arjianto
 
Socket programming
Rajivarnan (Rajiv)
 
03-socketprogramming for college students.ppt
SoumabhaRoy
 
Ad

Recently uploaded (20)

PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Python basic programing language for automation
DanialHabibi2
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Python basic programing language for automation
DanialHabibi2
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 

Basics of sockets

  • 2. Socket basics  Socket basics  Socket details (TCP and UDP)  Socket options
  • 3. Socket Basics (1 of 2)  An end-point for an Internet network connection  what the application layer “plugs into” User Application Socket Operating System Transport Layer Internet Protocol Layer  User sees “descriptor” - integer index or object handle  like: FILE *, or file index from open()  returned by socket() call (more later)  programmer cares about Application Programming Interface (API)
  • 4. Socket Basics (2 of 2)  End point determined by two things:  Host address: IP address is Network Layer  Port number: is Transport Layer  Two end-points determine a connection: socket pair  ex: 206.62.226.35,p21 + 198.69.10.2,p1500  ex: 206.62.226.35,p21 + 198.69.10.2,p1499
  • 5. Ports  Numbers (typical, since vary by OS):  0-1023 “reserved”, must be root  1024 - 5000 “ephemeral”  Above 5000 for general use  (50,000 is specified max)  Well-known, reserved services (see /etc/services in Unix):  ftp 21/tcp  telnet 23/tcp  finger 79/tcp  snmp 161/udp
  • 6. Transport Layer  UDP: User Datagram Protocol  no acknowledgements  no retransmissions  out of order, duplicates possible  connectionless  TCP: Transmission Control Protocol  reliable (in order, all arrive, no duplicates)  flow control  Connection-based  While TCP ~95% of all flows and packets, much UDP traffic is games!
  • 7. Socket Details Outline  Structure to hold address information  Functions pass address from user to OS bind() connect() sendto()  Functions pass address from OS to user accept() recvfrom()
  • 10. Socket Address Structure struct in_addr { in_addr_t s_addr; /* 32-bit IPv4 addresses */ }; struct sockaddr_in { unit8_t sin_len; /* length of structure */ sa_family_t sin_family; /* AF_INET */ in_port_t sin_port; /* TCP/UDP Port num */ struct in_addr sin_addr; /* IPv4 address (above) */ char sin_zero[8]; /* unused */ }  Are also “generic” and “IPv6” socket structures
  • 13. TCP Client-Server socket() bind() listen() accept() Server socket() connect() send() recv() Client (Block until connection) “Handshake” recv() send() Data (request) Data (reply) close()End-of-File recv() close() “well-known” port
  • 14. socket() int socket(int family, int type, int protocol); Create a socket, giving access to transport layer service.  family is one of  AF_INET (IPv4), AF_INET6 (IPv6), AF_LOCAL (local Unix),  AF_ROUTE (access to routing tables), AF_KEY (new, for encryption)  type is one of  SOCK_STREAM (TCP), SOCK_DGRAM (UDP)  SOCK_RAW (for special IP packets, PING, etc. Must be root)  setuid bit (-rws--x--x root 1997 /sbin/ping*)  protocol is 0 (used for some raw socket options)  upon success returns socket descriptor  Integer, like file descriptor  Return -1 if failure
  • 15. bind()  sockfd is socket descriptor from socket()  myaddr is a pointer to address struct with:  port number and IP address  if port is 0, then host will pick ephemeral port  not usually for server (exception RPC port-map)  IP address != INADDR_ANY (unless multiple nics)  addrlen is length of structure  returns 0 if ok, -1 on error  EADDRINUSE (“Address already in use”) int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); Assign a local protocol address (“name”) to a socket.
  • 16. listen()  sockfd is socket descriptor from socket()  backlog is maximum number of incomplete connections  historically 5  rarely above 15 on a even moderate Web server!  Sockets default to active (for a client)  change to passive so OS will accept connection int listen(int sockfd, int backlog); Change socket state for TCP server.
  • 17. accept()  sockfd is socket descriptor from socket()  cliaddr and addrlen return protocol address from client  returns brand new descriptor, created by OS  note, if create new process or thread, can create concurrent server int accept(int sockfd, struct sockaddr cliaddr, socklen_t *addrlen); Return next completed connection.
  • 18. close()  sockfd is socket descriptor from socket()  closes socket for reading/writing  returns (doesn’t block)  attempts to send any unsent data  socket option SO_LINGER  block until data sent  or discard any remaining data  returns -1 if error int close(int sockfd); Close socket for use.
  • 19. TCP Client-Serversocket() bind() listen() accept() Server socket() connect() send() recv() Client (Block until connection) “Handshake” recv() send() Data (request) Data (reply) close()End-of-File recv() close() “well-known” port
  • 20. connect()  sockfd is socket descriptor from socket()  servaddr is a pointer to a structure with:  port number and IP address  must be specified (unlike bind())  addrlen is length of structure  client doesn’t need bind()  OS will pick ephemeral port  returns socket descriptor if ok, -1 on error int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); Connect to server.
  • 21. Sending and Receiving int recv(int sockfd, void *buff, size_t mbytes, int flags); int send(int sockfd, void *buff, size_t mbytes, int flags);
  • 22. UDP Client-Server socket() bind() recvfrom() Server socket() sendto() recvfrom() Client (Block until receive datagram) sendto() Data (request) Data (reply) close() “well-known” port - No “handshake” - No simultaneous close - No fork()/spawn() for concurrent servers!
  • 23. Sending and Receiving int recvfrom(int sockfd, void *buff, size_t mbytes, int flags, struct sockaddr *from, socklen_t *addrlen); int sendto(int sockfd, void *buff, size_t mbytes, int flags, const struct sockaddr *to, socklen_t addrlen);  Same as recv() and send() but for addr  recvfrom fills in address of where packet came from  sendto requires address of where sending packet to
  • 24. connect() with UDP  Record address and port of peer  datagrams to/from others are not allowed  does not do three way handshake, or connection  “connect” a misnomer, here. Should be setpeername()  Use send() instead of sendto()  Use recv() instead of recvfrom()  Can change connect or unconnect by repeating connect() call  (Can do similar with bind() on receiver)
  • 25. Why use connected UDP? • Send two datagrams unconnected: – connect the socket – output first dgram – unconnect the socket – connect the socket – ouput second dgram – unconnect the socket • Send two datagrams connected: – connect the socket – output first dgram – ouput second dgram
  • 26. Socket Options  setsockopt(), getsockopt()  SO_LINGER  upon close, discard data or block until sent  SO_RCVBUF, SO_SNDBUF  change buffer sizes  for TCP is “pipeline”, for UDP is “discard”  SO_RCVLOWAT, SO_SNDLOWAT  how much data before “readable” via select()  SO_RCVTIMEO, SO_SNDTIMEO  timeouts
  • 27. fcntl()  ‘File control’ but used for sockets, too  Signal driven sockets  Set socket owner  Get socket owner  Set socket non-blocking flags = fcntl(sockfd, F_GETFL, 0); flags |= O_NONBLOCK; fcntl(sockfd, F_SETFL, flags);  Beware not getting flags before setting!
  • 28. Concurrent Servers  Close sock in child, newsock in parent  Reference count for socket descriptor Text segment sock = socket() /* setup socket */ while (1) { newsock = accept(sock) fork() if child read(newsock) until exit } Parent int sock; int newsock; Child int sock; int newsock;