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
Ali Nezhad
 
PDF
Netty @Apple: Large Scale Deployment/Connectivity
C4Media
 
PDF
Lecture10
vantinhkhuc
 
PDF
Socket Programming using Java
Rahul Hada
 
PPTX
Network Socket Programming with JAVA
Dudy Ali
 
PDF
IPTABLES_linux_Firewall_Administration (1).pdf
mpassword
 
PDF
04 android
guru472
 
PDF
Embedded linux network device driver development
Amr Ali (ISTQB CTAL Full, CSM, ITIL Foundation)
 
Network programming using python
Ali Nezhad
 
Netty @Apple: Large Scale Deployment/Connectivity
C4Media
 
Lecture10
vantinhkhuc
 
Socket Programming using Java
Rahul Hada
 
Network Socket Programming with JAVA
Dudy Ali
 
IPTABLES_linux_Firewall_Administration (1).pdf
mpassword
 
04 android
guru472
 
Embedded linux network device driver development
Amr Ali (ISTQB CTAL Full, CSM, ITIL Foundation)
 

What's hot (20)

PPT
Opentalk at Large - StS 2005
Martin Kobetic
 
PDF
An Overview of Linux Networking Options
Scott Lowe
 
PPT
Interface between kernel and user space
Susant Sahani
 
PPTX
Omnet++
Ahmed Nour
 
PDF
Creating an Arduino Web Server from scratch hardware and software
Justin Mclean
 
PDF
Building Topology in NS3
Rahul Hada
 
PDF
C# Advanced L08-Networking+WCF
Mohammad Shaker
 
PDF
Asynchronous Io Programming
l xf
 
PPT
Stackless Python In Eve
l xf
 
KEY
Twisted: a quick introduction
Robert Coup
 
PPT
Network programming1
Soham Sengupta
 
PPTX
Docker Networking
Kingston Smiler
 
PPT
Pemrograman Jaringan
belajarkomputer
 
PDF
Geep networking stack-linuxkernel
Kiran Divekar
 
PPTX
Docker networking Tutorial 101
LorisPack Project
 
PPTX
Java - Sockets
Riccardo Cardin
 
PPT
Improving Robustness In Distributed Systems
l xf
 
PPTX
How to write a Neutron Plugin - if you really need to
salv_orlando
 
PDF
Skydive, real-time network analyzer
Sylvain Afchain
 
PPTX
Training open stack networking -neutron
Haifeng Yan (颜海峰)
 
Opentalk at Large - StS 2005
Martin Kobetic
 
An Overview of Linux Networking Options
Scott Lowe
 
Interface between kernel and user space
Susant Sahani
 
Omnet++
Ahmed Nour
 
Creating an Arduino Web Server from scratch hardware and software
Justin Mclean
 
Building Topology in NS3
Rahul Hada
 
C# Advanced L08-Networking+WCF
Mohammad Shaker
 
Asynchronous Io Programming
l xf
 
Stackless Python In Eve
l xf
 
Twisted: a quick introduction
Robert Coup
 
Network programming1
Soham Sengupta
 
Docker Networking
Kingston Smiler
 
Pemrograman Jaringan
belajarkomputer
 
Geep networking stack-linuxkernel
Kiran Divekar
 
Docker networking Tutorial 101
LorisPack Project
 
Java - Sockets
Riccardo Cardin
 
Improving Robustness In Distributed Systems
l xf
 
How to write a Neutron Plugin - if you really need to
salv_orlando
 
Skydive, real-time network analyzer
Sylvain Afchain
 
Training open stack networking -neutron
Haifeng Yan (颜海峰)
 
Ad

Similar to Socket & Server Socket (20)

PPTX
Networking in Java
Tushar B Kute
 
PPT
Network programming in Java
Tushar B Kute
 
PPT
Java Socket Programming
Vipin Yadav
 
PPTX
Java socket programming
Mohammed Abdalla Youssif
 
PPT
Networking.ppt(client/server, socket) uses in program
govindjha339843
 
PPT
Socket Programming in Java.ppt yeh haii
inambscs4508
 
PPTX
Tcp/ip server sockets
rajshreemuthiah
 
PPTX
5_6278455688045789623.pptx
EliasPetros
 
PDF
28 networking
Ravindra Rathore
 
PPTX
Advance Java-Network Programming
ashok hirpara
 
PPTX
Java 1
VidyaVarshini3
 
PDF
Java networking programs - theory
Mukesh Tekwani
 
PPT
Md13 networking
Rakesh Madugula
 
PPT
Network programming in Java
Tushar B Kute
 
PPTX
Java Network Programming.pptx
RoshniSundrani
 
PPT
Network Programming in Java
Tushar B Kute
 
PPTX
Socket programming
Ujjwal Kumar
 
PPTX
A.java
JahnaviBhagat
 
PPTX
Java socket presentation
Zahidul Islam Razu
 
PPTX
Javanetworkingbasicssocketsoverview
rajshreemuthiah
 
Networking in Java
Tushar B Kute
 
Network programming in Java
Tushar B Kute
 
Java Socket Programming
Vipin Yadav
 
Java socket programming
Mohammed Abdalla Youssif
 
Networking.ppt(client/server, socket) uses in program
govindjha339843
 
Socket Programming in Java.ppt yeh haii
inambscs4508
 
Tcp/ip server sockets
rajshreemuthiah
 
5_6278455688045789623.pptx
EliasPetros
 
28 networking
Ravindra Rathore
 
Advance Java-Network Programming
ashok hirpara
 
Java networking programs - theory
Mukesh Tekwani
 
Md13 networking
Rakesh Madugula
 
Network programming in Java
Tushar B Kute
 
Java Network Programming.pptx
RoshniSundrani
 
Network Programming in Java
Tushar B Kute
 
Socket programming
Ujjwal Kumar
 
Java socket presentation
Zahidul Islam Razu
 
Javanetworkingbasicssocketsoverview
rajshreemuthiah
 
Ad

More from Hemant Chetwani (12)

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

Recently uploaded (20)

PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
Software Testing Tools - names and explanation
shruti533256
 
PDF
5 Influence line.pdf for structural engineers
Endalkazene
 
PPTX
Module_II_Data_Science_Project_Management.pptx
anshitanarain
 
PDF
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
PPTX
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
PDF
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
PDF
flutter Launcher Icons, Splash Screens & Fonts
Ahmed Mohamed
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PDF
Activated Carbon for Water and Wastewater Treatment_ Integration of Adsorptio...
EmilianoRodriguezTll
 
PPTX
AgentX UiPath Community Webinar series - Delhi
RohitRadhakrishnan8
 
PDF
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
PDF
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PDF
B.Tech Data Science Program (Industry Integrated ) Syllabus
rvray078
 
PDF
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
PDF
6th International Conference on Artificial Intelligence and Machine Learning ...
gerogepatton
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
dodultrongaming
 
PDF
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
demidovs1
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Software Testing Tools - names and explanation
shruti533256
 
5 Influence line.pdf for structural engineers
Endalkazene
 
Module_II_Data_Science_Project_Management.pptx
anshitanarain
 
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
flutter Launcher Icons, Splash Screens & Fonts
Ahmed Mohamed
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
Activated Carbon for Water and Wastewater Treatment_ Integration of Adsorptio...
EmilianoRodriguezTll
 
AgentX UiPath Community Webinar series - Delhi
RohitRadhakrishnan8
 
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
B.Tech Data Science Program (Industry Integrated ) Syllabus
rvray078
 
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
6th International Conference on Artificial Intelligence and Machine Learning ...
gerogepatton
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
dodultrongaming
 
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
demidovs1
 

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