socket programming using tcp (part 2)

29
Chapter 5 – Part 2 Java Socket Programming over TCP

Upload: tamimi-haji-tamby

Post on 16-Apr-2015

35 views

Category:

Documents


3 download

DESCRIPTION

JAVA Socket Programming using TCP (Part 2)

TRANSCRIPT

Page 1: Socket Programming using TCP (Part 2)

Chapter 5 – Part 2

Java Socket Programming overTCP

Page 2: Socket Programming using TCP (Part 2)

Topics

• Transport protocol: – TCP

• Socket Programming– TCP

Page 3: Socket Programming using TCP (Part 2)

TCP: Transmission Control Protocol

• TCP is an alternative transport layer protocol over IP.

Application Layer(HTTP, FTP, DNS, etc.)

Transport Layer(TCP, UDP)

Network Layer(IP)

Link and Physical Layer

Page 4: Socket Programming using TCP (Part 2)

TCP: Transmission Control Protocol

• TCP provides:

– Connection-oriented

– Reliable

– Full-duplex

– Byte-Stream

Page 5: Socket Programming using TCP (Part 2)

Connection-Oriented

• Connection oriented means that a virtual connection is established before any user data is transferred.

• If the connection cannot be established - the user program is notified.

• If the connection is ever interrupted - the user program(s) is notified.

Page 6: Socket Programming using TCP (Part 2)

Connection-Oriented vs Connectionless

Page 7: Socket Programming using TCP (Part 2)

Reliable

• Reliable means that every transmission of data is acknowledged by the receiver.

• If the sender does not receive acknowledgement within a specified amount of time, the sender retransmits the data.

Page 8: Socket Programming using TCP (Part 2)

Byte Stream

• Stream means that the connection is treated as a stream of bytes.

• The user application does not need to package data in individual datagrams (as with UDP).

Page 9: Socket Programming using TCP (Part 2)

Full Duplex

• TCP provides transfer in both directions.

• To the application program these appear as 2 unrelated data streams

• E.g. TCP connection between host A and B– Data can flow from A to B– And, at the same time from B to A

Page 10: Socket Programming using TCP (Part 2)

TCP Ports

• Interprosses communication via TCP is achieved with the use of ports (also for UDP).

• UDP ports have no relation to TCP ports (different name spaces).

Page 11: Socket Programming using TCP (Part 2)

Buffering

• TCP is responsible for buffering data and determining when it is time to send a datagram.

• It is possible for an application to tell TCP to send the data it has buffered without waiting for a buffer to fill up.

controlled byoperatingsystem

controlled byoperatingsystem

Page 12: Socket Programming using TCP (Part 2)

Client – Server Paradigm

• In network programming, applications that use sockets are divided into 2 categories:– The client– The server

• Sockets provide an interface for programming networks at the transport layer.

Page 13: Socket Programming using TCP (Part 2)

Client – Server Paradigm (cont.)

• A server – is a piece of software which advertises and then

provides some service on request– Listens for connections and processes requests

• A client – is a piece of software (usually on a different

machine) which makes use of some service– Initiates a connection and sends requests

Page 14: Socket Programming using TCP (Part 2)

Client – Server Paradigm (cont.)

• Client – server communication model is an important theoretical concept that widely used in practical applications

• There also other comm. model like peer-to-peer, in which either party may initiate communication (no rigidly defined role as server or client)

• Client – server model is popular due to its simplicity and widely used in network programming

Page 15: Socket Programming using TCP (Part 2)

What is a Socket?• socket: one end of a 2-way communication

link between two networked programs (and their ports)– a socket is created in each program– a connection is made between the two sockets– the output of one socket is sent to the input of the

other socket and vice-versa

Page 16: Socket Programming using TCP (Part 2)

What is a Socket?

• Sockets provide an interface for programming networks at the transport layer.

• Network communication using Sockets is very much similar to performing file I/O– In fact, socket handle is treated like file handle.– The streams used in file I/O operation are also applicable to

socket-based I/O• Socket-based communication is programming

language independent.– That means, a socket program written in Java language can

also communicate to a program written in Java or non-Java socket program.

Page 17: Socket Programming using TCP (Part 2)

Socket Communication

• A server (program) runs on a specific computer and has a socket that is bound to a specific port.

• The server waits and listens to the socket for a client to make a connection request.

serverClient

Connection requestport

Page 18: Socket Programming using TCP (Part 2)

Socket Communication• If everything goes well, the server accepts the connection.

• Upon acceptance, the server gets a new socket bounds to a different port.

• It needs a new socket (consequently a different port number) so that it can continue to listen to the original socket for connection requests while serving the connected client.

server

ClientConnection

port

port por

t

Page 19: Socket Programming using TCP (Part 2)

Sockets operations

• Socket communication over TCP can be grouped into 2 categories:– Client side (active socket)– Server side (passive socket)

Page 20: Socket Programming using TCP (Part 2)

Sockets operations (cont.)

• TCP active sockets (client) perform a variety of operation:– Establish a connection to a remote host– Send data to a remote host– Receive data from a remote host– Close a connection

Page 21: Socket Programming using TCP (Part 2)

Sockets operations (cont.)

• TCP passive sockets (server) perform a variety of operation:– Bind to a local port– Accept incoming connections from remote hosts– Unbind from a local port

Page 22: Socket Programming using TCP (Part 2)

The Java API for TCP

• Java supports TCP communication with two classes– ServerSocket instances are

used to listen for incoming connections in a server process

– Socket objects are created by ServerSocket’s accept method and are also used by clients to initiate connections

+connect()+close()+getInputStream()+getOutputStream()

java.net.Socket

+accept()+getLocalAddress()+close()

java.net.ServerSocket

See the JDK API for details.

Page 23: Socket Programming using TCP (Part 2)

Java Sockets

ServerSocket(1234)

Socket(“161.139.18.99”, 1234)

Output/write stream

Input/read stream

It can be host_name like “www.utm.my”

Client

Server

Page 24: Socket Programming using TCP (Part 2)

The Java API for TCP

• java.net.ServerSocket implements a socket that is intended for servers to use to listen for incoming connect requests

• Constructors:– ServerSocket( int port ) throws IOException

• Creates a ServerSocket and attempts to bind it to the specified port• If the port argument has value zero, this constructor attempts to

bind the ServerSocket to any available port

– ServerSocket( int port, int numberOfClients ) throws IOException

• Creates a ServerSocket and attempts to bind it to the specified port• Allocates sufficient space to queue specified number of client

sockets

Page 25: Socket Programming using TCP (Part 2)

The Java API for TCP

• java.net.ServerSocket some useful methods: – Socket accept( ) throws IOException

• Listens for a connection to be made and accepts it. • This method returns a Socket instance to which the client is

connected

– close( ) throws IOException• Closes the ServerSocket• Unbind the TCP port and allows other services to use it

– int getLocalPort• Return the port number to which the server socket is bound

Page 26: Socket Programming using TCP (Part 2)

The Java API for TCP

• java.net.Socket implements a socket that is intended for clients use to connect to a server

• Constructors:– Socket( String host, int port ) throws IOException

• Creates a Socket instance that is bound to the destination (host, port) where host is the DNS name of the server’s host

– Socket( InetAddress address, int port ) throws IOException• Creates a Socket instance that is bound to the destination

(address, port) where host is represented by the InetAddress argument

Page 27: Socket Programming using TCP (Part 2)

The Java API for TCP

• java.net.Socket some useful methods:

– InputStream getInputStream() throws IOException• Returns the InputStream associated with the Socket instance

– OutputStream getOutputStream() throws IOException• Returns the OutputStream for the Socket instance

– boolean isConnected( )• Returns true if the Socket is connected to a ServerSocket, false

otherwise

– void close ( ) throws IOException• Closes the socket connection

Page 28: Socket Programming using TCP (Part 2)

Socket Programming with TCP

• Server process must first be running (must have created a socket).

– Recall that TCP is not connectionless (i.e. connection oriented).

• Client contacts the server by creating client-local socket specifying IP address and port number of server process.

– Client TCP establishes connection to server TCP.

Page 29: Socket Programming using TCP (Part 2)

Socket Programming with TCP

• When contacted by client, server TCP creates a new socket for server process to communicate with client.

– Allows server to talk with multiple clients

– Source port numbers used to distinguish clients

• From application viewpoint:

– TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server.