Skip to main content

Implement a ping server program using TCP socket

Implement a ping server program using a TCP socket



Server-side


import java.net.*;
import java.io.*;
import java.net.SocketException;
import java.util.*;
import java.lang.*;
class pingtcpserver
{
public static void main(String args[]) throws Exception
{
ServerSockets=null;
Socketsoc=null;
String smsg="";
PrintWriter pw;
try
{
s=new ServerSocket(5219);
System.out.println("Connecting with client ....... ");
soc=s.accept();
System.out.println("Connection Established");
BufferedReader brin=new BufferedReader(new InputStreamReader(soc.getInputStream()));
for(int i=0;i<5;i++)
{
pw=new PrintWriter(soc.getOutputStream(),true);
smsg=brin.readLine();
if(i==3)
Thread.sleep(100);
pw.println(smsg);
}}
catch(SocketException e)
{
System.out.println(e);
}
finally
{
s.close();
soc.close();
}}}




Client-side


import java.io.*;
import java.net.*;
import java.net.SocketException;
import java.util.*;
class pingtcpclient
{
public static void main(String args[]) throws Exception
{
Socketsoc=null; try
{
String str="";
String ip="127.0.0.1";
String cmsg="Ping Testing";
intsendcount=0,recvcount=0,lost=0;
long diff=0,avg=0,min=0,max=0,tottime=0;
soc=new Socket("localhost",5219);
BufferedReader in=new BufferedReader( new InputStreamReader(soc.getInputStream())); PrintWriter
pw=new PrintWriter(soc.getOutputStream(),true);
System.out.println("");
System.out.println("Pinging"+" "+ ip+" "+"with"+" "+cmsg.length()+" "+"bytes of data:");
System.out.println("");
for(int i=0;i<5;i++)
{
Date senddate=new Date();
sendcount=sendcount+1;
pw.println(cmsg);
str=in.readLine();
Date receivedate=new Date();
if (!str.equals(""))
{
recvcount=recvcount+1;
}
lost=recvcount-sendcount;
diff=((receivedate.getTime())-(senddate.getTime()));
if(recvcount==1)
{
min=diff;
}
tottime=tottime+diff;
if(diff<min)
{
min=diff;
}
if(diff>max)
{
max=diff;
}
avg=(tottime/recvcount);
if (diff > 2000)
System.out.println("Timeout");
else
System.out.println("Reply from "+ ip +": "+"bytes=" +str.length() +" "+"time<"+diff+ "ms TTL=128");
}//
for System.out.println("");
System.out.println("Ping statistics for "+ip+":");
System.out.println (" Packets: Sent = "+cmsg.length()+", Received= "+str.length() +", Lost= "+
lost+"("+(lost/sendcount)*100+"%loss),");
System.out.println("Approximate round trip times in milli-seconds:");
System.out.println(" Minimum= "+min+"ms, Maximum= "+max+"ms, Average= "+avg+"ms");
}//try
catch(Exception e)
{
System.out.println(e);
}
finally
{
soc.close();
}}}

Comments

Popular posts from this blog

DBMS Keys

DBMS Keys KEYS in DBMS is an attribute or set of attributes which helps you to identify a row (tuple) uniquely in a relation(table). They allow you to find the relation between two tables. Keys help you uniquely identify a row in a table by a combination of one or more columns in that table. Key is also helpful for finding unique record or row from the table. Database key is also helpful for finding unique record or row from the table. Example: Employee ID FirstName LastName 11 Andrew Johnson 22 Tom Wood 33 Alex Hale In the above-given example, employee ID is a primary key because it uniquely identifies an employee record. In this table, no other employee can have the same employee ID. Here are some reasons for using sql key in the DBMS system. Keys help you to identify any row of data in a table. In a real-world application, a table could contain thousands of records. Moreover, the records could be duplicated. Keys in RDBMS ensure that you can uniquely identify a table record despite ...

four methods overload these methods

Write a program in java which has the following classes and methods:  Class : OverloadDemo Methods : test() Declare four methods with the same name “test()” and overload these methods class OverloadDemo{  public void test(){  System.out.println("It's a test method for null.");  }  public void test(int n){  System.out.println("It's a test method for displaying the int value "+n);  }  public void test(double d){  System.out.println("It's a test method for displaying the double value "+d);  }  public void test(String s){  System.out.println("It's a test method for displaying the String "+s);  }  }  class overClass{  public static void main(String[] args) {  OverloadDemo old = new OverloadDemo();  old.test();  old.test(12);  old.test(25.35);  old.test("Subham");  }  } OUTPUT: It's a test method for null. It's a test method for displaying the int value 12 It's a test method for ...

Computer Full Forms

COMPUTER - full form or meaning is :  Common Operating Machine Purposely Used for Technological and Educational Research. COMPUTER ABBREVIATIONS CPU - Central Processing Unit RAM - Random Access Memory ROM - Read Only Memory PROM - Programmable Read Only Memory EPROM - Erasable PROM EEPROM - Electrically EPROM HDD - Hard Disk Drive FDD - Floppy Disk Drive KBD - KeyBoard I/O - Input & Output CD - Compact Disk DVD - Digital Video Disk SMPS - Switch Mode Power Supply POST - Power ON Self Test BIOS - Basic Input Output System VDU - Visible Display Unit LED - Light Embedded Diode LCD - Liquid Crystal Display USB - Universal Serial Bus VGA - Video/Visual Graphic Adapter LAN - Local Area Network WAN - Wide Area Network MAN - Metropolitan Area Network HLL - High-Level Language LLL - Low-Level Language MIPS - Million of Instruction Per Second Mbps - Mega Bytes Per second Kbps - Kilo Bytes per second HTTP - Hyper Text Templates WWW - World Wide Web IP - Int...