SlideShare a Scribd company logo
ACTIVE LEARNING
ASSIGNMENT FOR THE
SUBJECT
“ADVANCE JAVA”
SOCKET, SERVER SOCKET,
DATAGRAM SOCKET AND DATAGRAM PACKET
Guided By : -
Ronak Roy
(Assistance prof.)
Prepared By :-
Hemant H. Chetwani
(130410107010 TY CE-II)
What is Socket ?
• A natural or artificial hollow into which something fits or in which
something revolves.
• An electrical device receiving a plug or light bulb to make a connection.
• Means, basically it is used to form a connection or communication between
two or more end-points (ports).
Technically . . .
• A Socket is an endpoint of a two-way communication link between two programs
running on the network.
• Basically there are two types of Sockets :
1) Connection-oriented Sockets. (Based onTCP)
2) Connection-less Sockets. (Based on UDP)
• Java provides a set of classes, defined in a package called java.net, to enable the
development of network applications.
• Key classes in java.net package, simplifying the complexity involved in creating
client and server programs are :
Classes
InetAddress
Socket
ServerSocket
DatagramSocket
DatagramPacket
TCP
UDP
InetAddress class
• Static methods you can use to create new InetAddress objects.
getByName(String host)
getAllByName(String host)
getLocalHost()
InetAddress x = InetAddress.getByName(“gtu.ac.in”);
Throws UnknownHostException
try {
InetAddress a = InetAddress.getByName(hostname);
System.out.println(hostname + ":" +
a.getHostAddress());
} catch (UnknownHostException e) {
System.out.println("No address found for " +
hostname);
}
Sample Code: Lookup.java
• Uses InetAddress class to lookup hostnames found on command line.
java Lookup www.yahoo.com
www.yahoo.com:209.131.36.158
SocketClass
• Corresponds to activeTCP sockets only!
• client sockets
• socket returned by accept();
• Passive sockets are supported by a different class:
• ServerSocket
SocketConstructors
• Constructor creates aTCP connection to a namedTCP server.
• There are a number of constructors:
Socket(InetAddress server, int port);
Socket(InetAddress server, int port,InetAddress local,
int localport);
Socket(String hostname, int port);
Socket Methods
void close();
InetAddress getInetAddress();
InetAddress getLocalAddress();
InputStream getInputStream();
OutputStream getOutputStream();
• Lots more (setting/getting socket options, partial close, etc.)
Socket I/O
• Socket I/O is based on the Java I/O support
• in the package java.io
• InputStream and OutputStream are abstract classes
• common operations defined for all kinds of InputStreams,
OutputStreams.
InputStream Basics
// reads some number of bytes and
// puts in buffer array b
int read(byte[] b);
// reads up to len bytes
int read(byte[] b, int off, int len);
Both methods can throw IOException.
Both return –1 on EOF.
// writes b.length bytes
void write(byte[] b);
// writes len bytes starting
// at offset off
void write(byte[] b, int off, int len);
Both methods can throw IOException.
OutputStream Basics
ServerSocketConstructor
ServerSocket(int port);
ServerSocket(int port, int backlog);
ServerSocket(int port, int backlog,InetAddress
bindAddr);
Socket accept();
void close();
InetAddress getInetAddress();
int getLocalPort();
throw IOException, SecurityException
ServerSocket Methods
Example client-server app:
• Client reads line from standard input,
sends to server via socket.
• server reads line from socket
• server converts line to uppercase,
sends back to client
• client reads, prints modified line from
socket.
outToServer
to network from network
inFromServer
inFromUser
keyboard monitor
Process
clientSocket
input
stream
input
stream
output
stream
TCP
socket
Input stream:
sequence of bytes
into processoutput stream:
sequence of bytes
out of process
Client
process
client TCP
socket
Socket programming with TCP
Client/server socket interaction: TCP
wait for incoming
connection request
connectionSocket =
welcomeSocket.accept()
create socket,
port=x, for
incoming request:
welcomeSocket =
ServerSocket()
create socket,
connect to hostid, port=x
clientSocket =
Socket()
close
connectionSocket
read reply from
clientSocket
close
clientSocket
Server (running on hostid) Client
send request using
clientSocketread request from
connectionSocket
write reply to
connectionSocket
TCP
connection setup
Socket Programming with UDP
• UDP
• Connectionless and unreliable service.
• There isn’t an initial handshaking phase.
• Doesn’t have a pipe.
• transmitted data may be received out of order, or lost
• Socket Programming with UDP
• No need for a welcoming socket.
• No streams are attached to the sockets.
• the sending hosts creates “packets” by attaching the IP destination address and
port number to each batch of bytes.
• The receiving process must unravel to received packet to obtain the packet’s
information bytes.
DatagramSocketConstructors
DatagramSocket();
DatagramSocket(int port);
DatagramSocket(int port, InetAddress a);
All can throw SocketException or SecurityException
Datagram Methods
void connect(InetAddress, int port);
void close();
void receive(DatagramPacket p);
void send(DatagramPacket p);
Lots more!
DatagramPacketConstructors
For receiving:
DatagramPacket( byte[] buf, int len);
For sending:
DatagramPacket( byte[] buf, int len
InetAddress a, int port);
DatagramPacket methods
byte[] getData();
void setData(byte[] buf);
void setAddress(InetAddress a);
void setPort(int port);
InetAddress getAddress();
int getPort();
Example: Java client (UDP)
sendPacketto network from network
receivePacket
inFromUser
keyboard monitor
Process
clientSocket
UDP
packet
input
stream
UDP
packet
UDP
socket
Output: sends
packet (TCP sent
“byte stream”)
Input: receives
packet (TCP
received “byte
stream”)
Client
process
client UDP
socket
Client/server socket interaction: UDP
close
clientSocket
Server (running on hostid)
read reply from
clientSocket
create socket,
clientSocket =
DatagramSocket()
Client
Create, address (hostid, port=x,
send datagram request
using clientSocket
create socket,
port=x, for
incoming request:
serverSocket =
DatagramSocket()
read request from
serverSocket
write reply to
serverSocket
specifying client
host address,
port umber
Socket & Server Socket

More Related Content

PPTX
Network programming using python
PDF
Netty @Apple: Large Scale Deployment/Connectivity
PDF
Lecture10
PDF
Socket Programming using Java
PPTX
Network Socket Programming with JAVA
PDF
IPTABLES_linux_Firewall_Administration (1).pdf
PDF
04 android
PDF
Embedded linux network device driver development
Network programming using python
Netty @Apple: Large Scale Deployment/Connectivity
Lecture10
Socket Programming using Java
Network Socket Programming with JAVA
IPTABLES_linux_Firewall_Administration (1).pdf
04 android
Embedded linux network device driver development

What's hot (20)

PPT
Opentalk at Large - StS 2005
PDF
An Overview of Linux Networking Options
PPT
Interface between kernel and user space
PPTX
Omnet++
PDF
Creating an Arduino Web Server from scratch hardware and software
PDF
Building Topology in NS3
PDF
C# Advanced L08-Networking+WCF
PDF
Asynchronous Io Programming
PPT
Stackless Python In Eve
KEY
Twisted: a quick introduction
PPT
Network programming1
PPTX
Docker Networking
PPT
Pemrograman Jaringan
PDF
Geep networking stack-linuxkernel
PPTX
Docker networking Tutorial 101
PPTX
Java - Sockets
PPT
Improving Robustness In Distributed Systems
PPTX
How to write a Neutron Plugin - if you really need to
PDF
Skydive, real-time network analyzer
PPTX
Training open stack networking -neutron
Opentalk at Large - StS 2005
An Overview of Linux Networking Options
Interface between kernel and user space
Omnet++
Creating an Arduino Web Server from scratch hardware and software
Building Topology in NS3
C# Advanced L08-Networking+WCF
Asynchronous Io Programming
Stackless Python In Eve
Twisted: a quick introduction
Network programming1
Docker Networking
Pemrograman Jaringan
Geep networking stack-linuxkernel
Docker networking Tutorial 101
Java - Sockets
Improving Robustness In Distributed Systems
How to write a Neutron Plugin - if you really need to
Skydive, real-time network analyzer
Training open stack networking -neutron
Ad

Similar to Socket & Server Socket (20)

PPTX
Networking in Java
PPT
Network programming in Java
PPT
Java Socket Programming
PPTX
Java socket programming
PPT
Networking.ppt(client/server, socket) uses in program
PPT
Socket Programming in Java.ppt yeh haii
PPTX
Tcp/ip server sockets
PPTX
5_6278455688045789623.pptx
PDF
28 networking
PPTX
Advance Java-Network Programming
PPTX
PDF
Java networking programs - theory
PPT
Md13 networking
PPT
Network programming in Java
PPTX
Java Network Programming.pptx
PPT
Network Programming in Java
PPTX
Socket programming
PPTX
PPTX
Java socket presentation
PPTX
Javanetworkingbasicssocketsoverview
Networking in Java
Network programming in Java
Java Socket Programming
Java socket programming
Networking.ppt(client/server, socket) uses in program
Socket Programming in Java.ppt yeh haii
Tcp/ip server sockets
5_6278455688045789623.pptx
28 networking
Advance Java-Network Programming
Java networking programs - theory
Md13 networking
Network programming in Java
Java Network Programming.pptx
Network Programming in Java
Socket programming
Java socket presentation
Javanetworkingbasicssocketsoverview
Ad

More from Hemant Chetwani (12)

PPTX
Simulated annealing in n - queens
PPTX
Channel Capacity and transmission media
PPTX
Pseudo Random Number
PPTX
CART – Classification & Regression Trees
PPTX
Types of Compilers
PPTX
Properties and indexers in C#
PPTX
Pumming Lemma
PPTX
Hash table
PPTX
First pass of assembler
PPTX
130410107010 exception handling
PPTX
Counters & time delay
PPTX
Bucket sort
Simulated annealing in n - queens
Channel Capacity and transmission media
Pseudo Random Number
CART – Classification & Regression Trees
Types of Compilers
Properties and indexers in C#
Pumming Lemma
Hash table
First pass of assembler
130410107010 exception handling
Counters & time delay
Bucket sort

Recently uploaded (20)

PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Construction Project Organization Group 2.pptx
PPTX
Artificial Intelligence
PDF
Digital Logic Computer Design lecture notes
PDF
composite construction of structures.pdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPT
introduction to datamining and warehousing
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
additive manufacturing of ss316l using mig welding
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPT
Mechanical Engineering MATERIALS Selection
Foundation to blockchain - A guide to Blockchain Tech
Model Code of Practice - Construction Work - 21102022 .pdf
Construction Project Organization Group 2.pptx
Artificial Intelligence
Digital Logic Computer Design lecture notes
composite construction of structures.pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
bas. eng. economics group 4 presentation 1.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
CYBER-CRIMES AND SECURITY A guide to understanding
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
introduction to datamining and warehousing
UNIT-1 - COAL BASED THERMAL POWER PLANTS
additive manufacturing of ss316l using mig welding
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Automation-in-Manufacturing-Chapter-Introduction.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Mechanical Engineering MATERIALS Selection

Socket & Server Socket

  • 1. ACTIVE LEARNING ASSIGNMENT FOR THE SUBJECT “ADVANCE JAVA” SOCKET, SERVER SOCKET, DATAGRAM SOCKET AND DATAGRAM PACKET Guided By : - Ronak Roy (Assistance prof.) Prepared By :- Hemant H. Chetwani (130410107010 TY CE-II)
  • 2. What is Socket ? • A natural or artificial hollow into which something fits or in which something revolves. • An electrical device receiving a plug or light bulb to make a connection. • Means, basically it is used to form a connection or communication between two or more end-points (ports).
  • 3. Technically . . . • A Socket is an endpoint of a two-way communication link between two programs running on the network. • Basically there are two types of Sockets : 1) Connection-oriented Sockets. (Based onTCP) 2) Connection-less Sockets. (Based on UDP) • Java provides a set of classes, defined in a package called java.net, to enable the development of network applications. • Key classes in java.net package, simplifying the complexity involved in creating client and server programs are :
  • 5. InetAddress class • Static methods you can use to create new InetAddress objects. getByName(String host) getAllByName(String host) getLocalHost() InetAddress x = InetAddress.getByName(“gtu.ac.in”); Throws UnknownHostException
  • 6. try { InetAddress a = InetAddress.getByName(hostname); System.out.println(hostname + ":" + a.getHostAddress()); } catch (UnknownHostException e) { System.out.println("No address found for " + hostname); }
  • 7. Sample Code: Lookup.java • Uses InetAddress class to lookup hostnames found on command line. java Lookup www.yahoo.com www.yahoo.com:209.131.36.158
  • 8. SocketClass • Corresponds to activeTCP sockets only! • client sockets • socket returned by accept(); • Passive sockets are supported by a different class: • ServerSocket
  • 9. SocketConstructors • Constructor creates aTCP connection to a namedTCP server. • There are a number of constructors: Socket(InetAddress server, int port); Socket(InetAddress server, int port,InetAddress local, int localport); Socket(String hostname, int port);
  • 10. Socket Methods void close(); InetAddress getInetAddress(); InetAddress getLocalAddress(); InputStream getInputStream(); OutputStream getOutputStream(); • Lots more (setting/getting socket options, partial close, etc.)
  • 11. Socket I/O • Socket I/O is based on the Java I/O support • in the package java.io • InputStream and OutputStream are abstract classes • common operations defined for all kinds of InputStreams, OutputStreams.
  • 12. InputStream Basics // reads some number of bytes and // puts in buffer array b int read(byte[] b); // reads up to len bytes int read(byte[] b, int off, int len); Both methods can throw IOException. Both return –1 on EOF.
  • 13. // writes b.length bytes void write(byte[] b); // writes len bytes starting // at offset off void write(byte[] b, int off, int len); Both methods can throw IOException. OutputStream Basics
  • 14. ServerSocketConstructor ServerSocket(int port); ServerSocket(int port, int backlog); ServerSocket(int port, int backlog,InetAddress bindAddr);
  • 15. Socket accept(); void close(); InetAddress getInetAddress(); int getLocalPort(); throw IOException, SecurityException ServerSocket Methods
  • 16. Example client-server app: • Client reads line from standard input, sends to server via socket. • server reads line from socket • server converts line to uppercase, sends back to client • client reads, prints modified line from socket. outToServer to network from network inFromServer inFromUser keyboard monitor Process clientSocket input stream input stream output stream TCP socket Input stream: sequence of bytes into processoutput stream: sequence of bytes out of process Client process client TCP socket Socket programming with TCP
  • 17. Client/server socket interaction: TCP wait for incoming connection request connectionSocket = welcomeSocket.accept() create socket, port=x, for incoming request: welcomeSocket = ServerSocket() create socket, connect to hostid, port=x clientSocket = Socket() close connectionSocket read reply from clientSocket close clientSocket Server (running on hostid) Client send request using clientSocketread request from connectionSocket write reply to connectionSocket TCP connection setup
  • 18. Socket Programming with UDP • UDP • Connectionless and unreliable service. • There isn’t an initial handshaking phase. • Doesn’t have a pipe. • transmitted data may be received out of order, or lost • Socket Programming with UDP • No need for a welcoming socket. • No streams are attached to the sockets. • the sending hosts creates “packets” by attaching the IP destination address and port number to each batch of bytes. • The receiving process must unravel to received packet to obtain the packet’s information bytes.
  • 19. DatagramSocketConstructors DatagramSocket(); DatagramSocket(int port); DatagramSocket(int port, InetAddress a); All can throw SocketException or SecurityException
  • 20. Datagram Methods void connect(InetAddress, int port); void close(); void receive(DatagramPacket p); void send(DatagramPacket p); Lots more!
  • 21. DatagramPacketConstructors For receiving: DatagramPacket( byte[] buf, int len); For sending: DatagramPacket( byte[] buf, int len InetAddress a, int port);
  • 22. DatagramPacket methods byte[] getData(); void setData(byte[] buf); void setAddress(InetAddress a); void setPort(int port); InetAddress getAddress(); int getPort();
  • 23. Example: Java client (UDP) sendPacketto network from network receivePacket inFromUser keyboard monitor Process clientSocket UDP packet input stream UDP packet UDP socket Output: sends packet (TCP sent “byte stream”) Input: receives packet (TCP received “byte stream”) Client process client UDP socket
  • 24. Client/server socket interaction: UDP close clientSocket Server (running on hostid) read reply from clientSocket create socket, clientSocket = DatagramSocket() Client Create, address (hostid, port=x, send datagram request using clientSocket create socket, port=x, for incoming request: serverSocket = DatagramSocket() read request from serverSocket write reply to serverSocket specifying client host address, port umber