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

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

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

Normalization Types in DBMS

Normalization Types in DBMS First Normal Form (1NF) A relation will be 1NF if it contains an atomic value.  It states that an attribute of a table cannot hold multiple values. It must hold only single-valued attribute. First normal form disallows the multi-valued attribute, composite attribute, and their combinations. Example: Relation EMPLOYEE is not in 1NF because of multi-valued attribute EMP_PHONE. EMPLOYEE table: EMP_ID EMP_NAME EMP_PHONE EMP_STATE 14 John 7272826385, 9064738238 UP 20 Harry 8574783832 Bihar 12 Sam 7390372389, 8589830302 Punjab The decomposition of the EMPLOYEE table into 1NF has been shown below: EMP_ID EMP_NAME EMP_PHONE EMP_STATE 14 John 7272826385 UP 14 John 9064738238 UP 20 Harry 8574783832 Bihar 12 Sam 7390372389 Punjab 12 Sam 8589830302 Punjab Second Normal Form (2NF) In the 2NF, relational must be in 1NF.  In the second normal form, all non-key attributes are fully functional dependent on the primary key  Example: Let's assume, a school can ...