Network programming – Networking classes and interfaces in java

Network programming – Networking classes and interfaces in java

Network programming – Java is an internet programming language. Java supports the package java.net to make internet programs. Java supports two types of protocols-

  • TCP/IP Protocol
  • UDP Protocol

 

TCP/IP Protocol – Tutorial in java

TCP/IP is a connection-oriented protocol. It is a reliable stream connection protocol. Sockets are used for TCP/IP.

All the computers on the internet using TCP/IP communicate through the sockets.

 

Sockets – Tutorial in java

A socket is one endpoint of a two-way communication link between two programs running on the network. The socket classes are used to represent the connection between a client program and a server program.

The socket is not a physical device but an abstraction for the software used for the networking, that allows communication between a server and a client.

Classes that use TCP to communicate over a network are as below-

  • URL
  • URLConnection
  • Socket
  • ServerSocket

 

UDP Protocol

UDP protocol is a connectionless protocol. UDP protocol is used to send data packets (Datagram) over a network.

 

Difference between TCP and UDP

  • TCP establishes a reliable connection over a network between two computers. UDP establish an unreliable connection over a network between two computers
  • In TCP data sent in a continuous stream from source to a destination like telephone calls. In UDP data is to be sent in packets like a letter.
  • In TCP data is received in the order as it is transmitted. In UDP order of data may be changed in the destination site.

 

IP Address – Tutorial in java

The internet protocol provides every network device, a logical address called IP address.

The IP address consists of a series of four 8 bits numbers. Hence, the IP address has a 32 pattern ranging from the value 0 to 255.

In java, InetAddress is a class of java.net package that is used for finding IP addresses of computers that exist on the Internet.

The InetAddress class has following methods-

getLocalHost()

getByName()

getByAllName()

 

Following program gives the Local machine IP address

 

Network programming Tutorial in java

//Network programming in java – Program to get local machine IP address

import java.net.*;

import java.io.*;

public class FindIPaddress

{

Public static void main(String args[])

{

InetAddress a= InetAddress.getLocalHost();

System.out.println(“Host and address:” + a);

System.out.println (“Host name:”+a.getHostName());

String s=a.toString();

System.out.println(“IP address” + s.substring(s.indexOf(“/”) +1));

}

}

Output-

Host and address diya/127.0.0.1

Host name:diya

IP address: 127.0.0.1

 

Program – Write a program in java that ask to enter hostname and the program will return IP address

Network programming Tutorial in java

//Network programming in java – Enter host name to get IP address

import java.net.*;

import java.io.*;

public class FindIPaddress

{

Public static void main(String args[])

{

System.out.println(“Enter the host name:-”);                 

String s=new DataInputStream(System.in).readLine();

InetAddress i=InetAddress.getByName(s));

System.out.pritln(“IP address:”+i);

}

}

Compile and run the program FindIPaddress.java

Javac FindIPaddress.java

Java FindIPaddress

Output:

Enter the host name:-Diya

IP address:127.0.0.1

 

Port – Tutorial in java

Port is not a physical device.

It is an abstraction to make communication possible between the client and a server.

The TCP/IP uses ports to map incoming data to a specific process running on the computer.

Ports are represented by 16-bit numbers.

Port numbers range from 0 to 65535.

 

URL – Tutorial in java

URL is a uniform resource locator. URL is a reference or an address to a resource on the internet. For example URL –

https://www.csitweb.com/ is an address to find the website csitweb.com.

URL has two components-

  • Protocol identifier
  • Resource name

The following program is an example to get the URL. To run this program your computer must be connected with the Internet otherwise program will not run and will give an error message.

 

Network programming Tutorial in java

// Network programming Tutorial in java

import java.net.*;

import java.io.*;

public class FindURL

{

Public static void main(String args[]) throws Exception

{

URL u = new(“http://java.sun.com:80/docs/index#down”);

System.out.println(“protocol:”+u.getProtocol());

System.out.println(“host:”+u.getHost());

System.out.println(“filename:”+u.getFile());

System.out.println(“port:” +u.getPort());

System.out.println(“ref”+u.getRef());

}

}

 

Compile and run the program- FindURL.java

Javac FindURL.java

Java FindURL

Output-

protocol:http

host:java.sun.com

filename:/docs/index

port:80

ref:down

 

Network programming in java – Tutorial in java

 

Thanks for reading, Welcome to your comments on this Post

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