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

Schedule in DBMS

Schedule A series of operation from one transaction to another transaction is known as schedule. It is used to preserve the order of the operation in each of the individual transaction. 1. Serial Schedule The serial schedule is a type of schedule where one transaction is executed completely before starting another transaction. In the serial schedule, when the first transaction completes its cycle, then the next transaction is executed. For example: Suppose there are two transactions T1 and T2 which have some operations. If it has no interleaving of operations, then there are the following two possible outcomes: Execute all the operations of T1 which was followed by all the operations of T2.  Execute all the operations of T1 which was followed by all the operations of T2.  In the given (a) figure, Schedule A shows the serial schedule where T1 followed by T2. In the given (b) figure, Schedule B shows the serial schedule where T2 followed by T1. 2. Non-serial Schedule If interle...

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...

Mathematical Symbols

Mathematical Symbols (Source: Google Images) Basic mathematical symbols with name, meaning, and examples : The basic mathematical symbols used in Mathematics help us to work with mathematical concepts in a theoretical way. In simple words, without symbols, we cannot do mathematics. Mathematical signs and symbols are considered representative of value. The basic symbols in mathematics are used to express mathematical thoughts.  The relationship between sign and value refers to the fundamental need of mathematics. With the help of symbols, certain concepts and ideas are clearly explained. Here is a list of commonly used math symbols with names and meanings. In addition, an example is provided to understand the use of mathematical symbols. Symbol Symbol Name in Maths Math Symbols Meaning Example ≠ not equal sign inequality 10 ≠ 6 = equal sign equality 3 = 1 + 2 < strict inequality less than 7 < 10 > strict inequality greater than 6 > 2 ≤ inequality less than or equal to x ...