• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Network Programming – java.net Package

July 27, 2017 By j2eereference Leave a Comment

Java.net package is composed of (i) classes, (ii) exceptions, and (iii) interfaces that offer a commanding infrastructure for Java networking.

  1. URL

Uniform Resource Locator (URL) signifies a source on the WWW (World Wide Web). A URL can be split up into protocol (http, https, etc), host (authority), port, path (filename), reference and query.

Methods

The URL class offers:

  • Methods for obtaining specific parts (protocol, port, etc) of the provided URL.
  • Methods for deciding whether 2 URL element references have the identical content.
  • Also openConnection( ) method delivers a URLConnection that offers a link to the distant content.

 

  1. URLCONNECTION

The URLConnection class recovers the parts stated by URL elements.

Methods

  • The getContent( ) method is similar to URL class method.
  • URLConnection class also offers methods for obtaining header field estimate. Ex: (i) getContentType( ), (ii) guessContentTypeFromStream( ).
  • Methods getInputStream ( ) that reads information and getOutputStream ( ) for output are offered.
  • Other methods handle the fixing or recovering operations of variables.

 

  1. Socket

A Socket class is the Java depiction of a TCP link. When a Socket is developed, a link is created to a particular destination.

Constructors

Socket constructors support 2 arguments:

  • Host name and
  • Port value.

Methods

  • Methods (i) getInputStream( ), (ii) getOutputStream( ), deliver stream elements that exchange information via socket.
  • Close method orders the primary operating system to shutdown.
  • Few methods obtain information about the link.

 

  1. ServerSocket

ServerSocket denotes a listening TCP link. When a link is requested, this class will deliver a Socket component representing the link.

Constructors

The constructors get the port number as argument for listening the link requests.

Methods

  • The accept( ) method stops the requesting thread until a link is accepted.
  • The close( ) method instructs the system to prevent listening to requests on the socket.
  • There are also methods to recover the host name and port number.

 

  1. DATAGRAMSOCKET

The DatagramSocket signifies a linkless datagram socket. Along with DatagramPacket, this class enables communication utilizing the UDP protocol.

Constructors

As UDP is linkless, port number alone is required and host name is not required for developing a DatagramSocket. There is another constructor that does not obtain arguments.

Methods

Every method acquires a properly created DatagramPacket.

  • send( ) – sends the packet data to the given host and port.
  • receive( ) – stops processing until a packet is obtained by the socket.
  • close( ) – requests the socket to shutdown.
  • getLocalPort( ) – submits the local port number linked with the socket.
  1. DATAGRAMPACKET

DatagramPacket signifies a data packet transmitted by means of UDP protocol (with a DatagramSocket).

Constructors

The DatagramPacket offers 2 constructors for:

  • Incoming packets that picks a byte array as argument and
  • Outgoing packets that picks distant host name and port number as arguments.

Methods

There are 4 methods in the class, they are:

  • getData( ) – retrieves data
  • getLength( ) – retrieves datagram length
  • getAddress( ) – retrieves address
  • getPort( ) – retrieves port number

 

 

  1. INETADDRESS

The InetAddress signifies a host name and its IP values. The class also offers the functionality for acquiring IP value.

Constructors

This class has no clear constructors. Alternatively, static methods getByName( ) and getAllByName( ), are used.

Methods

  • getHostName( ) – delivers a host name that the InetAddress specifies
  • getAddress( ) – delivers an array of the raw bytes of address
  • equals( ) – compares the address objects
  • toString( ) – prints host name and IP address

Related Posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Difference between Stack and Heap memory
  • What is ThreadMXBean in java?
  • What is serialVersionUID
  • What is exchanger in concurrency?
  • UncaughtExceptionHandler in java

Filed Under: Core Java

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.