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

Book Photography Tips

বইছবি সুন্দর করার জন্য কয়েকটি পরামর্শঃ ১। প্রথম তো দিনের আলোতে বইয়ের ছবি তুলুন। গুমোট অন্ধকারে তোলা ছবির লুক ভালো আসে না। রাতে লাইটের আলোতে তোলা ছবির লুকও যথেষ্ট ভালো আসে না। ২। যে বইটার ছবি তুলবেন, শুধুমাত্র ঐ বইটার দিকে ক্যামেরার ফোকাস রাখুন। ডেকোরেশনের জন্য অন্য বই সাথে রাখতে পারেন, কিন্তু ঐসকল বইয়ের নাম যেন দেখা না যায়। ঐসকল বইয়ের পেছনের মলাট খোলা রেখে ছবি তুলুন। একটা বই উল্টো করে অন্য বইটা ঢেকে দিন। ৩। বইয়ের বেকগ্রাউন্ডে পুরোপুরি সাদা বা এক কালারের রঙিন কাপড় বিছিয়ে ছবি তুলুন। ছবির সৌন্দর্য অনেকগুণ বাড়বে। ৪। পারতপক্ষে বইয়ের সাথে দু-একটি বুকমার্ক রাখুন। ছবি মানানসই দেখাবে। ৫। সৌন্দর্য বৃদ্ধির জন্য প্রয়োজনীয় উপকরণ যেমন:  চায়ের কাপ, মগ, কালার পেপার, ফুল, রঙিন পাতা, ইত্যাদি আনুষঙ্গিক জিনিসপত্র সাথে রেখে ডেকোরেশন করে এরপর বইয়ের ছবি তুলুন। আগের চেয়ে বেটার আসবে ছবি। ৬। ছবি তোলার আগে উপকরণগুলো বিভিন্ন পজিশনে সাজিয়ে নিন। কোনো উপকরণ দুটোর বেশি রাখবেন না। হিজিবিজি দেখাবে। আবার অনেকগুলো উপকরণ সাথে রেখে ছবি তুলতে যাবেন না। বেমানান লাগবে। ৭। বইয়ের বিষয়বস্তুর সাথে খাপ খায় এমন শব্দ, বাক্য সাদা...

Indexing in DBMS

Indexing in DBMS Indexing is used to optimize the performance of a database by minimizing the number of disk accesses required when a query is processed.  The index is a type of data structure. It is used to locate and access the data in a database table quickly. Index structure: Indexes can be created using some database columns. The first column of the database is the search key that contains a copy of the primary key or candidate key of the table. The values of the primary key are stored in sorted order so that the corresponding data can be accessed easily.  The second column of the database is the data reference. It contains a set of pointers holding the address of the disk block where the value of the particular key can be found. Indexing Methods Ordered indices The indices are usually sorted to make searching faster. The indices which are sorted are known as ordered indices. Example: Suppose we have an employee table with thousands of record and each of which is 10 byte...

List vs Tuple

List vs Tuple (Image by - Sharma Guides | Subham232330) List | |- Syntax :- [2,3,4] | |- Mutable :- Elements can be changed after creation. | |- Length :- Variable length. | |- Memory :- List takes more memory than a tuple. Tuple | |- Syntax :- (2,3,4) | |- Immutable :- Elements cannot be changed after creation. | |- Length :- Fixed length. | |- Memory :- It takes less memory than a list.