Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: DCCN Lab. No Comments on DCCN lab program – To write a socket program for implementing ping command in Java
0
(0)

Aim

To write a socket program for implementing ping command in Java.

 

S/W required

Java

 

Background information

Client-server communication model

One of the widely used way of communication across the Internet is the client-server communication. In this communication model, there is a system called server which always listens for connections from clients. Once a connection has been established, the server receives the requests from client and processes them.

 

On the other hand, a client is a system that can connect to a server when needed and sends requests to it. A request can be to access a file, data in a database, or some other resource that the server can provide.


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


 

Socket and port

A port is a point of communication. A PC or laptop has a total of 32,536 ports among which top 1024 ports are called as well-defined ports. These 1024 ports are used by different protocols for communication. For example, port 80 is the default port for HTTP. Every application which communicates with other applications across the Internet uses a port.

 

A socket is a combination of the IP address and a port. Generally, for developing a client-server application, we need to establish a connection through sockets for the purpose of communication.

 

Java socket programming

Java socket programming is used for allowing communication between applications running on JRE which are on different systems. Java’s socket programming can be connection-oriented or connectionless.

 

Socket and ServerSocket classes are used for connection-oriented socket programming and DatagramSocket and DatagramPacket classes are used for connectionless socket programming. Majority of the socket programming related classes are available in java.net package.

 

The server binds and listens to a port for connections from the clients. The accept() method of ServerSocket class blocks the console until the client is connected. After the successful connection of client, it returns the instance of Socket at server-side. The general process of client-server communication is as shown in the below figure:

client server socket communication

 

Ping command

The ping command sends packets of data to a specific IP address on a network, and then lets you know how long it took to transmit that data and get a response. It’s a handy tool that you can use to quickly test various points of your network.

 

On a computer network, a ping tool is built into most operating systems that works in much the same way. You issue the ping command along with a specific URL or IP address. Your computer sends several packets of information out to that device, and then waits for a response. When it gets the response, the ping tool shows you how long each packet took to make the round trip—or tells you there was no reply.

 

You can test whether your computer can reach another device—like your router—on your local network, or whether it can reach a device on the Internet. This can help you determine if a network problem is somewhere on your local network, or somewhere beyond. The time it takes packets to return to you can help you identify a slow connection, or if you’re experiencing packet loss.

 

To test the ping command, at the command prompt or terminal, type “ping” along with the URL or IP address you want to ping, and then hit Enter. In the image below, we’re pinging www.google.com and getting a normal response.

ping command output

 

That response shows the URL you’re pinging, the IP address associated with that URL, and the size of the packets being sent on the first line. The next four lines show the replies from each individual packet, including the time (in milliseconds) it took for the response and the time-to-live (TTL) of the packet, which is the number of routers through which the packet can pass before it is discarded.

 

Activity

Java program

pingserver.java

import java.net.*;
import java.io.*;

class NewServer
{
	public static void main(String[] args) throws Exception
	{
		ServerSocket ss = new ServerSocket(6500);
		Socket s = ss.accept();
		DataInputStream dis = new DataInputStream(s.getInputStream());
		DataOutputStream dos = new DataOutputStream(s.getOutputStream());
		String stream="";
		String input="";
		for(int i=1; i<=4; i++)
		{
			stream = dis.readUTF();
			System.out.println("Request " + i + ": " + stream);
			dos.writeUTF("Hello");
			dos.flush();
			Thread.currentThread().sleep(1000);
		}
		dis.close();
		dos.close();
		s.close();
		ss.close();
	}
}

 

pingclient.java

import java.net.*;
import java.io.*;

class NewClient
{
	public static void main(String[] args) throws Exception
	{
		Socket s = new Socket("localhost", 6500);
		DataInputStream dis = new DataInputStream(s.getInputStream());
		DataOutputStream dos = new DataOutputStream(s.getOutputStream());
		String stream="";
		String input="";
		for(int i=1; i<=4; i++)
		{
			dos.writeUTF("Hello");
			dos.flush();
			Thread.currentThread().sleep(1000);
			stream = dis.readUTF();
			System.out.println("Reply " + i + ": " + stream);
		}
		dis.close();
		dos.close();
		s.close();
	}
}

 

Input and output

implementing ping command

 

Result

Ping command was explored and implemented successfully.

 
Visit DCCN video tutorials for more study material.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Leave a Reply

Your email address will not be published. Required fields are marked *