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

JS Code for Generating OTP

JS Code for Generating OTP -  * Learn how to create a simple JavaScript function to generate a random 4-digit OTP. (GENERATED BY - ChatGPT) function OTP() { let otp = ""; otp = Math.floor(Math.random() * 9000 + 1000); return otp; } console.log("Your OTP is-", OTP());

Windows Key Shortcuts

List of common keyboard shortcuts that can be used with the Windows key: (Image by - Sharma Guides | Subham232330) 1. Windows Key + D: Show the Desktop 2. Windows Key + E: Open File Explorer 3. Windows Key + I: Open Settings 4. Windows Key + L: Lock the computer 5. Windows Key + R: Open the Run Dialog 6. Windows Key + S: Open the search bar 7. Windows Key + Tab: Open Task View 8. Windows Key + Ctrl + D: Create a new virtual desktop 9. Windows Key + Ctrl + Left or Right arrow: Switch between virtual desktops 10. Windows Key + M: Minimize all windows 11. Windows Key + Shift + M: Undo minimize all windows 12. Windows Key + Up Arrow: Maximize the current window 13. Windows Key + Down Arrow: Minimize the current window 14. Windows Key + Right Arrow: Snap the current window to the right 15. Windows Key + Left Arrow: Snap the current window to the left 16. Windows Key + P: Project to a second screen 17. Windows Key + Home: Minimize all but the active window 18. Windows Key + ...