SlideShare a Scribd company logo
B.Kohila
M.Sc(info.tech),
Nadar Saraswathi College of arts and science,
Theni
 The Inetaddress class provides you with a limited interface to
DNS for doing both forward and reverse internet address
lookups
- An InetAddress class method corresponds to a DNS request
 Npublic constructoro
 Three static methods:
 InetAddress getByName(String)
 Static method used to retrieve the address for the host name passed
as the parameter.
 InetAddress [ ] getAllByName(String)
 Static method used to retrieve all the addresses for the host name
passed as a parameter.
 InetAddress getLocalHost( )
 Static method used to retrieve the address for the current, or local,
host.
 Three additional “getter” methods
 String getHostName( )
 Returns the host name.
 byte[ ] getAddress( )
 Returns the IP address.
 String getHostAddress( )
 Returns the IP address as a string.
try
{
InetAddress fullname =
netAddress.getByName(“bigyellowcat.cs.binghamton.edu");
InetAddress alias = InetAddress.getByName(“bigyellowcat");
InetAddress octets = InetAddress.getByName(“128.226.121.44");
if (fullname.equals(alias) && fullname.equals(octets))
// All is right with the world!}
catch (UnknownHostException e)
{ // Exception handling here. }
 Once a TCP socket connection is made, a virtual
stream is in place. Java’s IO model is that of a stream,
therefore the models are consistent; all you need to do
connect a TCP socket to a stream and read and write
the streams as normal
 Socket(String ip, int port)
 Creates a streaming socket and binds it to the host and port specified as
parameters.
 Socket(String ip, int port, boolean TCPorUDP)
 Creates a socket and binds it to the host and port specified as
parameters. The last parameter is used to indicate whether the socket
should be a stream or datagram socket.
 Socket(InetAddress ia, int port)
 Creates a streaming socket connected to the specified host and port.
 Socket(InetAddress ia, int port, boolean TCPorUDP)
 Creates a socket connected to the specified host and port. The last
parameter specifies whether the socket should be a stream or datagram
socket.
 InetAddress getInetAddress( )
 Returns an InetAddress object representing the host for this socket.
 Int getPort( )
 . Returns the port number on the remote host for this socket
 Int getLocalPort( )
 Returns the port number on the local host for this socket.
 InputStream getInputStream( )
 Returns an input stream for the socket.
 OutputStream getOutputStream( )
 Returns an output stream for the socket.
 Close( )
 Closes the socket.
 SetSocketImplFactory (SocketImplFactory)
 Sets the socket factory that will be used to create all sockets.
try
{
Socket socket = new Socket("somehost.somewhere.com", -1);
// Always a good idea to buffer the stream to mitigate blocking.
PrintStream out = new PrintStream( new BufferedOutputStream(socket.getOutputStream()));
out.println("Are you listening?");
DataInputStream in = new DataInputStream( new BufferedInputStream(socket.getInputStream()));
in.readLine();
// ...
// Don't forget to close the socket!
socket.close()
}
catch (Exception e)
// Exception handling logic.
 Since UDP is a connectionless protocol; there is no virtual stream
between the hosts so streams are not used for IO.
 UDP applications are not thought of in terms of clients and servers,
but rather in terms of senders and receivers.
 For conversational applications both ends (sender and receiver) will be
changing states from sender to receiver and back again
 Many UDP based applications are simple send a request then receive the
data (sender’s perspective), like a DNS request. The receiver’s perspective
is to ‘listen’ for a request, send the response, listen for more requests.
 UDP sockets send and receive Datagrams
 Constructors: two for receiving, four for sending
 DatagramPacket( byte[ ] buff , int len)
 Constructs a DatagramPacket for receiving packets of length len.
 DatagramPacket(byte[] buf, int off, int len)
 Constructs a DatagramPacket for receiving packets of length len,
specifying an offset of off bytes into the buffer.
 DatagramPacket((byte[] buf, int len, InetAddress addr, int port)
 Constructs a datagram packet for sending packets of length len to the
specified port number on the specified host.
 DatagramPacker(byte[] buf, int off, int len, InetAddress addr, int port)
 Constructs a datagram packet for sending packets of length len with
offset off to the specified port number on the specified host.
 DatagramPacket(byte[] buf, int off, int len, SocketAddress addr)
 Constructs a datagram packet for sending packets of length len with
offset off to the specified port number on the specified host.
 Constructors
 DatagramSocket()
 Constructs a datagram socket and binds it to any available port on the
local host.
 DatagramSocket(DatagramSocketImpl impl)
 Creates an unbound datagram socket with the specified
DatagramSocketImpl.
 DatagramSocket(int port)
 Constructs a datagram socket and binds it to the specified port on the
local host.
 DatagramSocket(int port, InetAddress iaddr)
 Creates a datagram socket, bound to the specified local address.
 DatagramSocket(SocketAddress bindaddr)
 Creates a datagram socket, bound to the specified local socket address.

More Related Content

PPTX
Namespace1
PDF
Åsted .Net (CSI .Net)
PPT
Sockets
PPTX
Dynamic memory allocation in c++
PPTX
Dynamic Memory allocation
PPTX
Array of pointer
PPTX
Dynamic memory Allocation in c language
Namespace1
Åsted .Net (CSI .Net)
Sockets
Dynamic memory allocation in c++
Dynamic Memory allocation
Array of pointer
Dynamic memory Allocation in c language

What's hot (19)

PDF
20150207 howes-gpgpu8-dark secrets
PDF
Functional Programming and Ruby
PPTX
Programming Assignment Help
PDF
The TCP/IP stack in the FreeBSD kernel COSCUP 2014
PDF
Hacker Thursdays: An introduction to binary exploitation
PPT
Lec2&3 data structure
PPT
Lec2
PPT
Lec2&3_DataStructure
RTF
PPTX
Programming Assignment Help
PPTX
Introduction to Python and TensorFlow
PPTX
C Programming Homework Help
ODP
Nach os network
ODP
Nach os network
ODP
Nach os network
PPTX
Operating System Assignment Help
PPT
Memory allocation in c
ODP
Stackless Python 101
PDF
Lesson11 transactions
20150207 howes-gpgpu8-dark secrets
Functional Programming and Ruby
Programming Assignment Help
The TCP/IP stack in the FreeBSD kernel COSCUP 2014
Hacker Thursdays: An introduction to binary exploitation
Lec2&3 data structure
Lec2
Lec2&3_DataStructure
Programming Assignment Help
Introduction to Python and TensorFlow
C Programming Homework Help
Nach os network
Nach os network
Nach os network
Operating System Assignment Help
Memory allocation in c
Stackless Python 101
Lesson11 transactions
Ad

Similar to Java networking basics & sockets overview (20)

PPTX
Advance Java-Network Programming
PPTX
Networking in Java
PDF
28 networking
PDF
Lecture6
PPT
Udp Programming
PPT
Udp Programming
PPTX
Java seminar.pptx
PDF
Chapter 3 : User Datagram Protocol (UDP)
PPT
Md13 networking
PPTX
5_6278455688045789623.pptx
PPTX
Socket & Server Socket
PPT
UDP-Dept Businesss Computing university .ppt
PPTX
Java socket programming
PPT
Basic Networking in Java
PDF
Networking
PPT
Network programming in Java
PPTX
IPT Chapter 2 Web Services and Middleware - Dr. J. VijiPriya
PDF
Networking Basics1ofjavaprogramming.pptx.pdf
PPTX
PPT
Networking
Advance Java-Network Programming
Networking in Java
28 networking
Lecture6
Udp Programming
Udp Programming
Java seminar.pptx
Chapter 3 : User Datagram Protocol (UDP)
Md13 networking
5_6278455688045789623.pptx
Socket & Server Socket
UDP-Dept Businesss Computing university .ppt
Java socket programming
Basic Networking in Java
Networking
Network programming in Java
IPT Chapter 2 Web Services and Middleware - Dr. J. VijiPriya
Networking Basics1ofjavaprogramming.pptx.pdf
Networking
Ad

More from rajshreemuthiah (20)

PPTX
PPTX
PPTX
PPTX
polymorphism
PPTX
solutions and understanding text analytics
PPTX
interface
PPTX
Testing &ampdebugging
PPTX
concurrency control
PPTX
Education
PPTX
Formal verification
PPTX
Transaction management
PPTX
Multi thread
PPTX
System testing
PPTX
software maintenance
PPTX
exception handling
PPTX
e governance
PPTX
recovery management
PPTX
Implementing polymorphism
PPSX
Buffer managements
PPTX
os linux
polymorphism
solutions and understanding text analytics
interface
Testing &ampdebugging
concurrency control
Education
Formal verification
Transaction management
Multi thread
System testing
software maintenance
exception handling
e governance
recovery management
Implementing polymorphism
Buffer managements
os linux

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Approach and Philosophy of On baking technology
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
Big Data Technologies - Introduction.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
Unlocking AI with Model Context Protocol (MCP)
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Encapsulation theory and applications.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Machine learning based COVID-19 study performance prediction
Agricultural_Statistics_at_a_Glance_2022_0.pdf
MYSQL Presentation for SQL database connectivity
Approach and Philosophy of On baking technology
NewMind AI Weekly Chronicles - August'25 Week I
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Big Data Technologies - Introduction.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Spectral efficient network and resource selection model in 5G networks
Building Integrated photovoltaic BIPV_UPV.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Network Security Unit 5.pdf for BCA BBA.
The Rise and Fall of 3GPP – Time for a Sabbatical?

Java networking basics & sockets overview

  • 2.  The Inetaddress class provides you with a limited interface to DNS for doing both forward and reverse internet address lookups - An InetAddress class method corresponds to a DNS request  Npublic constructoro
  • 3.  Three static methods:  InetAddress getByName(String)  Static method used to retrieve the address for the host name passed as the parameter.  InetAddress [ ] getAllByName(String)  Static method used to retrieve all the addresses for the host name passed as a parameter.  InetAddress getLocalHost( )  Static method used to retrieve the address for the current, or local, host.
  • 4.  Three additional “getter” methods  String getHostName( )  Returns the host name.  byte[ ] getAddress( )  Returns the IP address.  String getHostAddress( )  Returns the IP address as a string.
  • 5. try { InetAddress fullname = netAddress.getByName(“bigyellowcat.cs.binghamton.edu"); InetAddress alias = InetAddress.getByName(“bigyellowcat"); InetAddress octets = InetAddress.getByName(“128.226.121.44"); if (fullname.equals(alias) && fullname.equals(octets)) // All is right with the world!} catch (UnknownHostException e) { // Exception handling here. }
  • 6.  Once a TCP socket connection is made, a virtual stream is in place. Java’s IO model is that of a stream, therefore the models are consistent; all you need to do connect a TCP socket to a stream and read and write the streams as normal
  • 7.  Socket(String ip, int port)  Creates a streaming socket and binds it to the host and port specified as parameters.  Socket(String ip, int port, boolean TCPorUDP)  Creates a socket and binds it to the host and port specified as parameters. The last parameter is used to indicate whether the socket should be a stream or datagram socket.  Socket(InetAddress ia, int port)  Creates a streaming socket connected to the specified host and port.  Socket(InetAddress ia, int port, boolean TCPorUDP)  Creates a socket connected to the specified host and port. The last parameter specifies whether the socket should be a stream or datagram socket.
  • 8.  InetAddress getInetAddress( )  Returns an InetAddress object representing the host for this socket.  Int getPort( )  . Returns the port number on the remote host for this socket  Int getLocalPort( )  Returns the port number on the local host for this socket.  InputStream getInputStream( )  Returns an input stream for the socket.  OutputStream getOutputStream( )  Returns an output stream for the socket.  Close( )  Closes the socket.  SetSocketImplFactory (SocketImplFactory)  Sets the socket factory that will be used to create all sockets.
  • 9. try { Socket socket = new Socket("somehost.somewhere.com", -1); // Always a good idea to buffer the stream to mitigate blocking. PrintStream out = new PrintStream( new BufferedOutputStream(socket.getOutputStream())); out.println("Are you listening?"); DataInputStream in = new DataInputStream( new BufferedInputStream(socket.getInputStream())); in.readLine(); // ... // Don't forget to close the socket! socket.close() } catch (Exception e) // Exception handling logic.
  • 10.  Since UDP is a connectionless protocol; there is no virtual stream between the hosts so streams are not used for IO.  UDP applications are not thought of in terms of clients and servers, but rather in terms of senders and receivers.  For conversational applications both ends (sender and receiver) will be changing states from sender to receiver and back again  Many UDP based applications are simple send a request then receive the data (sender’s perspective), like a DNS request. The receiver’s perspective is to ‘listen’ for a request, send the response, listen for more requests.
  • 11.  UDP sockets send and receive Datagrams  Constructors: two for receiving, four for sending  DatagramPacket( byte[ ] buff , int len)  Constructs a DatagramPacket for receiving packets of length len.  DatagramPacket(byte[] buf, int off, int len)  Constructs a DatagramPacket for receiving packets of length len, specifying an offset of off bytes into the buffer.  DatagramPacket((byte[] buf, int len, InetAddress addr, int port)  Constructs a datagram packet for sending packets of length len to the specified port number on the specified host.  DatagramPacker(byte[] buf, int off, int len, InetAddress addr, int port)  Constructs a datagram packet for sending packets of length len with offset off to the specified port number on the specified host.  DatagramPacket(byte[] buf, int off, int len, SocketAddress addr)  Constructs a datagram packet for sending packets of length len with offset off to the specified port number on the specified host.
  • 12.  Constructors  DatagramSocket()  Constructs a datagram socket and binds it to any available port on the local host.  DatagramSocket(DatagramSocketImpl impl)  Creates an unbound datagram socket with the specified DatagramSocketImpl.  DatagramSocket(int port)  Constructs a datagram socket and binds it to the specified port on the local host.  DatagramSocket(int port, InetAddress iaddr)  Creates a datagram socket, bound to the specified local address.  DatagramSocket(SocketAddress bindaddr)  Creates a datagram socket, bound to the specified local socket address.