Application Layer and Socket Programmingelliando dias
The document discusses computer networking concepts related to the application layer and socket programming. It provides an overview of client-server applications and protocols, and describes how sockets allow applications to communicate over TCP and UDP. Specifically, it explains how servers can use sockets, binding, listening and accepting to establish TCP connections with clients. It also explains how clients can use sockets and connecting to communicate with servers. For UDP, it describes how servers can use sockets and binding to receive datagrams, while clients can use sockets and sending to communicate with servers.
This document provides an overview of socket programming. It discusses what sockets are, which are endpoints for communication between an application and the TCP/IP protocol stack. Sockets are represented by file descriptors that allow applications to read and write data to the network. It then covers protocols like TCP and UDP, the client-server programming paradigm, and the common socket API. TCP provides reliable, in-order connections, while UDP is unreliable but supports broadcasting. Sample code demonstrates how sockets are created and used in both TCP and UDP client-server applications.
The document discusses the TCP/IP protocol suite and transport layer services. Some key points:
- TCP/IP was originally developed by DARPA and later included in UNIX. It maps to the OSI layers and supports various physical/data link protocols.
- The transport layer provides logical communication between application processes on different hosts. TCP and UDP are the main transport protocols.
- TCP provides reliable, in-order byte streams using connection establishment and acknowledgments. UDP is a simpler connectionless protocol.
- Port numbers and IP addresses are used to multiplex/demultiplex segments between sockets at hosts for processes to communicate.
- TCP uses a three-way handshake to establish reliable connections between
This document discusses networking concepts in Java including:
- Computer networking allows computers to send and receive messages over the Internet through an Internet Service Provider using technologies like dialup, DSL, or cable modem.
- Java networking allows sharing of resources and centralized software management between connected devices.
- IP addresses uniquely identify computers on the Internet and are assigned to each network node, while domain names provide friendly names that map to IP addresses via DNS servers.
- Common network protocols like TCP and UDP define rules for communication, with TCP being connection-oriented and reliable and UDP being connectionless and faster.
- Client-server models involve clients making requests to servers which provide shared resources, with ports identifying applications and IP addresses locating
The document outlines a 15 week semester plan for learning network programming. It includes 7 topics covered over the semester with associated tasks, estimated time commitments, and learning outcomes. The topics progress from an overview of networking concepts to implementing client-server applications and multithreaded network programming.
09 Systems Software Programming-Network Programming.pptxKushalSrivastava23
This document discusses client-server networking and the TCP/IP protocol stack.
It begins by explaining the client-server model and how servers manage resources for clients. It then describes the layers of a computer network from SAN to WAN. The document discusses how Ethernet segments, bridges, and routers connect local area networks. It introduces the concepts of internet protocols and how they provide naming and delivery of packets across incompatible networks. The roles of IP, TCP, UDP, and sockets in client-server communication are summarized. Finally, it provides examples of functions like getaddrinfo() and getnameinfo() for host and service name resolution.
This document discusses various topics related to computer networking including protocols, sockets, IP addresses, URLs, reading web page source code, downloading files from the internet, TCP/IP and UDP servers and clients, file servers and clients, two-way communication between servers and clients, and sending simple emails. It provides information on networking hardware and software, protocols like TCP/IP and UDP, socket programming, parsing URLs, creating servers and clients, and sending/receiving data over networks.
Aplication and Transport layer- a practical approachSarah R. Dowlath
The document discusses the application layer and transport layer in network communication, detailing how they interact and the protocols involved, such as DNS, HTTP, SMTP, and FTP. It emphasizes the role of the transport layer in data delivery, including port addressing and connection types (TCP and UDP), while mentioning various network utility tools for monitoring. Overall, it highlights the importance of understanding these layers for effective network troubleshooting and management.
This document provides an introduction to socket programming in C on Linux. Some key points:
- Sockets allow communication between processes over a network and act as an interface between applications and the network. They are represented by file descriptors.
- There are two main types of sockets - stream sockets (TCP) which provide reliable, ordered data transmission and datagram sockets (UDP) which are unreliable but do not require a connection.
- The socket API includes functions like socket(), bind(), listen(), accept(), connect(), recv(), send(), etc. to create and manage sockets and network communication.
- Data structures like sockaddr_in are used to store socket addresses containing IP and port. Byte ordering functions like
The document describes a LAN chat server that allows for chatting, voice chat, file transfer, and a bulletin board between clients on a local network. It uses sockets and networking concepts in Java to allow a server to communicate with multiple clients simultaneously through separate connections on the same port. The server forms sockets to clients, receives and forwards data between clients in loops, and uses streams to transfer various types of data between the client and server communication channels.
chapter-4-networking hjgjjgj did hfhhfhjAmitDeshai
This document provides an overview of networking concepts including client-server computing, networking basics, ports, sockets, TCP, UDP, proxy servers, internet addressing, and Java networking APIs. Some key points:
- A client-server model involves a client machine making requests to a server machine that provides a shared resource. Common server types include web, print, file, and compute servers.
- Network communication uses TCP or UDP protocols over IP addresses and port numbers to direct data between applications on different devices.
- Sockets provide an endpoint for inter-process communication and are identified by an IP address and port number combination.
- Java supports networking through classes like InetAddress, ServerSocket, Socket,
The document provides a comprehensive overview of Java network programming, emphasizing networking basics, client-server models, and the use of TCP/IP with Java sockets. It covers key topics such as DNS, ports, and the different protocols (TCP and UDP) that facilitate communication over the internet, along with practical examples of implementing servers and clients in Java. Additionally, it details Java networking classes and methods for handling URLs and web connections, highlighting the importance of networking in modern software development.
The document discusses computer networks and network protocols. It begins with an introduction to network protocols and the Internet protocols. It then provides definitions and explanations of communication protocols, including addressing, transmission modes, and error detection/recovery techniques. It lists and describes common network protocols like TCP/IP, routing protocols, FTP, SMTP, and more. It also discusses the OSI model layers, TCP/IP protocol suite, data encapsulation, protocol data units, protocol assignments to layers, and addresses at each layer.
CCNA (R & S) Module 01 - Introduction to Networks - Chapter 9Waqas Ahmed Nawaz
The document outlines the transport layer's role in managing data communication between applications, focusing on TCP and UDP protocols. It details the characteristics of both protocols, including TCP's reliability and flow control versus UDP's simplicity and lower overhead. It also explains how connection establishment, termination, and data ordering are managed, along with details on port numbers and socket pairs.
The document provides an introduction to socket programming, outlining the definition and types of sockets, along with client-server communication methodologies. It describes connection-oriented (TCP) and connectionless (UDP) protocols, detailing their characteristics and use cases, particularly in data transmission. Additionally, it covers the concepts of ports and addresses necessary for establishing communication between processes on different machines.
The document discusses Java network programming, covering topics such as networking basics, TCP/UDP protocols, client-server model, and the elements necessary for internet-enabled applications. It outlines the use of Java classes for networking, including sockets, URLs, and URL connections, while providing examples and explanations of how to implement client-server communication. The document also highlights key concepts like ports, the domain name system, and the importance of reliable versus connectionless communication in network applications.
Byte ordering refers to the arrangement of bytes when data is transmitted over a network. There are two common forms of byte ordering - big endian and little endian. Special functions like htons() and htonl() are used to convert between host byte ordering and network byte ordering when communicating between machines.
TCP and UDP are transport layer protocols that provide communication between applications on different hosts. TCP is a connection-oriented protocol that provides reliable, ordered delivery of streams of bytes. UDP is a connectionless protocol that provides best-effort delivery of datagrams but has less overhead than TCP. Both protocols use port numbers and socket APIs for processes to communicate.
The document provides an overview of the TCP/IP protocol stack, including its layers and core protocols. It discusses the four layers of TCP/IP - network interface, internet, transport and application layers. Key protocols like IP, TCP, UDP, ARP are explained along with their functions. The relationship between TCP/IP and OSI models is covered. The use of ports, common port numbers, and risks of open ports are also summarized.
This document provides an overview and introduction to network theory and Java programming. It discusses key topics like network communication models (OSI and TCP/IP), protocols, ports, sockets, firewalls, proxies, and an overview of Java. The document also provides code samples for basic Java socket programming including using ServerSocket for servers and Socket for clients. It explains concepts like connection-oriented and connectionless sockets in UDP and TCP. The objective is to help readers understand network environments and be able to develop basic networking applications in Java.
This document provides an overview of the TCP/IP protocol stack. It discusses the four layers of TCP/IP - network interface, internet, transport and application layer - and how they relate to the seven-layer OSI model. Key protocols of each TCP/IP layer like IP, TCP, UDP, HTTP, FTP, SMTP are explained along with their functions. Other topics covered include ports, port scanning, and Windows sockets API.
This document provides an overview of the TCP/IP protocol stack. It discusses the four layers of TCP/IP - network interface, internet, transport and application layer - and how they relate to the seven-layer OSI model. Key protocols at each layer like IP, TCP, UDP, HTTP, and FTP are explained. The roles of ports, port scanning, and APIs are also summarized.
The document discusses TCP/IP and its transport and internet layers. It describes how TCP/IP provides reliable, connection-oriented communication using mechanisms like three-way handshakes, windowing, acknowledgments and retransmissions. It also discusses UDP, a connectionless protocol that does not provide reliability. Well-known ports allow clients to identify server applications.
This document provides an overview of the transport layer and protocols TCP and UDP. It discusses how the transport layer provides communication between processes on different hosts using port numbers for multiplexing. TCP provides reliable, connection-oriented transport using mechanisms like flow control, congestion control, and reliable data transfer with sequence numbers and acknowledgments. UDP provides a simpler, connectionless datagram service without reliability. The document outlines TCP and UDP header formats and explains TCP connection establishment and closing procedures. It also describes TCP congestion control algorithms like slow start, congestion avoidance, fast retransmit, and timeouts for loss recovery.
The document is a chapter about the transport layer from an introduction to networks textbook. It discusses the purpose of the transport layer in managing end-to-end data transportation. It describes the TCP and UDP protocols, how they establish connections, ensure reliability, and multiplex communications. TCP provides reliable, ordered data streams using acknowledgments while UDP is simpler but unreliable. Applications like video benefit from UDP's low overhead whereas databases require TCP's reliability.
The document outlines the course content for a Small Office Home Office (SOHO) IT Network Setup course. The course covers topics such as network components, configurations, email and file sharing setup. It includes chapters on understanding networks, network components, terminologies, a SOHO network lab, and advanced Google search operators. Network abbreviations and concepts such as IP addressing, static versus dynamic IP, and private versus public IP are also defined.
The Internet protocol suite, commonly known as TCP/IP, is a framework for org...MathivananP4
The document provides an overview of TCP/IP, a set of protocols that enable computers to communicate over a network, defining both transport and network layers. It discusses the structure of the TCP/IP model, various protocols such as SMTP, FTP, and their functions, as well as addressing, routing, and domain name systems. Key concepts include connection-oriented and connectionless communication, port multiplexing, and the use of ARP for address resolution.
BitRecover OST to PST Converter is a powerful tool designed to convert inaccessible or orphaned OST files into Outlook PST format. It ensures complete data recovery including emails, contacts, calendars, and attachments. The software supports both ANSI and Unicode OST files and works with all Outlook versions. With a user-friendly interface and advanced filtering options, it's ideal for professionals needing quick and accurate OST to PST conversion.
https://www.bitrecover.com/ost-to-pst/
This document provides an introduction to socket programming in C on Linux. Some key points:
- Sockets allow communication between processes over a network and act as an interface between applications and the network. They are represented by file descriptors.
- There are two main types of sockets - stream sockets (TCP) which provide reliable, ordered data transmission and datagram sockets (UDP) which are unreliable but do not require a connection.
- The socket API includes functions like socket(), bind(), listen(), accept(), connect(), recv(), send(), etc. to create and manage sockets and network communication.
- Data structures like sockaddr_in are used to store socket addresses containing IP and port. Byte ordering functions like
The document describes a LAN chat server that allows for chatting, voice chat, file transfer, and a bulletin board between clients on a local network. It uses sockets and networking concepts in Java to allow a server to communicate with multiple clients simultaneously through separate connections on the same port. The server forms sockets to clients, receives and forwards data between clients in loops, and uses streams to transfer various types of data between the client and server communication channels.
chapter-4-networking hjgjjgj did hfhhfhjAmitDeshai
This document provides an overview of networking concepts including client-server computing, networking basics, ports, sockets, TCP, UDP, proxy servers, internet addressing, and Java networking APIs. Some key points:
- A client-server model involves a client machine making requests to a server machine that provides a shared resource. Common server types include web, print, file, and compute servers.
- Network communication uses TCP or UDP protocols over IP addresses and port numbers to direct data between applications on different devices.
- Sockets provide an endpoint for inter-process communication and are identified by an IP address and port number combination.
- Java supports networking through classes like InetAddress, ServerSocket, Socket,
The document provides a comprehensive overview of Java network programming, emphasizing networking basics, client-server models, and the use of TCP/IP with Java sockets. It covers key topics such as DNS, ports, and the different protocols (TCP and UDP) that facilitate communication over the internet, along with practical examples of implementing servers and clients in Java. Additionally, it details Java networking classes and methods for handling URLs and web connections, highlighting the importance of networking in modern software development.
The document discusses computer networks and network protocols. It begins with an introduction to network protocols and the Internet protocols. It then provides definitions and explanations of communication protocols, including addressing, transmission modes, and error detection/recovery techniques. It lists and describes common network protocols like TCP/IP, routing protocols, FTP, SMTP, and more. It also discusses the OSI model layers, TCP/IP protocol suite, data encapsulation, protocol data units, protocol assignments to layers, and addresses at each layer.
CCNA (R & S) Module 01 - Introduction to Networks - Chapter 9Waqas Ahmed Nawaz
The document outlines the transport layer's role in managing data communication between applications, focusing on TCP and UDP protocols. It details the characteristics of both protocols, including TCP's reliability and flow control versus UDP's simplicity and lower overhead. It also explains how connection establishment, termination, and data ordering are managed, along with details on port numbers and socket pairs.
The document provides an introduction to socket programming, outlining the definition and types of sockets, along with client-server communication methodologies. It describes connection-oriented (TCP) and connectionless (UDP) protocols, detailing their characteristics and use cases, particularly in data transmission. Additionally, it covers the concepts of ports and addresses necessary for establishing communication between processes on different machines.
The document discusses Java network programming, covering topics such as networking basics, TCP/UDP protocols, client-server model, and the elements necessary for internet-enabled applications. It outlines the use of Java classes for networking, including sockets, URLs, and URL connections, while providing examples and explanations of how to implement client-server communication. The document also highlights key concepts like ports, the domain name system, and the importance of reliable versus connectionless communication in network applications.
Byte ordering refers to the arrangement of bytes when data is transmitted over a network. There are two common forms of byte ordering - big endian and little endian. Special functions like htons() and htonl() are used to convert between host byte ordering and network byte ordering when communicating between machines.
TCP and UDP are transport layer protocols that provide communication between applications on different hosts. TCP is a connection-oriented protocol that provides reliable, ordered delivery of streams of bytes. UDP is a connectionless protocol that provides best-effort delivery of datagrams but has less overhead than TCP. Both protocols use port numbers and socket APIs for processes to communicate.
The document provides an overview of the TCP/IP protocol stack, including its layers and core protocols. It discusses the four layers of TCP/IP - network interface, internet, transport and application layers. Key protocols like IP, TCP, UDP, ARP are explained along with their functions. The relationship between TCP/IP and OSI models is covered. The use of ports, common port numbers, and risks of open ports are also summarized.
This document provides an overview and introduction to network theory and Java programming. It discusses key topics like network communication models (OSI and TCP/IP), protocols, ports, sockets, firewalls, proxies, and an overview of Java. The document also provides code samples for basic Java socket programming including using ServerSocket for servers and Socket for clients. It explains concepts like connection-oriented and connectionless sockets in UDP and TCP. The objective is to help readers understand network environments and be able to develop basic networking applications in Java.
This document provides an overview of the TCP/IP protocol stack. It discusses the four layers of TCP/IP - network interface, internet, transport and application layer - and how they relate to the seven-layer OSI model. Key protocols of each TCP/IP layer like IP, TCP, UDP, HTTP, FTP, SMTP are explained along with their functions. Other topics covered include ports, port scanning, and Windows sockets API.
This document provides an overview of the TCP/IP protocol stack. It discusses the four layers of TCP/IP - network interface, internet, transport and application layer - and how they relate to the seven-layer OSI model. Key protocols at each layer like IP, TCP, UDP, HTTP, and FTP are explained. The roles of ports, port scanning, and APIs are also summarized.
The document discusses TCP/IP and its transport and internet layers. It describes how TCP/IP provides reliable, connection-oriented communication using mechanisms like three-way handshakes, windowing, acknowledgments and retransmissions. It also discusses UDP, a connectionless protocol that does not provide reliability. Well-known ports allow clients to identify server applications.
This document provides an overview of the transport layer and protocols TCP and UDP. It discusses how the transport layer provides communication between processes on different hosts using port numbers for multiplexing. TCP provides reliable, connection-oriented transport using mechanisms like flow control, congestion control, and reliable data transfer with sequence numbers and acknowledgments. UDP provides a simpler, connectionless datagram service without reliability. The document outlines TCP and UDP header formats and explains TCP connection establishment and closing procedures. It also describes TCP congestion control algorithms like slow start, congestion avoidance, fast retransmit, and timeouts for loss recovery.
The document is a chapter about the transport layer from an introduction to networks textbook. It discusses the purpose of the transport layer in managing end-to-end data transportation. It describes the TCP and UDP protocols, how they establish connections, ensure reliability, and multiplex communications. TCP provides reliable, ordered data streams using acknowledgments while UDP is simpler but unreliable. Applications like video benefit from UDP's low overhead whereas databases require TCP's reliability.
The document outlines the course content for a Small Office Home Office (SOHO) IT Network Setup course. The course covers topics such as network components, configurations, email and file sharing setup. It includes chapters on understanding networks, network components, terminologies, a SOHO network lab, and advanced Google search operators. Network abbreviations and concepts such as IP addressing, static versus dynamic IP, and private versus public IP are also defined.
The Internet protocol suite, commonly known as TCP/IP, is a framework for org...MathivananP4
The document provides an overview of TCP/IP, a set of protocols that enable computers to communicate over a network, defining both transport and network layers. It discusses the structure of the TCP/IP model, various protocols such as SMTP, FTP, and their functions, as well as addressing, routing, and domain name systems. Key concepts include connection-oriented and connectionless communication, port multiplexing, and the use of ARP for address resolution.
BitRecover OST to PST Converter is a powerful tool designed to convert inaccessible or orphaned OST files into Outlook PST format. It ensures complete data recovery including emails, contacts, calendars, and attachments. The software supports both ANSI and Unicode OST files and works with all Outlook versions. With a user-friendly interface and advanced filtering options, it's ideal for professionals needing quick and accurate OST to PST conversion.
https://www.bitrecover.com/ost-to-pst/
Slides: Eco Economic Epochs for The World Game (s) pdfSteven McGee
The REAL QFS - DeFi Quantum Financial System structured data AI symbols exchange derived from Net Centric Warfare, Battlefield Digitization for the World Game (s) Great Reset - Redesign: "End state = "in the beginning state" "Start at end state & work backwards" FutureMan
#quantum #financial #system #QFS #patent #Cryptocurrency #Tariff #TradeFi #commodity #wars #patent #law #AI artificial Intelligence Github: https://github.com/Beacon-Heart
Transmission Control Protocol (TCP) and StarlinkAPNIC
Geoff Huston, APNIC Chief Scientist, delivered a remote presentation on 'TCP and Starlink' at INNOG 8 held in conjunction with the 2nd India ISP Conclave held in Delhi, India from 23 to 28 May 2025.
DDoS in India, presented at INNOG 8 by Dave PhelanAPNIC
Dave Phelan, Senior Network Analyst & Technical Trainer at APNIC, presented on 'DDoS in India' at INNOG 8 held in conjunction with the 2nd India ISP Conclave held in Delhi, India from 23 to 28 May 2025.
1:1原版澳洲澳大利亚国立大学毕业证文凭【Q微:1954 292 140】鉴于此,ANUdiploma澳大利亚国立大学挂科处理解决方案ANU毕业证成绩单专业服务学历认证【Q微:1954 292 140】一比一还原国外大学毕业证,定制国外大学学历,制作国外大学文凭,复刻国外大学毕业证书。学位证1:1完美还原海外各大学毕业材料上的工艺:水印,阴影底纹,钢印LOGO烫金烫银,LOGO烫金烫银复合重叠。文字图案浮雕、激光镭射、紫外荧光、温感、复印防伪等防伪工艺。
【澳大利亚国立大学成绩单一站式办理专业技术完美呈现The Australian National University Transcripts】
购买日韩成绩单、英国大学成绩单、美国大学成绩单、澳洲大学成绩单、加拿大大学成绩单(q微1954292140)新加坡大学成绩单、新西兰大学成绩单、爱尔兰成绩单、西班牙成绩单、德国成绩单。成绩单的意义主要体现在证明学习能力、评估学术背景、展示综合素质、提高录取率,以及是作为留信认证申请材料的一部分。
澳大利亚国立大学成绩单能够体现您的的学习能力,包括澳大利亚国立大学课程成绩、专业能力、研究能力。(q微1954292140)具体来说,成绩报告单通常包含学生的学习技能与习惯、各科成绩以及老师评语等部分,因此,成绩单不仅是学生学术能力的证明,也是评估学生是否适合某个教育项目的重要依据!
国外留学无法毕业的留学生(即没有获得毕业证书和学位证书的情况下)想要在中国进行学历认证,通常不能通过正常的学历认证流程进行认证,因为正常的学历认证需要提供有效的毕业证书和学位证书,以及其他相关的学业文件。
在这种情况下,留学生可以选择通过留学服务中心的留信认证来尝试认证他们的学习经历。具体来说,留信认证是针对没有获得正规学位或毕业证书的学生,通过提供学习经历、课程成绩等材料,进行学业经历的认证。
Paper: The World Game (s) Great Redesign.pdfSteven McGee
Paper: The REAL QFS - DeFi Quantum Financial System structured data AI symbols exchange derived from Net Centric Warfare, Battlefield Digitization for the World Game (s) Great Reset - Redesign: "End state = "in the beginning state" "Start at end state & work backwards" FutureMan
3. Lecture 3: 9-4-01 3
Applications and Application-Layer
Protocols
• Application: communicating,
distributed processes
• Running in network hosts in
“user space”
• Exchange messages to
implement app
• e.g., email, file transfer, the
Web
• Application-layer protocols
• One “piece” of an app
• Define messages exchanged
by apps and actions taken
• User services provided by
lower layer protocols
application
transport
network
data link
physical
application
transport
network
data link
physical
application
transport
network
data link
physical
4. Lecture 3: 9-4-01 4
Client-Server Paradigm
Typical network app has two pieces: client and server
application
transport
network
data link
physical
application
transport
network
data link
physical
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
request
reply
5. Lecture 3: 9-4-01 5
Ftp: The File Transfer Protocol
• Transfer file to/from remote host
• Client/server model
• Client: side that initiates transfer (either to/from remote)
• Server: remote host
• ftp: RFC 959
• ftp server: port 21
file transfer
FTP
server
FTP
user
interface
FTP
client
local file
system
remote file
system
user
at host
6. Lecture 3: 9-4-01 6
Ftp: Separate Control, Data
Connections
• Ftp client contacts ftp server
at port 21, specifying TCP
as transport protocol
• Two parallel TCP
connections opened:
• Control: exchange commands,
responses between client,
server.
“out of band control”
• Data: file data to/from server
• Ftp server maintains “state”:
current directory, earlier
authentication
FTP
client
FTP
server
TCP control connection
port 21
TCP data connection
port 20
7. Lecture 3: 9-4-01 7
Ftp Commands, Responses
Sample Commands:
• sent as ASCII text over
control channel
• USER username
• PASS password
• LIST return list of files in
current directory
• RETR filename
retrieves (gets) file
• STOR filename stores
(puts) file onto remote host
Sample Return Codes
• status code and phrase
• 331 Username OK,
password required
• 125 data connection
already open;
transfer starting
• 425 Can’t open data
connection
• 452 Error writing
file
8. Lecture 3: 9-4-01 8
What Transport Service Does an
Application Need?
Data loss
• Some apps (e.g., audio) can
tolerate some loss
• Other apps (e.g., file transfer,
telnet) require 100% reliable
data transfer
Timing
• Some apps (e.g., Internet
telephony, interactive
games) require low delay to
be “effective”
Bandwidth
• Some apps (e.g., multimedia) require minimum amount of
bandwidth to be “effective”
• Other apps (“elastic apps”) make use of whatever bandwidth they
get
9. Lecture 3: 9-4-01 9
Transport Service Requirements
of Common Apps
no loss
no loss
no loss
loss-tolerant
loss-tolerant
loss-tolerant
no loss
elastic
elastic
elastic
audio: 5Kb-1Mb
video:10Kb-5Mb
same as above
few Kbps
elastic
no
no
no
yes, 100’s msec
yes, few secs
yes, 100’s msec
yes and no
file transfer
e-mail
web documents
real-time audio/
video
stored audio/video
interactive games
financial apps
Application Data loss Bandwidth Time Sensitive
11. Lecture 3: 9-4-01 11
Server and Client
TCP/UDP
IP
Ethernet Adapter
Server
TCP/UDP
IP
Ethernet Adapter
Clients
Server and Client exchange messages over the
network through a common Socket API
Socket API
hardware
kernel
space
user
space
ports
12. Lecture 3: 9-4-01 12
User Datagram Protocol(UDP):
An Analogy
Postal Mail
• Single mailbox to receive
messages
• Unreliable
• Not necessarily in-order
delivery
• Each letter is independent
• Must address each reply
Example UDP applications
Multimedia, voice over IP
UDP
• Single socket to receive
messages
• No guarantee of delivery
• Not necessarily in-order
delivery
• Datagram – independent
packets
• Must address each packet
Postal Mail
• Single mailbox to receive
letters
• Unreliable
• Not necessarily in-order
delivery
• Letters sent independently
• Must address each reply
13. Lecture 3: 9-4-01 13
Transmission Control Protocol
(TCP): An Analogy
TCP
• Reliable – guarantee
delivery
• Byte stream – in-order
delivery
• Connection-oriented –
single socket per
connection
• Setup connection
followed by data transfer
Telephone Call
• Guaranteed delivery
• In-order delivery
• Connection-oriented
• Setup connection
followed by conversation
Example TCP applications
Web, Email, Telnet
14. Lecture 3: 9-4-01 14
Network Addressing Analogy
412-268-8000
ext.123
Central Number
Applications/Servers
Web
Port 80
Mail
Port 25
Exchange
Area Code
412-268-8000
ext.654
IP Address
Network No.
Host Number
Telephone No
15-441 Students Clients
Professors at CMU
Network Programming
Telephone Call
Port No.
Extension
15. Lecture 3: 9-4-01 15
Concept of Port Numbers
• Port numbers are used to identify
“entities” on a host
• Port numbers can be
• Well-known (port 0-1023)
• Dynamic or private (port 1024-65535)
• Servers/daemons usually use well-
known ports
• Any client can identify the server/service
• HTTP = 80, FTP = 21, Telnet = 23, ...
• /etc/service defines well-known ports
• Clients usually use dynamic ports
• Assigned by the kernel at run time
TCP/UDP
IP
Ethernet Adapter
NTP
daemon
Web
server
port 123 port 80
16. Lecture 3: 9-4-01 16
Names and Addresses
• Each attachment point on Internet is given
unique address
• Based on location within network – like phone
numbers
• Humans prefer to deal with names not
addresses
• DNS provides mapping of name to address
• Name based on administrative ownership of
host
17. Lecture 3: 9-4-01 17
#include <netinet/in.h>
/* Internet address structure */
struct in_addr {
u_long s_addr; /* 32-bit IPv4 address */
}; /* network byte ordered */
/* Socket address, Internet style. */
struct sockaddr_in {
u_char sin_family; /* Address Family */
u_short sin_port; /* UDP or TCP Port# */
/* network byte ordered */
struct in_addr sin_addr; /* Internet Address */
char sin_zero[8]; /* unused */
};
Internet Addressing Data Structure
• sin_family = AF_INET selects Internet address family
19. Lecture 3: 9-4-01 19
Byte Ordering Functions
• Converts between host byte order and network
byte order
• ‘h’ = host byte order
• ‘n’ = network byte order
• ‘l’ = long (4 bytes), converts IP addresses
• ‘s’ = short (2 bytes), converts port numbers
#include <netinet/in.h>
unsigned long int htonl(unsigned long int hostlong);
unsigned short int htons(unsigned short int
hostshort);
unsigned long int ntohl(unsigned long int netlong);
unsigned short int ntohs(unsigned short int
netshort);
21. Lecture 3: 9-4-01 21
• A socket is a file descriptor that lets an application read/write data
from/to the network
• socket returns an integer (socket descriptor)
• fd < 0 indicates that an error occurred
• socket descriptors are similar to file descriptors
• AF_INET: associates a socket with the Internet protocol family
• SOCK_STREAM: selects the TCP protocol
• SOCK_DGRAM: selects the UDP protocol
What is a Socket?
int fd; /* socket descriptor */
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) }
perror(“socket”);
exit(1);
}
22. Lecture 3: 9-4-01 22
TCP
IP
Ethernet Adapter
Web Server
Port 80
• For example: web
server
• What does a web server
need to do so that a web
client can connect to it?
TCP Server
23. Lecture 3: 9-4-01 23
• Since web traffic uses TCP, the web server must create a socket of type
SOCK_STREAM
int fd; /* socket descriptor */
if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror(“socket”);
exit(1);
}
• socket returns an integer (socket descriptor)
• fd < 0 indicates that an error occurred
• AF_INET associates a socket with the Internet protocol family
• SOCK_STREAM selects the TCP protocol
Socket I/O: socket()
24. Lecture 3: 9-4-01 24
• A socket can be bound to a port
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by bind() */
/* create the socket */
srv.sin_family = AF_INET; /* use the Internet addr family */
srv.sin_port = htons(80); /* bind socket ‘fd’ to port 80*/
/* bind: a client may connect to any of my addresses */
srv.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(fd, (struct sockaddr*) &srv, sizeof(srv)) < 0) {
perror("bind"); exit(1);
}
• Still not quite ready to communicate with a client...
Socket I/O: bind()
25. Lecture 3: 9-4-01 25
Socket I/O: listen()
• listen indicates that the server will accept a connection
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by bind() */
/* 1) create the socket */
/* 2) bind the socket to a port */
if(listen(fd, 5) < 0) {
perror(“listen”);
exit(1);
}
• Still not quite ready to communicate with a client...
26. Lecture 3: 9-4-01 26
Socket I/O: accept()
• accept blocks waiting for a connection
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by bind() */
struct sockaddr_in cli; /* used by accept() */
int newfd; /* returned by accept() */
int cli_len = sizeof(cli); /* used by accept() */
/* 1) create the socket */
/* 2) bind the socket to a port */
/* 3) listen on the socket */
newfd = accept(fd, (struct sockaddr*) &cli, &cli_len);
if(newfd < 0) {
perror("accept"); exit(1);
}
• accept returns a new socket (newfd) with the same properties as the
original socket (fd)
• newfd < 0 indicates that an error occurred
27. Lecture 3: 9-4-01 27
Socket I/O: accept() continued...
struct sockaddr_in cli; /* used by accept() */
int newfd; /* returned by accept() */
int cli_len = sizeof(cli); /* used by accept() */
newfd = accept(fd, (struct sockaddr*) &cli, &cli_len);
if(newfd < 0) {
perror("accept");
exit(1);
}
• How does the server know which client it is?
• cli.sin_addr.s_addr contains the client’s IP address
• cli.sin_port contains the client’s port number
• Now the server can exchange data with the client by
using read and write on the descriptor newfd.
• Why does accept need to return a new descriptor?
28. Lecture 3: 9-4-01 28
Socket I/O: read()
• read can be used with a socket
• read blocks waiting for data from the client but
does not guarantee that sizeof(buf) is read
int fd; /* socket descriptor */
char buf[512]; /* used by read() */
int nbytes; /* used by read() */
/* 1) create the socket */
/* 2) bind the socket to a port */
/* 3) listen on the socket */
/* 4) accept the incoming connection */
if((nbytes = read(newfd, buf, sizeof(buf))) < 0) {
perror(“read”); exit(1);
}
29. Lecture 3: 9-4-01 29
TCP
IP
Ethernet Adapter
2 Web Clients
TCP Client
• For example: web
client
• How does a web client
connect to a web server?
30. Lecture 3: 9-4-01 30
struct sockaddr_in srv;
srv.sin_addr.s_addr = inet_addr(“128.2.35.50”);
if(srv.sin_addr.s_addr == (in_addr_t) -1) {
fprintf(stderr, "inet_addr failed!n"); exit(1);
}
Converting a numerical address to a string:
Dealing with IP Addresses
• IP Addresses are commonly written as strings (“128.2.35.50”), but
programs deal with IP addresses as integers.
struct sockaddr_in srv;
char *t = inet_ntoa(srv.sin_addr);
if(t == 0) {
fprintf(stderr, “inet_ntoa failed!n”); exit(1);
}
Converting strings to numerical address:
31. Lecture 3: 9-4-01 31
Translating Names to Addresses
• Gethostbyname provides interface to DNS
• Additional useful calls
• Gethostbyaddr – returns hostent given sockaddr_in
• Getservbyname
• Used to get service description (typically port number)
• Returns servent based on name
#include <netdb.h>
struct hostent *hp; /*ptr to host info for remote*/
struct sockaddr_in peeraddr;
char *name = “www.cs.cmu.edu”;
peeraddr.sin_family = AF_INET;
hp = gethostbyname(name)
peeraddr.sin_addr.s_addr = ((struct in_addr*)(hp->h_addr))->s_addr;
32. Lecture 3: 9-4-01 32
Socket I/O: connect()
• connect allows a client to connect to a server...
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by connect() */
/* create the socket */
/* connect: use the Internet address family */
srv.sin_family = AF_INET;
/* connect: socket ‘fd’ to port 80 */
srv.sin_port = htons(80);
/* connect: connect to IP Address “128.2.35.50” */
srv.sin_addr.s_addr = inet_addr(“128.2.35.50”);
if(connect(fd, (struct sockaddr*) &srv, sizeof(srv)) < 0) {
perror(”connect"); exit(1);
}
33. Lecture 3: 9-4-01 33
Socket I/O: write()
• write can be used with a socket
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by connect() */
char buf[512]; /* used by write() */
int nbytes; /* used by write() */
/* 1) create the socket */
/* 2) connect() to the server */
/* Example: A client could “write” a request to a server
*/
if((nbytes = write(fd, buf, sizeof(buf))) < 0) {
perror(“write”);
exit(1);
}
35. Lecture 3: 9-4-01 35
UDP
IP
Ethernet Adapter
NTP
daemon
UDP Server Example
Port 123
• For example: NTP
daemon
• What does a UDP server
need to do so that a UDP
client can connect to it?
36. Lecture 3: 9-4-01 36
Socket I/O: socket()
• The UDP server must create a datagram socket…
int fd; /* socket descriptor */
if((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror(“socket”);
exit(1);
}
• socket returns an integer (socket descriptor)
• fd < 0 indicates that an error occurred
• AF_INET: associates a socket with the Internet protocol family
• SOCK_DGRAM: selects the UDP protocol
37. Lecture 3: 9-4-01 37
Socket I/O: bind()
• A socket can be bound to a port
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by bind() */
/* create the socket */
/* bind: use the Internet address family */
srv.sin_family = AF_INET;
/* bind: socket ‘fd’ to port 80*/
srv.sin_port = htons(80);
/* bind: a client may connect to any of my addresses */
srv.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(fd, (struct sockaddr*) &srv, sizeof(srv)) < 0) {
perror("bind"); exit(1);
}
• Now the UDP server is ready to accept packets…
38. Lecture 3: 9-4-01 38
Socket I/O: recvfrom()
• read does not provide the client’s address to the UDP server
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by bind() */
struct sockaddr_in cli; /* used by recvfrom() */
char buf[512]; /* used by recvfrom() */
int cli_len = sizeof(cli); /* used by recvfrom() */
int nbytes; /* used by recvfrom() */
/* 1) create the socket */
/* 2) bind to the socket */
nbytes = recvfrom(fd, buf, sizeof(buf), 0 /* flags */,
(struct sockaddr*) &cli, &cli_len);
if(nbytes < 0) {
perror(“recvfrom”); exit(1);
}
39. Lecture 3: 9-4-01 39
Socket I/O: recvfrom() continued...
nbytes = recvfrom(fd, buf, sizeof(buf), 0 /* flags */,
(struct sockaddr*) cli, &cli_len);
• The actions performed by recvfrom
• returns the number of bytes read (nbytes)
• copies nbytes of data into buf
• returns the address of the client (cli)
• returns the length of cli (cli_len)
• don’t worry about flags
40. Lecture 3: 9-4-01 40
TCP
IP
Ethernet Adapter
2 UDP Clients
UDP Client Example
ports
• How does a UDP client
communicate with a UDP
server?
41. Lecture 3: 9-4-01 41
Socket I/O: sendto()
• write is not allowed
• Notice that the UDP client does not bind a port number
• a port number is dynamically assigned when the first sendto is called
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by sendto() */
/* 1) create the socket */
/* sendto: send data to IP Address “128.2.35.50” port 80 */
srv.sin_family = AF_INET;
srv.sin_port = htons(80);
srv.sin_addr.s_addr = inet_addr(“128.2.35.50”);
nbytes = sendto(fd, buf, sizeof(buf), 0 /* flags */,
(struct sockaddr*) &srv, sizeof(srv));
if(nbytes < 0) {
perror(“sendto”); exit(1);
}
42. Lecture 3: 9-4-01 42
Review: UDP Client-Server
Interaction
socket()
bind()
recvfrom()
sendto()
UDP Server
socket()
UDP Client
sendto()
recvfrom()
close()
blocks until datagram
received from a client
data request
data reply
from UNIX Network Programming Volume 1, figure 8.1
43. Lecture 3: 9-4-01 43
UDP
IP
Ethernet Adapter
UDP Server
The UDP Server
Port 2000
Port 3000
• How can the UDP server
service multiple ports
simultaneously?
44. Lecture 3: 9-4-01 44
int s1; /* socket descriptor 1 */
int s2; /* socket descriptor 2 */
/* 1) create socket s1 */
/* 2) create socket s2 */
/* 3) bind s1 to port 2000 */
/* 4) bind s2 to port 3000 */
while(1) {
recvfrom(s1, buf, sizeof(buf), ...);
/* process buf */
recvfrom(s2, buf, sizeof(buf), ...);
/* process buf */
}
UDP Server: Servicing Two Ports
• What problems does this code have?
45. Lecture 3: 9-4-01 45
Socket I/O: select()
int select(int maxfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
FD_CLR(int fd, fd_set *fds); /* clear the bit for fd in fds */
FD_ISSET(int fd, fd_set *fds); /* is the bit for fd in fds? */
FD_SET(int fd, fd_set *fds); /* turn on the bit for fd in fds */
FD_ZERO(fd_set *fds); /* clear all bits in fds */
• maxfds: number of descriptors to be tested
• descriptors (0, 1, ... maxfds-1) will be tested
• readfds: a set of fds we want to check if data is available
• returns a set of fds ready to read
• if input argument is NULL, not interested in that condition
• writefds: returns a set of fds ready to write
• exceptfds: returns a set of fds with exception conditions
46. Lecture 3: 9-4-01 46
Socket I/O: select()
int select(int maxfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
struct timeval {
long tv_sec; /* seconds /
long tv_usec; /* microseconds */
}
• timeout
• if NULL, wait forever and return only when one of the descriptors is
ready for I/O
• otherwise, wait up to a fixed amount of time specified by timeout
• if we don’t want to wait at all, create a timeout structure with timer value
equal to 0
• Refer to the man page for more information
47. Lecture 3: 9-4-01 47
int s1, s2; /* socket descriptors */
fd_set readfds; /* used by select() */
/* create and bind s1 and s2 */
while(1) {
FD_ZERO(&readfds); /* initialize the fd set
*/
FD_SET(s1, &readfds); /* add s1 to the fd set */
FD_SET(s2, &readfds); /* add s2 to the fd set */
if(select(s2+1, &readfds, 0, 0, 0) < 0) {
perror(“select”);
exit(1);
}
if(FD_ISSET(s1, &readfds)) {
recvfrom(s1, buf, sizeof(buf), ...);
/* process buf */
}
/* do the same for s2 */
}
Socket I/O: select()
• select allows synchronous I/O multiplexing
48. Lecture 3: 9-4-01 48
TCP
IP
Ethernet Adapter
Web Server
Port 80
How can a a web server manage
multiple connections simultaneously?
Port 8001
More Details About a Web Server
49. Lecture 3: 9-4-01 49
int fd, next=0; /* original socket */
int newfd[10]; /* new socket descriptors */
while(1) {
fd_set readfds;
FD_ZERO(&readfds); FD_SET(fd, &readfds);
/* Now use FD_SET to initialize other newfd’s
that have already been returned by accept() */
select(maxfd+1, &readfds, 0, 0, 0);
if(FD_ISSET(fd, &readfds)) {
newfd[next++] = accept(fd, ...);
}
/* do the following for each descriptor newfd[n] */
if(FD_ISSET(newfd[n], &readfds)) {
read(newfd[n], buf, sizeof(buf));
/* process data */
}
}
Socket I/O: select()
• Now the web server can support multiple connections...