Results 1 to 3 of 3

Thread: Ping Client

  1. #1
    ICER is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default Ping Client

    Hey guys!
    I am new to Java, but I am trying to write a Ping Client and I keep getting an error that says:

    "cannot find symbol
    symbol: class PingMessage"

    Here is my code:

    Java Code:
    import java.io.*;
    import java.net.*;
    import java.lang.Object;
    import java.util.*;
    import java.net.InetAddress.*;
    
    public class PingClient implements Runnable
    {
    	String hostname;
    	int portnumber;
    	int pingReplies = 0;
    	static final int pingLength = 1024;
    	static final int PINGS = 10;
    	static final int TIMEOUT = 1000;
    	static final int WAIT = 5000;
    	/* Arrays */
    	static boolean[] replies = new boolean[PINGS];
    	static long[] RTT = new long[PINGS];
    	DatagramSocket socket;
    	
    	public PingMessage receivePing() throws SocketTimeoutException
    	{
    		byte recvBuf[] = new byte[pingLength];
    		DatagramPacket recvPacket = new DatagramPacket(recvBuf, pingLength);
    		PingMessage reply = null;
    		try
    		{
    			socket.receive(recvPacket);
    			System.out.println("Received message from " + recvPacket.getAddress() + ":" + recvPacket.getPort());
    			String recvMsg = new String(recvPacket.getData());
    			reply = new PingMessage(recvPacket.getAddress(), recvPacket.getPort(), recvMsg);
    		}
    		catch (SocketTimeoutException e)
    		{
    			throw e;
    		}
    		catch (IOException e)
    		{
    			System.out.println("Error reading from socket.");
    		}
    		return reply;
    	}
    	
    	public void sendPing(PingMessage ping)
    	{
    		InetAddress host = ping.getHost();
    		int port = ping.getPort();
    		String message = ping.getContents();
    		
    		DatagramPacket packet = new DatagramPacket(message.getBytes(), message.length(), host, port);
    		socket.send(packet);
    		System.out.println("Sent packet to " + host + ":" + port);
    	}
    	
    	private void handleReply(String reply)
    	{
    		String[] tmp = reply.split(" ");
    		int pingNumber = Integer.parseInt(tmp[1]);
    		long then = Long.parseLong(tmp[2]);
    		replies[pingNumber] = true;
    		
    		Date now = new Date();
    		RTT[pingNumber] = now.getTime() - then;
    		pingReplies++;
    	}
    	
    	public PingClient (String host, int port)
    	{
    		hostname = host;
    		portnumber = port;
    	}
    	
    	public void createSocket()
    	{
    		try
    		{
    			socket = new DatagramSocket();
    		}
    		catch (SocketException e)
    		{
    			System.out.println("Error creating socket.");
    		}
    	}
    	
    	public void createSocket(int port)
    	{
    		try
    		{
    			socket = new DatagramSocket(port);
    		}
    		catch (SocketException e)
    		{
    			System.out.println("Error creating socket.");
    		}
    	}
    	
    /*-----------------------------------------------------------*/	
    	public void run()
    	{
    		createSocket();
    		socket.setSoTimeout(TIMEOUT);
    		
    		for (int i = 0; i < PINGS; i++)
    		{
    			Date now = new Date();
    			String message = "PING " + i + " " + now.getTime() + " ";
    			replies[i] = false;
    			RTT[i] = 1000000;
    			PingMessage ping = null;
    		}
    		
    		ping = new PingMessage(InetAddress.getByName(hostname), portnumber, message);
    		sendPing(ping);
    		
    		PingMessage reply = receivePing();
    		handleReply(reply.getContents());
    		
    		try
    		{
    			socket.setSoTimeout(WAIT);
    		}
    		catch (SocketTimeoutException e)
    		{
    			System.out.println("Error setting timeout REPLY_TIMEOUT: ");
    		}
    		while (pingReplies < PINGS)
    		{
    			try
    			{
    				PingMessage reply = receivePing();
    				handleReply(reply.getContents());
    			}
    			catch (SocketTimeoutException e)
    			{
    				pingReplies = PINGS;
    			}
    		}
    		
    		for (int i = 0; i < PINGS; i++)
    		{
    			System.out.println("PING " + i + ": " + replies[i] + " RTT: " + RTT[i]);
    		}
    	}
    	/*----------------------------------------------------------------*/
    	public static void main(String args[])
    	{
    		String host = null;
    		int port = 0;
    		
    		host = args[0];
    		port = Integer.parseInt(args[1]);
    		System.out.println("Contacting host " + host + " at port " + port);
    		PingClient Client = new PingClient(host, port);
    		Client.run();
    	}
    }
    Am I using the function wrong or what? I can't figure out what I am doing wrong. Thanks in advance for any help.

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Don't double post

    Don't double or cross post... it irritates the people that are trying to help you and it won't get you an answer any faster.
    Ping Client error - help. =]

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    naavo is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default

    As I see you might be missing on the PingMessage class,which I believe is just a place holder or a POJO with fields IP Address, Port and Message.

    Its been done so that we can play around with the Reply Message

Similar Threads

  1. Ping in Java
    By VeasMKII in forum New To Java
    Replies: 7
    Last Post: 10-14-2012, 09:51 PM
  2. Ping pong game
    By adam405 in forum New To Java
    Replies: 8
    Last Post: 10-20-2010, 03:52 PM
  3. Ping Client error - help. =]
    By ICER in forum Networking
    Replies: 3
    Last Post: 02-18-2009, 02:57 PM
  4. How to mesure ping delay in JAVA..?
    By sacr83 in forum Networking
    Replies: 4
    Last Post: 06-15-2008, 06:37 AM
  5. ping cmd
    By kishi in forum Advanced Java
    Replies: 2
    Last Post: 11-14-2007, 03:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •