SlideShare a Scribd company logo
Operating Systems
Sockets
ENCE 360
Outline
• Introduction
• Details
• Example code
• Socket options+
Socket Overview
(TCP=Transport Control Protocol,
IP=Internet Protocol)
Application
Sockets
Protocol A Protocol B Protocol C
Network
• Socket - An end-point for
connection to another process
(remote or local)
– What application layer “plugs
into”
• User sees descriptor - integer
index/handle
– Like: file index from open()
– Returned by socket() call (more
later)
– Programmer cares about
Application Programming
Interface (API)  similar to file I/O
Connection Endpoints
• End point determined by two
things:
– Host address: e.g., IP address
– Port number
• Two end-points determine
connection  socket pair
Client
Port
Connection
Sever
Port
message
agreed port
any port socket
socket
Internet address = 138.37.88.249
Internet address = 138.37.94.248
other ports
client server
Ports
port 0
port 1
port 65535
• Each host has 65,536 ports
– 16-bit integer
• Some ports are reserved for
specific apps (/etc/services)
– FTP 20,21
– Telnet 23
– HTTP 80
• Ports below 1024 are reserved
– User level 1024+
• Ports 1024-5000 ephemeral
– Assigned in outgoing connection
• Ports 5001+ services
server client
port
Network Packet
port port port port
app app app app
port data
data
Packet
Two Main Network Transport
Protocols Today
• UDP: User Datagram Protocol
– no acknowledgements
– no retransmissions
– out of order, duplicates possible
– Connectionless
– SOCK_DGRAM
• TCP: Transmission Control Protocol
– reliable (in order, all arrive, no
duplicates)
– flow control
– connection-based
– SOCK_STREAM More in a
networks
course!
TCP ~95% of all flows
and packets on Internet
(What applications may
use UDP?)
Application
(http,ftp,telnet,…)
Transport
(TCP, UDP,..)
Network
(IP,..)
Link
(device driver,..)
Socket Descriptor Data Structure
Descriptor Table
0
1
2
3
4
Family: PF_INET
Service: SOCK_STREAM
Local IP: 111.22.3.4
Remote IP: 123.45.6.78
Local Port: 2249
Remote Port: 3726
received
queue
sent
queue
Socket Data Structure
Service is the Transport Protocol
IP (Internet Protocol) - address of computer
Port - specifies which process on computer
Outline
• Introduction (done)
• Details (next)
• Example code
• Socket options+
Unix Network Programming, W.
Richard Stevens, 2nd edition,
1998, Prentice Hall
Beej’s Guide to Network Programming,
Brian Hall, 2015, self-published,
http://beej.us/guide/bgnet/
Addresses and Sockets
• Structure to hold address information
• Functions pass info (e.g., address) from user to OS
bind()
connect()
sendto()
• Functions pass info (e.g., address) from OS to user
accept()
recvfrom()
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 number */
struct in_addr sin_addr; /* IPv4 address (above) */
char sin_zero[8];/* unused */
};
Also “generic” and “IPv6” socket structures
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 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 (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 (-rwsr-xr-x root 2014 /sbin/ping*)
• protocol is 0 (used for some raw socket options)
• upon success returns socket descriptor
– Integer, like file descriptor  index used internally
– Return -1 if failure
bind()
• sockfd is socket descriptor from socket()
• myaddr is 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 local protocol address (“name”) to socket
listen()
• sockfd is socket descriptor from socket()
• backlog is maximum number of incomplete
connections
– historically 5
– rarely above 15 even on moderately busy Web server!
• sockets default to active (for client)
– change to passive so OS will accept connection
int listen(int sockfd, int backlog);
Change socket state (to passive) for TCP server
accept()
• blocking call (by default)
• 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-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
connect()
• sockfd is socket descriptor from socket()
• servaddr is pointer to 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);
• Same as read() and write() but with flags
– MSG_DONTWAIT (this send non-blocking)
– MSG_OOB (out of band data, 1 byte sent ahead)
– MSG_PEEK (look, but don’t remove)
– MSG_WAITALL (don’t return less than mbytes)
– MSG_DONTROUTE (bypass routing table)
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 “connection”, no “handshake”
- No simultaneous close
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 with addr
– recvfrom fills in address of where packet came
from
– sendto requires address of where sending
packet to
Can connect() with UDP
• Record address and port of peer
– Datagrams to/from others are not allowed
– Does not do three way handshake, or connection
– So, “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)
Outline
• Introduction (done)
• Details (done)
• Example code (next)
• Socket options+
Example Code
% listen-tcp
listen-tcp - server to accept TCP connections
usage: listen-tcp <port>
<port> - port to listen on
% listen-tcp 7500
Listen activating.
Trying to create socket at port 7500...
Socket ready to go! Accepting connections....
received: 'Hello, world!'
received: 'Networking is awesome!'
server exiting
% talk-tcp
talk-tcp - client to try TCP connection to server
usage: talk-tcp <host> <port>
<host> - Internet name of server host
<port> - port
% talk-tcp localhost 7500
Talk activated.
Trying to connect to server localhost at port 7500...
Looking up localhost...
Found it. Setting port connection to 7500...
Done. Creating socket...
Created. Trying connection to server...
Connection established!
Type in messages to send to server.
Hello, world!
sending: 'Hello, world!'
Networking is awesome!
sending: 'Networking is awesome!'
Server Client
See:
“listen-tcp.c”
“talk-tcp.c”
1
2
3
4
Outline
• Introduction (done)
• Details (done)
• Example code (done)
• Socket options+ (next)
Socket Options (General)
• 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
Socket Options (TCP)
• TCP_KEEPALIVE
– Idle time before close (2 hours, default)
• TCP_MAXRT
– Set timeout value
• TCP_NODELAY
– Disable Nagle’s Algorithm
– Won’t buffer data for larger chunk, but sends immediately
fcntl()
• ‘File control’ - manipulate file descriptor
– So, used for sockets, too
• Get/Set socket owner
• Get/Set socket lock
• Set socket non-blocking
flags = fcntl(sockfd, F_GETFL, 0);
flags |= O_NONBLOCK;
fcntl(sockfd, F_SETFL, flags);
• Beware not getting flags before setting!
Connecting to the Internet
• If one computer, connect to server?
Internet Connecting to You
• If one computer, connect to server?
• Server connect to your computer?
Firewall to the Rescue!
Close all ports!
Note: even if no
services (processes),
OS response still gives
information to hackers!
(“fingerprint”)
Firewall Too Restrictive
• What if want connection? (Examples?)
Close all ports!
Note: even if no
services (processes),
OS response still gives
information to hackers!
(“fingerprint”)
Firewall – Port Forwarding
• What if want connection? (Examples?)
Open port.
Aka:
port forwarding
Close all ports!
Note: even if no
services (processes),
OS response still gives
information to hackers!
(“fingerprint”)
Connecting to the Internet, Take 2
• Single address,
multiple
computers?
202.36.179.10
104.45.147.81
(World)
(Home)
?
Connecting to the Internet, Take 2
• Single address,
multiple
computers?
– Assign one
box to route
packets to all
• But how to
differentiate
incoming?
192.168.1.3
192.168.1.2
192.168.1.4
192.168.1.1
202.36.179.10
104.45.147.81
(World)
(Home)
202.36.179.10
?
Network Address Translation (NAT)
• Single address,
multiple
computers?
– Assign one box
to route
packets to all
• But how to
differentiate
incoming?
– Based on port
– Setup when
connecting
192.168.1.3
192.168.1.2
192.168.1.4
192.168.1.1
202.36.179.10
104.45.147.81
(World)
(Home)
202.36.179.10
80
192.168.1.2
9991
Network Address Translation
NAT Tables
• Table maps local IP + port to Internet IP + port
• Setup when connected by client (punch out)
• Pre-setup by administrator when connecting in (local service/server)
https://microchip.wdfiles.com/local--files/tcpip:nat-translation-table/nat_table.JPG
Outline
• Introduction (done)
• Details (done)
• Example code (done)
• Socket options+ (done)

More Related Content

PPT
Introduction to sockets tcp ip protocol.ppt
PPT
Sockets in unix
PPTX
Basics of sockets
PPT
Socket Programming
PPT
03-socketprogramming for college students.ppt
PPT
03-socketprogrsamming forcoleeger students.ppt
PPT
Sockets
PDF
Socket programming using C
Introduction to sockets tcp ip protocol.ppt
Sockets in unix
Basics of sockets
Socket Programming
03-socketprogramming for college students.ppt
03-socketprogrsamming forcoleeger students.ppt
Sockets
Socket programming using C

Similar to L5-Sockets.pptx (20)

PPT
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
PPT
sockets_intro.ppt
PPT
Sockets intro
PPT
Basic socket programming
PPT
Socket programming-tutorial-sk
PPT
Multiplayer Game Programming Berkeley Socket API Chapter 3.ppt
PPTX
Byte Ordering - Unit 2.pptx
PPT
Unit 8 Java
PPT
LECTURE-17(Socket Programming) Detailed.
PPT
Sockets.ppt socket sofcv ohghjagshsdjjhjfb
PPTX
Socket programming
PDF
Sockets
PDF
Network Sockets
PPTX
Socket programming in c
PPTX
Socket programming
PPT
Network Prog.ppt
PPT
Network programming-Network for engineering
PPT
Np unit2
PPT
Socket programming in C
PPT
Application Layer and Socket Programming
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
sockets_intro.ppt
Sockets intro
Basic socket programming
Socket programming-tutorial-sk
Multiplayer Game Programming Berkeley Socket API Chapter 3.ppt
Byte Ordering - Unit 2.pptx
Unit 8 Java
LECTURE-17(Socket Programming) Detailed.
Sockets.ppt socket sofcv ohghjagshsdjjhjfb
Socket programming
Sockets
Network Sockets
Socket programming in c
Socket programming
Network Prog.ppt
Network programming-Network for engineering
Np unit2
Socket programming in C
Application Layer and Socket Programming
Ad

Recently uploaded (20)

PDF
Cableado de Controladores Logicos Programables
PPTX
KVL KCL ppt electrical electronics eee tiet
PPTX
INFERTILITY (FEMALE FACTORS).pptxgvcghhfcg
PPT
FABRICATION OF MOS FET BJT DEVICES IN NANOMETER
PPTX
code of ethics.pptxdvhwbssssSAssscasascc
PPTX
Syllabus Computer Six class curriculum s
PPTX
title _yeOPC_Poisoning_Presentation.pptx
PPTX
making presentation that do no stick.pptx
PPTX
PROGRAMMING-QUARTER-2-PYTHON.pptxnsnsndn
PPTX
Sem-8 project ppt fortvfvmat uyyjhuj.pptx
PPTX
dhcp concept.pptxfeegrvewfegrgerhtrhtrhredew
PPTX
Lecture-3-Computer-programming for BS InfoTech
PDF
Lifting Equipment Inspection Checklist with eAuditor Audits & Inspections
PPTX
ERP good ERP good ERP good ERP good good ERP good ERP good
PPTX
material for studying about lift elevators escalation
PPTX
udi-benefits-ggggggggfor-healthcare.pptx
PDF
How NGOs Save Costs with Affordable IT Rentals
PPTX
English grade 10 st augusitne eoeoknkklm
PPTX
了解新西兰毕业证(Wintec毕业证书)怀卡托理工学院毕业证存档可查的
PPTX
1.pptxsadafqefeqfeqfeffeqfqeqfeqefqfeqfqeffqe
Cableado de Controladores Logicos Programables
KVL KCL ppt electrical electronics eee tiet
INFERTILITY (FEMALE FACTORS).pptxgvcghhfcg
FABRICATION OF MOS FET BJT DEVICES IN NANOMETER
code of ethics.pptxdvhwbssssSAssscasascc
Syllabus Computer Six class curriculum s
title _yeOPC_Poisoning_Presentation.pptx
making presentation that do no stick.pptx
PROGRAMMING-QUARTER-2-PYTHON.pptxnsnsndn
Sem-8 project ppt fortvfvmat uyyjhuj.pptx
dhcp concept.pptxfeegrvewfegrgerhtrhtrhredew
Lecture-3-Computer-programming for BS InfoTech
Lifting Equipment Inspection Checklist with eAuditor Audits & Inspections
ERP good ERP good ERP good ERP good good ERP good ERP good
material for studying about lift elevators escalation
udi-benefits-ggggggggfor-healthcare.pptx
How NGOs Save Costs with Affordable IT Rentals
English grade 10 st augusitne eoeoknkklm
了解新西兰毕业证(Wintec毕业证书)怀卡托理工学院毕业证存档可查的
1.pptxsadafqefeqfeqfeffeqfqeqfeqefqfeqfqeffqe
Ad

L5-Sockets.pptx

  • 2. Outline • Introduction • Details • Example code • Socket options+
  • 3. Socket Overview (TCP=Transport Control Protocol, IP=Internet Protocol) Application Sockets Protocol A Protocol B Protocol C Network • Socket - An end-point for connection to another process (remote or local) – What application layer “plugs into” • User sees descriptor - integer index/handle – Like: file index from open() – Returned by socket() call (more later) – Programmer cares about Application Programming Interface (API)  similar to file I/O
  • 4. Connection Endpoints • End point determined by two things: – Host address: e.g., IP address – Port number • Two end-points determine connection  socket pair Client Port Connection Sever Port message agreed port any port socket socket Internet address = 138.37.88.249 Internet address = 138.37.94.248 other ports client server
  • 5. Ports port 0 port 1 port 65535 • Each host has 65,536 ports – 16-bit integer • Some ports are reserved for specific apps (/etc/services) – FTP 20,21 – Telnet 23 – HTTP 80 • Ports below 1024 are reserved – User level 1024+ • Ports 1024-5000 ephemeral – Assigned in outgoing connection • Ports 5001+ services server client port Network Packet port port port port app app app app port data data Packet
  • 6. Two Main Network Transport Protocols Today • UDP: User Datagram Protocol – no acknowledgements – no retransmissions – out of order, duplicates possible – Connectionless – SOCK_DGRAM • TCP: Transmission Control Protocol – reliable (in order, all arrive, no duplicates) – flow control – connection-based – SOCK_STREAM More in a networks course! TCP ~95% of all flows and packets on Internet (What applications may use UDP?) Application (http,ftp,telnet,…) Transport (TCP, UDP,..) Network (IP,..) Link (device driver,..)
  • 7. Socket Descriptor Data Structure Descriptor Table 0 1 2 3 4 Family: PF_INET Service: SOCK_STREAM Local IP: 111.22.3.4 Remote IP: 123.45.6.78 Local Port: 2249 Remote Port: 3726 received queue sent queue Socket Data Structure Service is the Transport Protocol IP (Internet Protocol) - address of computer Port - specifies which process on computer
  • 8. Outline • Introduction (done) • Details (next) • Example code • Socket options+ Unix Network Programming, W. Richard Stevens, 2nd edition, 1998, Prentice Hall Beej’s Guide to Network Programming, Brian Hall, 2015, self-published, http://beej.us/guide/bgnet/
  • 9. Addresses and Sockets • Structure to hold address information • Functions pass info (e.g., address) from user to OS bind() connect() sendto() • Functions pass info (e.g., 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 number */ struct in_addr sin_addr; /* IPv4 address (above) */ char sin_zero[8];/* unused */ }; Also “generic” and “IPv6” socket structures
  • 11. 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
  • 12. socket() int socket(int family, int type, int protocol); Create 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 (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 (-rwsr-xr-x root 2014 /sbin/ping*) • protocol is 0 (used for some raw socket options) • upon success returns socket descriptor – Integer, like file descriptor  index used internally – Return -1 if failure
  • 13. bind() • sockfd is socket descriptor from socket() • myaddr is 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 local protocol address (“name”) to socket
  • 14. listen() • sockfd is socket descriptor from socket() • backlog is maximum number of incomplete connections – historically 5 – rarely above 15 even on moderately busy Web server! • sockets default to active (for client) – change to passive so OS will accept connection int listen(int sockfd, int backlog); Change socket state (to passive) for TCP server
  • 15. accept() • blocking call (by default) • 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
  • 16. 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
  • 17. 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
  • 18. connect() • sockfd is socket descriptor from socket() • servaddr is pointer to 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
  • 19. 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); • Same as read() and write() but with flags – MSG_DONTWAIT (this send non-blocking) – MSG_OOB (out of band data, 1 byte sent ahead) – MSG_PEEK (look, but don’t remove) – MSG_WAITALL (don’t return less than mbytes) – MSG_DONTROUTE (bypass routing table)
  • 20. 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 “connection”, no “handshake” - No simultaneous close
  • 21. 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 with addr – recvfrom fills in address of where packet came from – sendto requires address of where sending packet to
  • 22. Can connect() with UDP • Record address and port of peer – Datagrams to/from others are not allowed – Does not do three way handshake, or connection – So, “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)
  • 23. Outline • Introduction (done) • Details (done) • Example code (next) • Socket options+
  • 24. Example Code % listen-tcp listen-tcp - server to accept TCP connections usage: listen-tcp <port> <port> - port to listen on % listen-tcp 7500 Listen activating. Trying to create socket at port 7500... Socket ready to go! Accepting connections.... received: 'Hello, world!' received: 'Networking is awesome!' server exiting % talk-tcp talk-tcp - client to try TCP connection to server usage: talk-tcp <host> <port> <host> - Internet name of server host <port> - port % talk-tcp localhost 7500 Talk activated. Trying to connect to server localhost at port 7500... Looking up localhost... Found it. Setting port connection to 7500... Done. Creating socket... Created. Trying connection to server... Connection established! Type in messages to send to server. Hello, world! sending: 'Hello, world!' Networking is awesome! sending: 'Networking is awesome!' Server Client See: “listen-tcp.c” “talk-tcp.c” 1 2 3 4
  • 25. Outline • Introduction (done) • Details (done) • Example code (done) • Socket options+ (next)
  • 26. Socket Options (General) • 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. Socket Options (TCP) • TCP_KEEPALIVE – Idle time before close (2 hours, default) • TCP_MAXRT – Set timeout value • TCP_NODELAY – Disable Nagle’s Algorithm – Won’t buffer data for larger chunk, but sends immediately
  • 28. fcntl() • ‘File control’ - manipulate file descriptor – So, used for sockets, too • Get/Set socket owner • Get/Set socket lock • Set socket non-blocking flags = fcntl(sockfd, F_GETFL, 0); flags |= O_NONBLOCK; fcntl(sockfd, F_SETFL, flags); • Beware not getting flags before setting!
  • 29. Connecting to the Internet • If one computer, connect to server?
  • 30. Internet Connecting to You • If one computer, connect to server? • Server connect to your computer?
  • 31. Firewall to the Rescue! Close all ports! Note: even if no services (processes), OS response still gives information to hackers! (“fingerprint”)
  • 32. Firewall Too Restrictive • What if want connection? (Examples?) Close all ports! Note: even if no services (processes), OS response still gives information to hackers! (“fingerprint”)
  • 33. Firewall – Port Forwarding • What if want connection? (Examples?) Open port. Aka: port forwarding Close all ports! Note: even if no services (processes), OS response still gives information to hackers! (“fingerprint”)
  • 34. Connecting to the Internet, Take 2 • Single address, multiple computers? 202.36.179.10 104.45.147.81 (World) (Home) ?
  • 35. Connecting to the Internet, Take 2 • Single address, multiple computers? – Assign one box to route packets to all • But how to differentiate incoming? 192.168.1.3 192.168.1.2 192.168.1.4 192.168.1.1 202.36.179.10 104.45.147.81 (World) (Home) 202.36.179.10 ?
  • 36. Network Address Translation (NAT) • Single address, multiple computers? – Assign one box to route packets to all • But how to differentiate incoming? – Based on port – Setup when connecting 192.168.1.3 192.168.1.2 192.168.1.4 192.168.1.1 202.36.179.10 104.45.147.81 (World) (Home) 202.36.179.10 80 192.168.1.2 9991 Network Address Translation
  • 37. NAT Tables • Table maps local IP + port to Internet IP + port • Setup when connected by client (punch out) • Pre-setup by administrator when connecting in (local service/server) https://microchip.wdfiles.com/local--files/tcpip:nat-translation-table/nat_table.JPG
  • 38. Outline • Introduction (done) • Details (done) • Example code (done) • Socket options+ (done)

Editor's Notes

  • #11: Length field makes it easier for OS to handle