Transcript Socket

IT355: Internet Technologies & Programming
Chapter 01: Introduction
Main Topics
1. Course Overview
2. Introduction
3. Socket Programming Using Java
• Overview of Java networking
• Addressing and naming
• TCP programming
• UDP programming.
• Java threads and multithreaded programming
• Distributed objects and remote method invocation (RMI)
4. Client Side Programming
• HTML 5
• CSS 3
• JavaScript
5. Server Side Programming
• Java Database (JDBC - JPA)
• Java Servlet
• Java Server Pages - JSP
• Java Server Faces - JSF
• Enterprise Java Beans - EJB
What is Network Programming?
• Network programs: Programs that use network in some
way to do their work.
• Send data across a network
• Provide services over a network
• Receive data over a network
• Invoke services across a network
• Network programming is the discipline of designing and
implementing network programs that communicate across
networks.
The Key Players
• Server
• a program that provides data and/or services to other programs
• a computer that manages a network resource
• file server, Web server, database server, mail server...
• Client
• a program that relies on another program for some of its data or
services
• Web browser, email client, ...
• Protocol
• an agreed-upon way of exchanging info and service requests
between clients and servers
Why Network Programming?
• Sharing of information
• stock quote, airline schedule, ...
• Parallel and distributed computing
• Application services
• client-server applications, Ecommerce, chat room, multiplayer
network games, ...
• Collaborative computing
• desktop conferencing, webcast, ...
• peer-to-peer applications
Java Networking
• Java supports network programming from the earliest
release in a platform-independent way. (“the language of
the Internet”)
• New Java networking APIs provide high-level object
models and function libraries that hide most of the lowlevel details
Java Networking Facilities
• Multithreading
• I/O streams & utilities
• TCP/IP networking
• Multicasting
• Distributed objects and remote method invocation
• Internet & Web access
• Security policies
• Common Object Request Broker Architecture - CORBA
support
Network Communication
• Most modern networks are packet-switched networks.
• A protocol is a precise set of rules and data format
defining how computers communicate.
• Network communication is layered
• Each layer represents a different level of abstraction.
TCP/IP Suite
Creating a Network Application
• write programs that
• run on (different) end systems
• communicate over network
• e.g., web server software
communicates with browser software
application
transport
network
data link
physical
• No need to write software for
network-core devices
• Network-core devices do not run user
applications
application
transport
network
data link
physical
application
transport
network
data link
physical
Network Application Architectures
• Client-server
• Peer-to-peer (P2P)
• Hybrid of client-server and P2P
Client-Server Architecture
• server:
• always-on host
• permanent IP address
• server farms for scaling
• clients:
• communicate with server
• may be intermittently connected
• may have dynamic IP addresses
• do not communicate directly with
each other
client/server
Servers
• In a client/server network, the server runs a service, or
process, sometimes called a server daemon.
• Like most services, daemons typically run in the
background and are not under an end user's direct
control.
• When a daemon "hears" a request from a client, it
exchanges appropriate messages with the client, as
required by its protocol, and proceeds to send the
requested data to the client in the proper format
Pure P2P Architecture
• no always-on server
• arbitrary end systems directly
communicate
peer-peer
• peers are intermittently
connected and change IP
addresses
• Highly scalable but difficult to
manage
Hybrid of Client-Server and P2P
• Skype
• voice-over-IP P2P application
• centralized server: finding address of remote party:
• client-client connection: direct (not through server)
• Instant messaging
• chatting between two users is P2P
• centralized service: client presence detection/location
• user registers its IP address with central server when it comes online
• user contacts central server to find IP addresses of buddies
Processes Communication
• Process: program running
• Client process: process
within a host.
• within same host, two
processes communicate
using inter-process
communication (defined
by OS).
• processes in different
hosts communicate by
exchanging messages
that initiates
communication
• Server process: process
that waits to be
contacted
• Note: applications with
P2P architectures have
client processes & server
processes
Sockets
• process sends/receives
messages to/from its socket
• socket analogous to door
•
•
sending process shoves
message out door
sending process relies on
transport infrastructure on
other side of door which
brings message to socket at
receiving process
host or
server
host or
server
process
controlled by
app developer
socket
socket
TCP with
buffers,
variables
Internet
controlled
by OS
• API Application Programming Interface:
1. choice of transport protocol;
2. ability to fix a few parameters
process
TCP with
buffers,
variables
Socket
• A socket is one of the most fundamental technologies of
computer network programming. Sockets allow network
software applications to communicate using standard
mechanisms built into network hardware and operating
systems.
• A socket represents a single connection between exactly
two pieces of software (a so-called point-topoint connection). More than two pieces of software can
communicate in client/server or distributed systems by
using multiple sockets.
Sockets
Addressing Processes
• To receive messages, process must have
identifier
• Host device has unique 32-bit IP address
Q: does IP address of host suffice for identifying
the process?
A: No, many processes can be running on same
host
Addressing Processes
• identifier includes both IP address and port numbers
associated with process on host.
• Example port numbers:
• HTTP server: 80
• Mail server: 25
• To send HTTP message to web server:
• IP address: 128.119.245.12
• Port number: 80
Application Layer Protocol Defines
• Types of messages
exchanged,
• e.g., request, response
• Message syntax:
• what fields in messages
& how fields are
delineated
• Message semantics
• meaning of information
in fields
• Rules for when and how
processes send & respond
to messages
• Public-domain protocols:
• defined in RFCs
• allows for
interoperability
• e.g., HTTP, SMTP
• Proprietary protocols:
• Do not necessarily
confirm to RFCs
• e.g., Skype
Internet Transport Protocols Services
• TCP service:
•
•
•
•
•
connection-oriented: setup
required between client
and server processes
reliable transport between
sending and receiving
process
flow control: sender won’t
overwhelm receiver
congestion control: throttle
sender when network
overloaded
does not provide: security
• UDP service:
• unreliable data transfer
between sending and
receiving process
• does not provide:
connection setup,
reliability, flow control,
congestion control, or
security
Socket Programming
• Goal: learn how to build client/server application that
communicate using sockets.
• Socket: a host-local, application-created, OS-controlled
interface (a “door”) into which application process can
both send and receive messages to/from another
application process
• Socket API
• introduced in BSD4.1 UNIX, 1981
• client/server paradigm
• two types of transport service via socket API:
• unreliable datagram
• reliable, byte stream-oriented
Socket-Programming Using TCP
• Socket: a door between application process and end-endtransport protocol (UCP or TCP)
• TCP service: reliable transfer of bytes from one process to
another
controlled by
application
developer
controlled by
operating
system
process
process
socket
TCP with
buffers,
variables
host or
server
internet
socket
TCP with
buffers,
variables
host or
server
controlled by
application
developer
controlled by
operating
system
Socket-Programming Using TCP
• Client must contact server
 server process must first be
running
 server must have created
socket (door) that welcomes
client’s contact
• Client contacts server by:
 creating client-local TCP
socket
 specifying IP address, port
number of server process
 When client creates socket:
client TCP establishes
connection to server TCP
 When contacted by client,
server TCP creates new
socket for server process to
communicate with client

allows server to talk with
multiple clients
 source port numbers used to
distinguish clients
application viewpoint
TCP provides reliable, in-order
transfer of bytes (“pipe”)
between client and server
Client/Server Socket Interaction: TCP
Client/Server Socket Interaction: TCP
Stream
• A stream is a sequence of
characters that flow into or
out of a process.
• An input stream is attached to
some input source for the
process, e.g., keyboard or
socket.
• An output stream is attached
to an output source, e.g.,
monitor or socket.
Socket-Programming Using TCP
• Example client-server app:
1. client reads line from standard input (inFromUser stream) ,
sends to server via socket (outToServer stream)
2. server reads line from socket
3. server converts line to uppercase, sends back to client
4. client reads, prints modified line from socket (inFromServer
stream)
Socket Programming using UDP
• UDP: no “connection”
between client and server
 no handshaking
 sender explicitly attaches IP
address and port of
destination to each packet
 server must extract IP
address, port of sender from
received packet
• UDP: transmitted data may
be received out of order, or
lost
application viewpoint
UDP provides unreliable transfer
of groups of bytes (“datagrams”)
between client and server
Client/Server Socket Interaction: TCP
Example: Java (UDP)
HTML5, CSS3, JavaScript
HTML5
• HTML (HyperText Markup Language) is a special type of
computer language called a markup language designed to
specify the content and structure of web pages (also
called documents) in a portable manner.
• HTML5 is the emerging version of HTML.
• HTML enables you to create content that will render
appropriately across the extraordinary range of devices
connected to the Internet—including smartphones, tablet
computers, notebook computers, desktop computers,
special-purpose devices such as large-screen displays at
concert arenas and sports stadiums, and more.
HTML5, CSS3, JavaScript
• A “stricter” version of HTML called XHTML (Extensible
HyperText Markup Language), which is based on XML
(eXtensible Markup Language), is still used frequently
today.
• Many of the server-side technologies we cover later
produce web pages as XHTML documents, by default, but
the trend is clearly to HTML5.
HTML5, CSS3, JavaScript
Cascading Style Sheets (CSS)
• Although HTML5 provides some capabilities for controlling a
•
•
•
•
document’s presentation, it’s better not to mix presentation with
content.
Cascading Style Sheets (CSS) are used to specify the presentation,
or styling, of elements on a web page (e.g., fonts, spacing, sizes,
colors, positioning).
CSS was designed to style portable web pages independently of their
content and structure.
By separating page styling from page content and structure, you can
easily change the look and feel of the pages on an entire website, or
a portion of a website, simply by swapping out one style sheet for
another.
CSS3 is the current version of CSS under development.
HTML5, CSS3, JavaScript
JavaScript
• JavaScript helps you build dynamic web pages (i.e., pages that can
•
•
•
•
be modified “on the fly” in response to events, such as user input,
time changes and more) and computer applications.
It enables you to do the client-side programming of web applications.
JavaScript was created by Netscape.
ECMAScript 5, the latest version of the standard.
JavaScript is a portable scripting language. Programs written in
JavaScript can run in web browsers across a wide range of devices.
Multitier Application Architecture
• Web-based applications are often multitier applications
(sometimes referred to as n-tier applications) that divide
functionality into separate tiers (i.e., logical groupings of
functionality).
• The following figure presents the basic structure of a
three-tier web-based application.
Multitier Application Architecture (cont.)
• The bottom tier (also called the data tier or the information tier)
•
•
•
•
•
•
maintains the application’s data.
This tier typically stores data in a relational database management
system (RDBMS).
The middle tier implements business logic, controller logic and
presentation logic to control interactions between the application’s
clients and its data.
The middle tier acts as an intermediary between data in the
information tier and the application’s clients.
The middle-tier controller logic processes client requests (such as
requests to view a product catalog) and retrieves data from the
database.
The middle-tier presentation logic then processes data from the
information tier and presents the content to the client.
Web applications typically present data to clients as HTML
documents.
Multitier Application Architecture (cont.)
• Business logic in the middle tier enforces business rules
•
•
•
•
•
and ensures that data is reliable before the application
updates a database or presents data to users.
Business rules dictate how clients access data and how
applications process data.
The top tier, or client tier, is the application’s user
interface, which gathers input and displays output.
Users interact directly with the application through the
user interface, which is typically a web browser or a
mobile device.
In response to user actions (e.g., clicking a hyperlink), the
client tier interacts with the middle tier to make requests
and to retrieve data from the information tier.
The client tier then displays the data retrieved for the user.
Client-Side Scripting versus Server-Side Scripting
• Client-side scripting with JavaScript can be used to validate user
•
•
•
•
•
input, to interact with the browser, to enhance web pages, and to add
client/server communication between a browser and a web server.
Client-side scripting does have limitations, such as browser
dependency; the browser or scripting host must support the scripting
language and capabilities.
Scripts are restricted from arbitrarily accessing the local hardware and
file system for security reasons.
Another issue is that client-side scripts can be viewed by the client by
using the browser’s source-viewing capability.
Sensitive information, such as passwords or other personally
identifiable data, should not be on the client.
All client-side data validation should be mirrored on the server. Also,
placing certain operations in JavaScript on the client can open web
applications to security issues.
Client-Side Scripting versus Server-Side Scripting
• Programmers have more flexibility with server-side scripts, which often
generate custom responses for clients.
• For example, a client might connect to an airline’s web server and
request a list of flights from Boston to San Francisco between April 19
and May 5. The server queries the database, dynamically generates an
HTML document containing the flight list and sends the document to the
client. This technology allows clients to obtain the most current flight
information from the database by connecting to an airline’s web server.
• Server-side scripting languages have a wider range of programmatic
capabilities than their client-side equivalents.
• Server-side scripts also have access to server-side software that extends
server functionality—Microsoft web servers use ISAPI (Internet Server
Application Program Interface) extensions and Apache HTTP Servers
use modules.
Client-Side Scripting versus Server-Side Scripting
Summary
• What is Network Programming?
• Why is Network Programming?
• Java Networking
• Client / Server Architecture
• Java Sockets
• Socket Programming
• Socket-Programming Using TCP
• Socket-Programming Using UDP
• Multitier Application Architecture
Reading
• Chapter 1
Next Chapter
• Chapter 2: Starting Network Programming in Java