Top Banner
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
20

Communication and Networking Java Socket ... - cs.bham.ac.uk · TCP Socket Programming in Java Java.net class I Java provides a set of classes, java.net , to enable the rapid development

Aug 30, 2019

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Communication and Networking Java Socket ... - cs.bham.ac.uk · TCP Socket Programming in Java Java.net class I Java provides a set of classes, java.net , to enable the rapid development

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 ... - cs.bham.ac.uk · TCP Socket Programming in Java Java.net class I Java provides a set of classes, java.net , to enable the rapid development

SSC - Communication and Networking

Outline

Outline of Topics

Sockets and Socket-based Communication

TCP Socket Programming in Java

Page 3: Communication and Networking Java Socket ... - cs.bham.ac.uk · TCP Socket Programming in Java Java.net class I Java provides a set of classes, java.net , to enable the rapid development

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 ... - cs.bham.ac.uk · TCP Socket Programming in Java Java.net class I Java provides a set of classes, java.net , to enable the rapid development

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 ... - cs.bham.ac.uk · TCP Socket Programming in Java Java.net class I Java provides a set of classes, java.net , to enable the rapid development

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 ... - cs.bham.ac.uk · TCP Socket Programming in Java Java.net class I Java provides a set of classes, java.net , to enable the rapid development

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 ... - cs.bham.ac.uk · TCP Socket Programming in Java Java.net class I Java provides a set of classes, java.net , to enable the rapid development

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,UDPI IMAP: 143/TCP

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

Page 8: Communication and Networking Java Socket ... - cs.bham.ac.uk · TCP Socket Programming in Java Java.net class I Java provides a set of classes, java.net , to enable the rapid development

SSC - Communication and Networking

Sockets and Socket-based Communication

Establish socket connection

I Step 1: The server listens to the socket of a specific portnumber for a client to make a connection 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(usually random) port for connection with the client

I Go to Step 1

I Question: the service port in the TCP socket connectionrequest is different from the service port for the connection.Why?

Page 9: Communication and Networking Java Socket ... - cs.bham.ac.uk · TCP Socket Programming in Java Java.net class I Java provides a set of classes, java.net , to enable the rapid development

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 10: Communication and Networking Java Socket ... - cs.bham.ac.uk · TCP Socket Programming in Java Java.net class I Java provides a set of classes, java.net , to enable the rapid development

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 ... - cs.bham.ac.uk · TCP Socket Programming in Java Java.net class I Java provides a set of classes, java.net , to enable the rapid development

SSC - Communication and Networking

TCP 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 ... - cs.bham.ac.uk · TCP Socket Programming in Java Java.net class I Java provides a set of classes, java.net , to enable the rapid development

SSC - Communication and Networking

TCP 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 ... - cs.bham.ac.uk · TCP Socket Programming in Java Java.net class I Java provides a set of classes, java.net , to enable the rapid development

SSC - Communication and Networking

TCP 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 ... - cs.bham.ac.uk · TCP Socket Programming in Java Java.net class I Java provides a set of classes, java.net , to enable the rapid development

SSC - Communication and Networking

TCP 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 ... - cs.bham.ac.uk · TCP Socket Programming in Java Java.net class I Java provides a set of classes, java.net , to enable the rapid development

SSC - Communication and Networking

TCP 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 ... - cs.bham.ac.uk · TCP Socket Programming in Java Java.net class I Java provides a set of classes, java.net , to enable the rapid development

SSC - Communication and Networking

TCP 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 ... - cs.bham.ac.uk · TCP Socket Programming in Java Java.net class I Java provides a set of classes, java.net , to enable the rapid development

SSC - Communication and Networking

TCP 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 ... - cs.bham.ac.uk · TCP Socket Programming in Java Java.net class I Java provides a set of classes, java.net , to enable the rapid development

SSC - Communication and Networking

TCP 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 ... - cs.bham.ac.uk · TCP Socket Programming in Java Java.net class I Java provides a set of classes, java.net , to enable the rapid development

SSC - Communication and Networking

TCP 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 ... - cs.bham.ac.uk · TCP Socket Programming in Java Java.net class I Java provides a set of classes, java.net , to enable the rapid development

SSC - Communication and Networking

TCP Socket Programming in Java

Socket programming for email

I POP3, IMAP and SMTP are all email protocols based onTCP socket communication

I Please take a look at my source code of send email using onlysocket