communication and networking java socket programming (i ...szh/teaching/2013backup/ssc/lecture...i...

29
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

Upload: others

Post on 21-Feb-2021

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

SSC - Communication and Networking

SSC - Communication and NetworkingJava Socket Programming (I)

Shan He

School for Computational ScienceUniversity of Birmingham

Module 06-19321: SSC

Page 2: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

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

Page 3: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

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 requestfor services

I Server: a computer running a program that offers requestedservices, e.g., processing database queries from one or moreclients

Client Server

Network (TCP/IP)

Request

Results

Page 4: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

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

Page 5: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

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 networkcommunication.

I Similar to performing file I/O, e.g., socket handle is treatedlike file handle.

I Independent of a programming language

Page 6: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

SSC - Communication and Networking

Sockets and Socket-based Communication

Sockets

I A socket consists ofI Local socket address: Local IP address and service port numberI Remote socket address: Only for established TCP socketsI Protocol: A transport protocol, e.g., TCP or UDP.

I A socket address is the combination of an IP address (phonenumber) and service port number (extension).

I A socket API is an application programming interface (API),usually provided by the operating system.

Page 7: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

SSC - Communication and Networking

Sockets and Socket-based Communication

Service ports

I Computers often communicate and provide more than onetype of service or to talk to multiple hosts/computers at atime

I Ports are used to distinguish these services

I Each service offered by a computer is identified by a portnumber

I Represented as a positive (16-bit) integer valueI Some ports have been reserved to support common services

I FTP: 21/TCPI HTTP: 80/TCP,UDP

I User-level process/services use port number value > 1024

Page 8: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

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

Page 9: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

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 aconnection request

I Step 2: the server accepts the connection if everything goeswell

I Step 3: the server gets a new socket bound to a different port

I Go to Step 1

Page 10: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

SSC - Communication and Networking

Sockets and Socket-based Communication

Put them together

I Client is connected to a server at the physical addressidentified by a IP address.

I Connection binds a port number to a socket number.

I Both TCP and UDP protocols use ports to map incomingdata to a particular process running on a computer.

FTP HTTP SMTP App

Port Port Port Port

TCP or UDP

Port# Data

PacketData

Sockets

Apps

Page 11: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

SSC - Communication and Networking

TCP/IP Socket Programming in Java

Java.net class

I Java provides a set of classes, java.net , to enable therapid development of network applications.

I Essentially provides classes, interfaces, and exceptions tosimplify the complexity involved in creating client and serverprograms

I Two key classes for creating server and client programsI ServerSocketI Socket

Page 12: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

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

Page 13: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

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

Page 14: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

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 clientDataInputStream is =

new DataInputStream(client.getInputStream());

DataOutputStream os =

new DataOutputStream(client.getOutputStream());

Page 15: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

SSC - Communication and Networking

TCP/IP Socket Programming in Java

A simple Server Program in Java (II)

I Step 4: Perform communication with clientI 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();

Page 16: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

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 thesocket.

I Step 3: Read from and write to the stream according to theserver’s protocol.

I Step 4: Close the streams.

I Step 5: Close the socket.

Page 17: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

SSC - Communication and Networking

TCP/IP Socket Programming in Java

A few explainations: PrintWriter vsDataOutputStream

I PrintWriter : “Prints formatted representations of objectsto a text-output stream.”

I DataOutputStream : “A data output stream lets anapplication write primitive Java data types to an outputstream in a portable way”

I PrintWriter inherited from Writer class, whileDataOutputStream inherited from OutputStream .

I OutputStream (therefore DataOutputStream ) is

designed for binary data, while Writer (thereforePrintWriter ) is designed for text data

Page 18: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

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 thelocal host.”

Page 19: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

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 haveagreed to use to communicate.

I Using Java.net class, you can build you own protocol usingsocket

I One example: KnockKnockProtocol can be found at here

Page 20: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

SSC - Communication and Networking

Java eamil application: Socket and JavaMail API

Socket programming for email

I Please download my source code from here

Page 21: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

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 mailsystem.

I A platform independent and protocol independent framework

I A Java optional package of JDK 1.4 and later but part of theJava Platform, Enterprise Edition (Java EE).

I Click here for JavaMail API FAQ

I A more detailed technical document by Oracle.

Page 22: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

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

Page 23: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

SSC - Communication and Networking

Java eamil application: Socket and JavaMail API

Some email protocols/standard

I SMTP: Simple Mail Transfer Protocol, an application-layerprotocol for e-mail transmission over Internet. Uses TCP port25.

I MIME: Multipurpose Internet Mail Extensions, and standardto extend the format of email, mainly used with SMTP fornon-text attachments.

I POP3: Post Office Protocol Version 3, an application-layerprotocol used by local e-mail clients to retrieve e-mail from aremote server over Internet.

I IMAP: Internet message access protocol, an application-layerprotocol for e-mail retrieval.

Page 24: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

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 timesI Multiple clients simultaneously connected to the same mailbox

and multiple mailboxes on the server: provide access to sharedand public email account

I Server-side searches: avoids downloading every message tolocal mailbox to perform search.

I Message state information: use flags defined in the IMAPprotocol, i.e., read, replied, deleted, to keep track of messagestate.

I Disadvantages of IMAP: introduced extra complexity, butfortunately is handled by the server side.

Page 25: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

SSC - Communication and Networking

Java eamil application: Socket and JavaMail API

Using JavaMail: Main steps

I Step 1: configuring a connection to e-mail serversI 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

Page 26: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

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 clientand the remote mail servers to send or receive messages.

I The first step is to set mail user properties usingProperties object to initiate a connection to mail servers

(Step 1.1)

I The Properties class represents a persistent set ofproperties.

I The second step is to use java.mail.Session object toestablish a mail session.

I The java.mail.Session object represents a mail sessionwhich provides access to the protocol providers thatimplement the Store, Transport, and related classes

Page 27: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

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, forstoring and retrieving messages. Provides many commonmethods for naming stores, connecting to stores, and listeningto connection events.

I Click here for Store Java Doc

I Transport : models a message transport. Provides manycommon methods for naming transports, connecting totransports, and listening to connection events.

I Click here for Transport Java Doc

Page 28: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

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.

Page 29: Communication and Networking Java Socket Programming (I ...szh/teaching/2013backup/ssc/lecture...I Message state information: use ags de ned in the IMAP protocol, i.e., read, replied,

SSC - Communication and Networking

Java eamil application: Socket and JavaMail API

Details about creating/sending message

I Creating messageI Use MimeMessage object.

I MimeMessage class: represents a MIME style email message.I Usage:

I Step 1: instantiate an empty MimeMessage objectI Step 2: fill it with appropriate attributes and content.

I Sending messageI Use Transport object.

I Transport class: models a message transport

I Usage: Transport.send(message)