You are on page 1of 24

Socket Programming(TCP)

Md. Mohaiminul Islam


Lecturer
University of Asia Pacific
OSI Model
7. Application Layer
This is the topmost layer in the seven OSI Layers. This is the layer that
the end-user (can be a computer programmer, or a regular PC user) is
actually interacting with. This layer allows access to network resources.
6. Presentation Layer
This is the layer in which the operating system operates with the data.
Main functions of this layers includes translation, encryption and
compression of data. Basically User interacts with Application layer,
which sends the data down to Presentation layer.
OSI Model
5. Session Layer
This layer has the job of maintaining proper communication by
establishing, managing and terminating sessions between two
computers. For example, whenever we visit any website, our computer
has to create a session with the web server of that website.
4. Transport Layer
This layer has a very important job. It decides how much information
should be sent at a time. So, when you are communicating with a
website, this layer will decide how much data you can transfer and
receive at a given point of time. Also, this layer provides reliable
process to process message delivery and error recovery.
3. Network Layer
The main job of this layer is to move packets from source to destination and
provide inter-networking. This is the layer that the routers operate on. Since
routers operate at the network level, hence we can say that the IP address is
at the network level.
2. Data Link Layer
This layer is responsible for organising bits into frames and ensuring hop to
hop delivery. This is the layer on which the Switches operate on. Since
routers operate at the network level, hence we can say that the MAC address
resides at the data link layer. All the computers in a specific network get
plugged inito a switch so that they can communicate with each other.
OSI Model
1. Physical Layer
This is the layer on which the real transmission of data bits takes place
through a medium. This layer is, as the name suggests, all the physical
stuff that connects the computers together.
OSI Model
TCP/IP Model
InetAddress Class
This class represents an Internet Protocol (IP) address.
Methods:
• static InetAddress getByAddress(byte[] addr)
Returns an InetAddress object given the raw IP address.

• static InetAddress getByName(String host)


Determines the IP address of a host, given the host's name.
InetAddress Class
• String getHostName()
Gets the host name for this IP address.
• getLocalHost()
Sample Code: Lookup.java
• Uses InetAddress class to lookup hostnames found on
command line.

> java Lookup cse.unr.edu www.yahoo.com


cse.unr.edu:134.197.40.9
www.yahoo.com:209.131.36.158

Java Socket Programming 10


try {

InetAddress a = InetAddress.getByName(hostname);

System.out.println(hostname + ":" +
a.getHostAddress());

} catch (UnknownHostException e) {

System.out.println("No address found for " +


hostname);

Java Socket Programming


Transport Layer
• TCP (Transmission Control Protocol): TCP is connection oriented. This
means that TCP tracks all data sent, requiring acknowledgment for
each of them.
• UDP(User Datagram Protocol): UDP is a simple transport-layer
protocol. It is connectionless protocol. UDP does not use
acknowledgments , and is usually used for protocols where a few lost
datagrams do not matter.
Introduction to Sockets
• A socket is one endpoint of a two-way communication link between
two programs running on the network.
• An interface through which processes can send/receive information
• Used for interprocess communication
Client Server Model
• Most interprocess communication uses client server model
• Client and server are two processes that want to communicate with
each other
• Client process requests to the server for connection
• Once the connection is established they can send and receive
information
Socket Connection
• An endpoint is a combination of an IP address and a port number.
• <ip address,Port#>
• What makes a connection?
{Source<IP address, Port #> , Destination <IP address, Port #>}
i.e. source socket – destination socket pair uniquely identifies a
connection.
TCP Socket Programming
Generic TCP Algorithm
• Algorithm for TCP client
– Find the IP address and port number of server
– Create a TCP socket
– Connect the socket to server (Server must be up and listening for new requests)
– Send/ receive data with server using the socket
– Close the connection
• Algorithm for TCP server
– Find the IP address and port number of server
– Create a TCP server socket
– Bind the server socket to server IP and Port number (this is the port to which clients will
connect)
– Accept a new connection from client
– returns a client socket that represents the client which is connected
– Send/ receive data with client using the client socket
– Close the connection with client
ServerSocket
The java.net.ServerSocket class is used by server applications to obtain
a port and listen for client requests.
• Constructors:
ServerSocket(int port);

ServerSocket(int port, int backlog);

ServerSocket(int port, int backlog,


InetAddress bindAddr);
ServerSocket
• Methods:
Socket accept();

void close();

InetAddress getInetAddress();

int getLocalPort();
Socket
• The java.net.Socket class represents the socket that both the client
and the server use to communicate with each other. The client
obtains a Socket object by instantiating one, whereas the server
obtains a Socket object from the return value of the accept() method.

• Constructor:
Socket(InetAddress server, int port);
Socket(String hostname, int port);
Socket
• Methoids:
void close();
InetAddress getInetAddress();
InetAddress getLocalAddress();
InputStream getInputStream();
OutputStream getOutputStream();
Socket
• ObjectInputStream(InputStream in)
• ObjectOutputStream(OutputStream out)
Materials
https://drive.google.com/open?id=1GCybxaiTzHdNew44eCUNp7WzCv-
gtx_p
Thank You

You might also like