0% found this document useful (0 votes)
10 views5 pages

Traceroute CN

Uploaded by

mr.dhanush.j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Traceroute CN

Uploaded by

mr.dhanush.j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

TraceRouteServer :

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class TraceRouteServer
{
public static void main(String[] args) throws IOException
{
int port = 33434;
// Port on which to listen for incoming packets
DatagramSocket serverSocket = new DatagramSocket
(port);
System.out.println("TraceRoute Server is running on port
" + port + "...");
while (true)
{
byte[] receiveData = new byte[1024];
DatagramPacket receivePacket=new Datagram
Packet (receiveData, receiveData.length);
// Receive packet from client
serverSocket.receive(receivePacket);
String message=new String(receivePacket.getData
(),0, receivePacket.getLength());
InetAddress clientAddress=receivePacket
.getAddress();
int clientPort = receivePacket.getPort();
System.out.println("Received from client: " +
message + " | Responding back...");
// Send a response packet back to client
String responseMessage = "Reply from " +

InetAddress.getLocalHost().getHostAddress();
byte[] responseData=responseMessage.
getBytes();
DatagramPacket responsePacket = new Datagram
Packet(responseData, responseData.length,clien
tAddress, clientPort);
serverSocket.send(responsePacket);
}
}
}

TraceRouteClient :

import java.io.IOException;
import java.net.*;
public class TraceRouteClient
{
public static void main(String[] args) throws IOException
{
String serverAddress = "localhost";
// Use the actual server address if needed
int port = 33434;
int maxHops = 30;
InetAddress address = InetAddress.getByName
(serverAddress);
System.out.println("Tracing route to " + serverAddress);
for (int ttl = 1; ttl <= maxHops; ttl++)
{
DatagramSocket socket = new DatagramSocket();
socket.setSoTimeout(3000);
// 3 seconds timeout
try {
socket.setTrafficClass(ttl);
// Setting the TTL
byte[] data = ("Trace packet with TTL " +
ttl).getBytes();
DatagramPacket packet = new Datagram
Packet(data, data.length, address, port);
// Send packet
long startTime = System.currentTimeMillis();
socket.send(packet);
// Prepare to receive response
byte[] receiveData = new byte[1024];
DatagramPacketreceivePacket=newDatagra
mPacket(receiveData, receiveData.length);
socket.receive(receivePacket);
// Measure round-trip time
long rtt = System.currentTimeMillis() -
startTime;
// Display the hop details
InetAddress routerAddress = receivePacket
.getAddress();
System.out.println("Hop " + ttl + ": " + router
Address .getHostAddress() + " (RTT: " + rtt +
"ms)");
// If the response address is the final
target
//then exit the loop
if (routerAddress.equals(address))
{
System.out.println("Reached destinati
on.");
break;
}
} catch (SocketTimeoutException e)
{
System.out.println("Hop " + ttl + ": Request
timed out.");
} finally
{
socket.close();
}
}
}
}
OUTPUT :

You might also like