P
Percy
I have written a code for pinging in java ,which worke don some
computer but not in all.Can n e body figure out the problem.The code is
:
import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.geom.*;
import java.net.*;
import java.util.*;
public class Ping extends JPanel implements ActionListener
{
static DatagramSocket socket;
static final int DEFAULT_PORT = 7;
static final int UDP_HEADER = 20 + 8;
static final int BACKSPACE = 8;
static String host = null;
static int port, count = 32, delay = 1000, size = 64;
static boolean flood = false;
ImageIcon conn;
JButton b;
static Vector v;
JScrollPane sp;
JTextField tf;
static JList l;
String s,args;
JLabel l1,l2,li,l4;
public Ping()
{
setLayout(null);
// setBackground(new Color(20,67,137));
v=new Vector();
conn=new ImageIcon("emailme.gif");
li=new JLabel(conn);
l=new JList(v);
sp= new JScrollPane(l);
b=new JButton("ping");
tf= new JTextField();
l1=new JLabel("Enter IP ADDRESS");
l2=new JLabel("STATISTICS");
l4=new JLabel("PING A CLIENT");
setBackground(new Color(13,134,255));
l4.setFont(new Font("TIMES",Font.BOLD,30));
l4.setBounds(250,20,400,30);
b.setBounds(550,80,100,20);
tf.setBounds(280,80,200,20);
l1.setBounds(100,80,200,20);
l2.setBounds(300,120,200,20);
sp.setBounds(120,140,500,220);
li.setBounds(270,110,110,50);
l1.setFont(new Font("TIMES",Font.BOLD,15));
l1.setForeground(Color.black);
l2.setFont(new Font("TIMES",Font.BOLD,15));
l2.setForeground(Color.black);
add(l4);
add(b);
add(sp);
add(tf);
add(l1);
add(l2);
add(li);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
{
args=tf.getText();
parseArgs(args);
try
{
init ();
}catch (IOException e1) {}
for (int i = 0; (i < count) || (count == 0); ++ i)
{
long past = System.currentTimeMillis ();
try
{
ping (i, past);
} catch (IOException e2){}
try
{
pong (i, past);
} catch (IOException e3){}
}
socket.close ();
printStats ();
}
}
static void parseArgs (String args)
{
if (host != null)
syntaxError ();
int colon = args.indexOf (":");
host = (colon > -1) ? args.substring (0, colon) : args;
port = ((colon > -1) && (colon < args.length () - 1)) ?
Integer.parseInt (args.substring (colon + 1)) :
DEFAULT_PORT;
if (host == null)
syntaxError ();
}
static void syntaxError()
{
throw new IllegalArgumentException
("Ping [-c count] [-i wait] [-s packetsize] [-f]
<hostname>[:<port>]");
}
static byte[] outBuffer, inBuffer;
static DatagramPacket outPacket, inPacket;
static void init () throws IOException
{
socket = new DatagramSocket ();
outBuffer = new byte[Math.max (12, size - UDP_HEADER)];
outPacket = new DatagramPacket (outBuffer,
outBuffer.length,InetAddress.getByName (host), port);
inBuffer = new byte[outBuffer.length];
inPacket = new DatagramPacket (inBuffer, inBuffer.length);
}
static int sent = 0;
static void ping (int seq, long past) throws IOException
{
writeInt (seq, outBuffer, 0);
writeLong (past, outBuffer, 4);
socket.send (outPacket);
++ sent;
if (flood)
{
System.out.write ('.');
System.out.flush ();
}
}
static final void writeInt (int datum, byte[] dst, int offset)
{
dst[offset] = (byte) (datum >> 24);
dst[offset + 1] = (byte) (datum >> 16);
dst[offset + 2] = (byte) (datum >> 8);
dst[offset + 3] = (byte) datum;
}
static final void writeLong (long datum, byte[] dst, int offset)
{
writeInt ((int) (datum >> 32), dst, offset);
writeInt ((int) datum, dst, offset + 4);
}
static int received = 0;
static void pong (int seq, long past) throws IOException
{
long present = System.currentTimeMillis ();
int tmpRTT = (maxRTT == 0) ? 500 : (int) maxRTT * 2;
int wait = Math.max (delay, (seq == count - 1) ? tmpRTT : 0);
do
{
socket.setSoTimeout (Math.max (1, wait - (int) (present -
past)));
socket.receive (inPacket);
++ received;
present = System.currentTimeMillis ();
processPong (present);
} while ((present - past < wait) && !flood);
}
static long minRTT = 100000, maxRTT = 0, totRTT = 0;
static void processPong (long present)
{
int seq = readInt (inBuffer, 0);
long when = readLong (inBuffer, 4);
long rtt = present - when;
if (!flood)
{
v.add((inPacket.getLength () + UDP_HEADER) +" bytes from " +
inPacket.getAddress ().getHostName () +": seq no " + seq + " time=" +
rtt + " ms");
l.setListData(v);
}
else
{
System.out.write (BACKSPACE);
System.out.flush ();
}
if (rtt < minRTT) minRTT = rtt;
if (rtt > maxRTT) maxRTT = rtt;
totRTT += rtt;
}
static final int readInt (byte[] src, int offset)
{
return (src[offset] << 24) | ((src[offset + 1] & 0xff) << 16)
|((src[offset + 2] & 0xff) << 8) | (src[offset + 3] & 0xff);
}
static final long readLong (byte[] src, int offset)
{
return ((long) readInt (src, offset) << 32) |((long) readInt
(src, offset + 4) & 0xffffffffL);
}
static void printStats ()
{
v.add(sent + " packets transmitted, " + received + " packets
received, " +(100 * (sent - received) / sent) + "% packet loss");
l.setListData(v);
if (received > 0)
{
v.add("round-trip min/avg/max = " + minRTT + '/' + ((float)
totRTT / received) + '/' + maxRTT + " ms");
l.setListData(v);
}
}
}
Please contact me personally at my email id[email protected]
computer but not in all.Can n e body figure out the problem.The code is
:
import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.geom.*;
import java.net.*;
import java.util.*;
public class Ping extends JPanel implements ActionListener
{
static DatagramSocket socket;
static final int DEFAULT_PORT = 7;
static final int UDP_HEADER = 20 + 8;
static final int BACKSPACE = 8;
static String host = null;
static int port, count = 32, delay = 1000, size = 64;
static boolean flood = false;
ImageIcon conn;
JButton b;
static Vector v;
JScrollPane sp;
JTextField tf;
static JList l;
String s,args;
JLabel l1,l2,li,l4;
public Ping()
{
setLayout(null);
// setBackground(new Color(20,67,137));
v=new Vector();
conn=new ImageIcon("emailme.gif");
li=new JLabel(conn);
l=new JList(v);
sp= new JScrollPane(l);
b=new JButton("ping");
tf= new JTextField();
l1=new JLabel("Enter IP ADDRESS");
l2=new JLabel("STATISTICS");
l4=new JLabel("PING A CLIENT");
setBackground(new Color(13,134,255));
l4.setFont(new Font("TIMES",Font.BOLD,30));
l4.setBounds(250,20,400,30);
b.setBounds(550,80,100,20);
tf.setBounds(280,80,200,20);
l1.setBounds(100,80,200,20);
l2.setBounds(300,120,200,20);
sp.setBounds(120,140,500,220);
li.setBounds(270,110,110,50);
l1.setFont(new Font("TIMES",Font.BOLD,15));
l1.setForeground(Color.black);
l2.setFont(new Font("TIMES",Font.BOLD,15));
l2.setForeground(Color.black);
add(l4);
add(b);
add(sp);
add(tf);
add(l1);
add(l2);
add(li);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
{
args=tf.getText();
parseArgs(args);
try
{
init ();
}catch (IOException e1) {}
for (int i = 0; (i < count) || (count == 0); ++ i)
{
long past = System.currentTimeMillis ();
try
{
ping (i, past);
} catch (IOException e2){}
try
{
pong (i, past);
} catch (IOException e3){}
}
socket.close ();
printStats ();
}
}
static void parseArgs (String args)
{
if (host != null)
syntaxError ();
int colon = args.indexOf (":");
host = (colon > -1) ? args.substring (0, colon) : args;
port = ((colon > -1) && (colon < args.length () - 1)) ?
Integer.parseInt (args.substring (colon + 1)) :
DEFAULT_PORT;
if (host == null)
syntaxError ();
}
static void syntaxError()
{
throw new IllegalArgumentException
("Ping [-c count] [-i wait] [-s packetsize] [-f]
<hostname>[:<port>]");
}
static byte[] outBuffer, inBuffer;
static DatagramPacket outPacket, inPacket;
static void init () throws IOException
{
socket = new DatagramSocket ();
outBuffer = new byte[Math.max (12, size - UDP_HEADER)];
outPacket = new DatagramPacket (outBuffer,
outBuffer.length,InetAddress.getByName (host), port);
inBuffer = new byte[outBuffer.length];
inPacket = new DatagramPacket (inBuffer, inBuffer.length);
}
static int sent = 0;
static void ping (int seq, long past) throws IOException
{
writeInt (seq, outBuffer, 0);
writeLong (past, outBuffer, 4);
socket.send (outPacket);
++ sent;
if (flood)
{
System.out.write ('.');
System.out.flush ();
}
}
static final void writeInt (int datum, byte[] dst, int offset)
{
dst[offset] = (byte) (datum >> 24);
dst[offset + 1] = (byte) (datum >> 16);
dst[offset + 2] = (byte) (datum >> 8);
dst[offset + 3] = (byte) datum;
}
static final void writeLong (long datum, byte[] dst, int offset)
{
writeInt ((int) (datum >> 32), dst, offset);
writeInt ((int) datum, dst, offset + 4);
}
static int received = 0;
static void pong (int seq, long past) throws IOException
{
long present = System.currentTimeMillis ();
int tmpRTT = (maxRTT == 0) ? 500 : (int) maxRTT * 2;
int wait = Math.max (delay, (seq == count - 1) ? tmpRTT : 0);
do
{
socket.setSoTimeout (Math.max (1, wait - (int) (present -
past)));
socket.receive (inPacket);
++ received;
present = System.currentTimeMillis ();
processPong (present);
} while ((present - past < wait) && !flood);
}
static long minRTT = 100000, maxRTT = 0, totRTT = 0;
static void processPong (long present)
{
int seq = readInt (inBuffer, 0);
long when = readLong (inBuffer, 4);
long rtt = present - when;
if (!flood)
{
v.add((inPacket.getLength () + UDP_HEADER) +" bytes from " +
inPacket.getAddress ().getHostName () +": seq no " + seq + " time=" +
rtt + " ms");
l.setListData(v);
}
else
{
System.out.write (BACKSPACE);
System.out.flush ();
}
if (rtt < minRTT) minRTT = rtt;
if (rtt > maxRTT) maxRTT = rtt;
totRTT += rtt;
}
static final int readInt (byte[] src, int offset)
{
return (src[offset] << 24) | ((src[offset + 1] & 0xff) << 16)
|((src[offset + 2] & 0xff) << 8) | (src[offset + 3] & 0xff);
}
static final long readLong (byte[] src, int offset)
{
return ((long) readInt (src, offset) << 32) |((long) readInt
(src, offset + 4) & 0xffffffffL);
}
static void printStats ()
{
v.add(sent + " packets transmitted, " + received + " packets
received, " +(100 * (sent - received) / sent) + "% packet loss");
l.setListData(v);
if (received > 0)
{
v.add("round-trip min/avg/max = " + minRTT + '/' + ((float)
totRTT / received) + '/' + maxRTT + " ms");
l.setListData(v);
}
}
}
Please contact me personally at my email id[email protected]