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

    Default Ping Client error - help. =]

    Hi, I am writing a UDP ping client program. I am using a function called PingMessage, however I keep getting compile errors that say:
    "cannot find symbol
    symbol: class PingMessage"

    Maybe I am overlooking something small... I was up all night coding this.
    Thanks in advance for any help!

    Code here:

    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();
    	}
    }

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

    Default PingMessage class?

    huh... I don't see a PingMessage class. I only see a PingClient class... does PingMessage.class exist?

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

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

    Default

    I am getting the error from the function:

    public PingMessage receivePing()

    Am I using that correctly?

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    What is a PingMessage? Either it exists somewhere, and you didn't import it, or you haven't written it yet.

Similar Threads

  1. Ping in Java
    By VeasMKII in forum New To Java
    Replies: 7
    Last Post: 10-14-2012, 10:51 PM
  2. Ping pong game
    By adam405 in forum New To Java
    Replies: 8
    Last Post: 10-20-2010, 04:52 PM
  3. How to mesure ping delay in JAVA..?
    By sacr83 in forum Networking
    Replies: 4
    Last Post: 06-15-2008, 07:37 AM
  4. Replies: 1
    Last Post: 02-15-2008, 05:07 PM
  5. ping cmd
    By kishi in forum Advanced Java
    Replies: 2
    Last Post: 11-14-2007, 04: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
  •