Upload
tamimi-haji-tamby
View
39
Download
3
Embed Size (px)
DESCRIPTION
JAVA Socket Programming using TCP (Part 2)
Citation preview
Chapter 5 – Part 2
Java Socket Programming overTCP
Topics
• Transport protocol: – TCP
• Socket Programming– TCP
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
TCP: Transmission Control Protocol
• TCP provides:
– Connection-oriented
– Reliable
– Full-duplex
– Byte-Stream
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.
Connection-Oriented vs Connectionless
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.
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).
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
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).
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
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.
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
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
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
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.
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
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
Sockets operations
• Socket communication over TCP can be grouped into 2 categories:– Client side (active socket)– Server side (passive socket)
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
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
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.
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
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
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
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
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
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.
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.