Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
SSC - Communication and Networking
SSC - Communication and Networking
Java Socket Programming (I)
Shan He
School for Computational Science
University of Birmingham
Module 06-19321: SSC
SSC - Communication and Networking
Outline
Outline of Topics
Review what we learned
Sockets and Socket-based Communication
TCP/IP Socket Programming in Java
Java eamil application: Socket and JavaMail API
SSC - Communication and Networking
Sockets and Socket-based Communication
Client/Server Communication
I The simplest level of network communication
I Consists of a server, client, and a network for communication
I Client: a computer running a program that makes a request
for services
I Server: a computer running a program that offers requested
services, e.g., processing database queries from one or more
clients
Client Server
Network (TCP/IP)
Request
Results
SSC - Communication and Networking
Sockets and Socket-based Communication
TCP/IP Application layer: Sockets
Transport 
Application
Internet
Network 
Interface
FTP SMTP HTTP IMAP
SSL/TLS ACSII
TCP UDP
IP
Physical
ProtocolsTCP/IP model
Sockets
SSC - Communication and Networking
Sockets and Socket-based Communication
TCP/IP model: Application layer - Sockets
I The lowest level before Transport layer
I Definition: “endpoint of a two-way communication link”.
I Provides an interface for programming network
communication.
I Similar to performing file I/O, e.g., socket handle is treated
like file handle.
I Independent of a programming language
SSC - Communication and Networking
Sockets and Socket-based Communication
Sockets
I A socket consists of
I Local socket address: Local IP address and service port number
I Remote socket address: Only for established TCP sockets
I Protocol: A transport protocol, e.g., TCP or UDP.
I A socket address is the combination of an IP address (phone
number) and service port number (extension).
I A socket API is an application programming interface (API),
usually provided by the operating system.
SSC - Communication and Networking
Sockets and Socket-based Communication
Service ports
I Computers often communicate and provide more than one
type of service or to talk to multiple hosts/computers at a
time
I Ports are used to distinguish these services
I Each service offered by a computer is identified by a port
number
I Represented as a positive (16-bit) integer value
I Some ports have been reserved to support common services
I FTP: 21/TCP
I HTTP: 80/TCP,UDP
I User-level process/services use port number value > 1024
SSC - Communication and Networking
Sockets and Socket-based Communication
Establish socket connection
Service Port
Server Client
Service Port 
Server Client
Service Port 
Connection request
Connection
SSC - Communication and Networking
Sockets and Socket-based Communication
Establish socket connection
I Step 1: The server listens to the socket for a client to make a
connection request
I Step 2: the server accepts the connection if everything goes
well
I Step 3: the server gets a new socket bound to a different port
I Go to Step 1
SSC - Communication and Networking
Sockets and Socket-based Communication
Put them together
I Client is connected to a server at the physical address
identified by a IP address.
I Connection binds a port number to a socket number.
I Both TCP and UDP protocols use ports to map incoming
data to a particular process running on a computer.
FTP HTTP SMTP App
Port Port Port Port
TCP or UDP
Port# Data
PacketData
Sockets
Apps
SSC - Communication and Networking
TCP/IP Socket Programming in Java
Java.net class
I Java provides a set of classes, java.net , to enable the
rapid development of network applications.
I Essentially provides classes, interfaces, and exceptions to
simplify the complexity involved in creating client and server
programs
I Two key classes for creating server and client programs
I ServerSocket
I Socket
SSC - Communication and Networking
TCP/IP Socket Programming in Java
Socket-based client and server Java programming
I Server: ServerSocket creates a new service socket (port)
I Clinet: Socket establishes connection with service socket
I Both: exchange data using input and output streams
SSC - Communication and Networking
TCP/IP Socket Programming in Java
Java.net class
ServiceSocket 
1254
Server (IP: 121.2.21.25) Client
Socket((“121.2.21.25”, 1254)
Output/write stream Input/read stream
SSC - Communication and Networking
TCP/IP Socket Programming in Java
A simple Server Program in Java (I)
Steps to create a simple server program:
I Step 1: Open the Server Socket:
ServerSocket server = new ServerSocket( PORT );
I Step 2: Wait for the Client Request:
Socket client = server.accept();
I Step 3: Create I/O streams for communicating to the client
DataInputStream is =
new DataInputStream(client.getInputStream());
DataOutputStream os =
new DataOutputStream(client.getOutputStream());
SSC - Communication and Networking
TCP/IP Socket Programming in Java
A simple Server Program in Java (II)
I Step 4: Perform communication with client
I Receive from client: String line = is.readLine();
I Send to client: os.writeBytes("Hello!");
I Step 5: Close the stream is.close(); os.close();
I Step 6: Close the socket: client.close();
SSC - Communication and Networking
TCP/IP Socket Programming in Java
A simple Client Program in Java
I Step 1: Open a socket.
I Step 2: Open an input stream and output stream to the
socket.
I Step 3: Read from and write to the stream according to the
server’s protocol.
I Step 4: Close the streams.
I Step 5: Close the socket.
SSC - Communication and Networking
TCP/IP Socket Programming in Java
A few explainations: PrintWriter vs
DataOutputStream
I PrintWriter : “Prints formatted representations of objects
to a text-output stream.”
I DataOutputStream : “A data output stream lets an
application write primitive Java data types to an output
stream in a portable way”
I PrintWriter inherited from Writer class, while
DataOutputStream inherited from OutputStream .
I OutputStream (therefore DataOutputStream ) is
designed for binary data, while Writer (therefore
PrintWriter ) is designed for text data
SSC - Communication and Networking
TCP/IP Socket Programming in Java
A few explainations: InetAddress
I InetAddress class: “represents an Internet Protocol (IP)
address.”
I getLocalHost() method: “Returns the address of the
local host.”
SSC - Communication and Networking
TCP/IP Socket Programming in Java
A few explainations: Protocol
I A protocol is the language that the client and server have
agreed to use to communicate.
I Using Java.net class, you can build you own protocol using
socket
I One example: KnockKnockProtocol can be found at here
SSC - Communication and Networking
Java eamil application: Socket and JavaMail API
Socket programming for email
I Please download my source code from here
SSC - Communication and Networking
Java eamil application: Socket and JavaMail API
JavaMail API
I JavaMail API is a set of abstract APIs that model a mail
system.
I A platform independent and protocol independent framework
I A Java optional package of JDK 1.4 and later but part of the
Java Platform, Enterprise Edition (Java EE).
I Click here for JavaMail API FAQ
I A more detailed technical document by Oracle.
SSC - Communication and Networking
Java eamil application: Socket and JavaMail API
JavaMail Achitechture
Java application
JavaMail API (Client Layer)
JavaMail SPI (Server/Protocol)
IMAP SMTP POP3
Message Message Message
SSC - Communication and Networking
Java eamil application: Socket and JavaMail API
Some email protocols/standard
I SMTP: Simple Mail Transfer Protocol, an application-layer
protocol for e-mail transmission over Internet. Uses TCP port
25.
I MIME: Multipurpose Internet Mail Extensions, and standard
to extend the format of email, mainly used with SMTP for
non-text attachments.
I POP3: Post Office Protocol Version 3, an application-layer
protocol used by local e-mail clients to retrieve e-mail from a
remote server over Internet.
I IMAP: Internet message access protocol, an application-layer
protocol for e-mail retrieval.
SSC - Communication and Networking
Java eamil application: Socket and JavaMail API
IMAP vs POP3
I Advantages of IMAP:
I Connected/disconnected modes of operation: clients often stay
connected wither server, resulting in faster response times
I Multiple clients simultaneously connected to the same mailbox
and multiple mailboxes on the server: provide access to shared
and public email account
I Server-side searches: avoids downloading every message to
local mailbox to perform search.
I Message state information: use flags defined in the IMAP
protocol, i.e., read, replied, deleted, to keep track of message
state.
I Disadvantages of IMAP: introduced extra complexity, but
fortunately is handled by the server side.
SSC - Communication and Networking
Java eamil application: Socket and JavaMail API
Using JavaMail: Main steps
I Step 1: configuring a connection to e-mail servers
I Step 1.1: Set mail user properties using Properties object.
I Step 1.2: Establish a mail session using
java.mail.Session .
I Step 2: email operation, e.g., sending/receiving email
SSC - Communication and Networking
Java eamil application: Socket and JavaMail API
Configuring a connection: details
I We need to establish a mail session between the mail client
and the remote mail servers to send or receive messages.
I The first step is to set mail user properties using
Properties object to initiate a connection to mail servers
(Step 1.1)
I The Properties class represents a persistent set of
properties.
I The second step is to use java.mail.Session object to
establish a mail session.
I The java.mail.Session object represents a mail session
which provides access to the protocol providers that
implement the Store, Transport, and related classes
SSC - Communication and Networking
Java eamil application: Socket and JavaMail API
Store and Transport classes
I Store : models a message store and its access protocol, for
storing and retrieving messages. Provides many common
methods for naming stores, connecting to stores, and listening
to connection events.
I Click here for Store Java Doc
I Transport : models a message transport. Provides many
common methods for naming transports, connecting to
transports, and listening to connection events.
I Click here for Transport Java Doc
SSC - Communication and Networking
Java eamil application: Socket and JavaMail API
Creating/Sending email uing JavaMail: step by step
I Step 1: Set certain properties, e.g., mail.smtp.host property
I Step 2: Establish a mail session ( java.mail.Session )
I Step 3: Create a message
I Step 4: Send the message via the javax.mail.Transport
class.
SSC - Communication and Networking
Java eamil application: Socket and JavaMail API
Details about creating/sending message
I Creating message
I Use MimeMessage object.
I MimeMessage class: represents a MIME style email message.
I Usage:
I Step 1: instantiate an empty MimeMessage object
I Step 2: fill it with appropriate attributes and content.
I Sending message
I Use Transport object.
I Transport class: models a message transport
I Usage: Transport.send(message)