Write A Socket Program For Echo/ping/talk Commands In Java

EC6611 COMPUTER NETWORKS (CN) Lab Manual. Study of Socket Programming and Client – Server model; Write a socket Program for Echo/Ping/Talk commands. To create scenario and study the performance of network with CSMA / CA protocoland compare with CSMA/CD protocols. I'm writing my first java client/server program which just establishes a connection with the server sends it a sentence and the server sends the sentence back all capitalized.

I have a TCP Client which continuously sends data to the server(The server is running continuously). It creates connection each time to the server to send the data. However, it throws exception after sending some amount of data to the server. Data is coming from some sensors continuously and that data is sent by my TCP Client to the server.

Here is my piece of TCPClient code :


try {

System.out.println('Inside Client :' + device_data);
logger.info('Inside Client :' + device_data);

InputStream is = new ByteArrayInputStream(device_data.getBytes());
BufferedReader socketRead = new BufferedReader(new InputStreamReader(is));

/**
* sending the contents to server. Uses PrintWriter
*/
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);

String str;

/**
* Reading data line by line
*/
while ((str = socketRead.readLine()) != null) {

if(!pwrite.checkError()){
pwrite.println('Data sent by Client : ' + str);
logger.info('Data sent by client' + str);
System.out.println('Data sent by client' + str);
}

else{

logger.info('Error in writing data');
System.out.println('Error in writing data');
}


}
pwrite.close();
socketRead.close();

}

catch (Exception exception) {

System.out.println('Exception' + exception);
logger.info('Exception in TCPClient :' + exception);
}

Thanks in advance.

Active10 months ago

Hi I need to execute the PING command using Java code and get summary of the ping host. How to do it in Java?

Bhesh Gurung
43.1k20 gold badges77 silver badges133 bronze badges
Salman RazaSalman Raza
5304 gold badges10 silver badges17 bronze badges

4 Answers

as viralpatel specified you can use Runtime.exec()

following is an example of it

output

refer http://www.velocityreviews.com/forums/t146589-ping-class-java.html

Uwe Plonus
8,3003 gold badges28 silver badges42 bronze badges
Hemant MetaliaHemant Metalia
20.8k14 gold badges61 silver badges85 bronze badges

InetAddress class has a method which uses ECMP Echo Request (aka ping) to determine the hosts availability.

If the above reachable variable is true, then it means that the host has properly answered with ECMP Echo Reply (aka pong) within given time (in millis).

Note: Not all implementations have to use ping. The documentation states that:

A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host.

Therefore the method can be used to check hosts availability, but it can not universally be used to check for ping-based checks.

DariuszDariusz
16k5 gold badges49 silver badges86 bronze badges

Check out this ping library for java I'm making:

Maybe it can help

tgoossenstgoossens
7,5801 gold badge11 silver badges21 bronze badges

Socket Program For One Way Communication

Below code will work on both Windows and Linux system,

Rakesh ChaudhariRakesh Chaudhari

Write A Socket Program For Echo/ping/talk Commands In Java

1,2911 gold badge14 silver badges20 bronze badges
Socket program for one way communication

Not the answer you're looking for? Browse other questions tagged javacommand-lineping or ask your own question.

Comments are closed.