WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus...

24
WECPP 1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter http://www.shu.ac.uk/java/
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    218
  • download

    0

Transcript of WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus...

Page 1: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 1

Java networking

Jim Briggs

based on notes by Amanda Peart

based on Bell & Parr's bonus chapterhttp://www.shu.ac.uk/java/networkprogramming/starthere.html

Page 2: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 2

Aims

• To understand Java networking

• To appreciate socket programming

• Define the client socket

• How to use sockets

• Overview of server sockets

Page 3: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 3

Review Internet basics

• TCP/IP protocol (contra UDP/IP)– ensures reliable transmission of data

• IP address– identifies Internet host (strictly interface)

• Domain name– user-friendly way of identifying hosts– DNS maps names to IP addresses (and vice-versa)

• Port– identifies particular service on a host (e.g. HTTP = 80)

Page 4: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 4

Sockets

• Sockets– abstract input-output devices

– could apply to any sort of device, but most often referred to in the context of networking:

• software mechanism for one program to talk to another

• Two-way connection between two programs– full-duplex link

– more than one socket can be associated with a port

Page 5: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 5

Client and server sockets

• Client socket and server socket need to be programmed in different ways

• Client socket assumes the server is ready to accept connections

• Server needs to listen for connection requests

Page 6: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 6

How is a socket used?

• A new socket is created by a program• The socket attempts to connect to a remote

host • Once the connection has been established,

the local machine and the remote machine send and receive data

• When the transmission is complete, one or both machines closes the connection

Page 7: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 7

Socket Programming

• Java uses ‘class Sockets’ to interface to TCP sockets (import java.net.*)

• The constructor method for the class:• creates a TCP socket

• allows the user to specify a host and port

• attempts to connect to the host

Socket connection = new Socket("www.port.ac.uk", 80);

Socket connection = new Socket(InetAddress, port);

Page 8: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 8

When connection is established...

• The client host port is chosen by the system at run time

• The client port number can be accessed using:connection.getLocalPort()

• A socket is closed when:– The socket is garbage collected

– The program ends

– The program explicitly closes it:connection.close()

Page 9: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 9

Errors and exceptions

• ConnectException– The connection is refused by the remote host. – Host could be busy or there might be no server

on the port

• NoRouteToHostException– The connection has timed out.

Page 10: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 10

Input with sockets

• For input it is most convenient to create a BufferedReader (java.io) as follows:BufferedReader in = new BufferedReader (new

InputStreamReader(socket.getInputStream()));

• This enables readLine to be used for string input. Data ends when null is returned. Example:String line; while ((line = in.readLine()) != null) {

processLine();}

Page 11: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 11

Output with sockets

• For output to a stream it is convenient to create a PrintWriter (java.io) object as follows:PrintWriter out = new

PrintWriter(socket.getOutputStream(), true);

• Use method println to output a line.out.println("string");

• Closing a socket closes all of the associated streams.

Page 12: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 12

Socket crashes

• When a program reads from a socket, the call on read blocks (waits) until the data has been obtained

• But if the remote host crashes, the program is left hanging

• An alternative is to call setSoTimeout(timeout)– Sets the maximum waiting time (in milliseconds)

• When the time expires, an InterruptedException is thrown

Page 13: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 13

Port scanner example

• A port scanner is a program that hunts for working ports on an Internet host– Warning: can be impolite

• PortScanner.java– Checks for unknown host

– Attempts to connect to ports 1 to 256 of specified host

– Ignores ports where no socket can be established

Page 14: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 14

Get server time example

• Connect to server's port 13

• Read a line of text (containing time and date)

• GetRemoteTime.java

• Of course it cannot connect if no service is running

Page 15: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 15

Class Sockets

Description Example

constructorspublic Socket(String host, int port)

creates a new Socket object corresponding to destination host and port

Socket socket = new Socket("www.shu.ac.uk", 80);

public Socket(InetAddress host, int port)

creates a new Socket object corresponding to the InetAddress object and port

Socket socket = new Socket(inetAddress, 80);

Page 16: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 16

Class Sockets

Description Example

Object Methodpublic InetAddress getInetAddress()

returns the InetAddress object corresponding to the host that is, or will be, connected to.

InetAddress iNE = socket.getInetAddress();

public int getPort() returns the port number of the host to which the socket is, or will be, connected.

int port = socket.getPort();

Page 17: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 17

Class Sockets

Description Example

Object Methodpublic int getLocalPort()

returns the local port number.

int port = socket.getLocalPort();

public InputStream getInputStream()

returns an input stream that can be used by the program for input.

InputStream is = socket.getInputStream();

public OutputStream getOutputStream()

returns an output stream that can be used by the program for output.

OutputStream os = socket.getOutputStream();

Page 18: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 18

Class Sockets

Description Example

Object Methodpublic synchronized void close()

disconnects a socket. This also closes the streams associated with the socket

socket.close();

public String toString() returns a string representation of a socket with remote host name, remote IP address, remote port, local port.

String details = socket.toString()

public synchronized void setSoTimeout(int ms)

sets the time-out to the parameter value (in milliseconds)

socket. setSoTimeout(18000);

Page 19: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 19

Sockets and Server Sockets

• Socket:– Implemented by class

socket– A socket object is a

connection to a certain host on a particular port

– Socket objects are used for data transfer

– A Socket object initiates a connection

• Server socket:– Implemented by class

ServerSocket

– A SocketServer object just waits on a particular port for incoming connections from anywhere

– A ServerSocket object waits for connections

Page 20: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 20

The algorithm for a server 1

• Create ServerSocket object on a particular port ServerSocket server = new ServerSocket(12345, 100);

• ServerSocket object listens for any connection attempts Socket connection = server.accept();

• The server is blocked (waits) until a client attempts to connect

• When a client attempts to connect, the server wakes up

Page 21: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 21

The algorithm for a server 2

• When a client connects, method accept returns a new Socket object representing the connection between the server and the client

• That socket object's getInputStream and/or getOutputStream methods are called to obtain streams which can be used to send/receive data

• The server and the client interact using streams according to an agreed protocol

• When finished, the server, the client, or both close the connection

Page 22: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 22

Daytime server example

• DaytimeServer.java

• Listens on port 13

• When a connection is accepted it uses the socket's output stream to write the current date and time

• Then closes the socket and listens for a new connection (forever)

Page 23: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 23

More complex servers

• A server dealing with one connection cannot respond to further connections– Normally, connection requests are queued

• May be better to create a new thread to deal with each connection– Allows main thread to keep listening for new

connections

Page 24: WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter .

WECPP 24

Reading

• Bell and Parr - bonus chapter http://www.shu.ac.uk/java/networkprogramming/starthere.html

• Deitel, chapter 18