#Router/Forwarder Code

1 messages · Page 1 of 1 (latest)

zenith mirage
#
import java.io.IOException;
import java.net.*;

public class Router {
    
    private DatagramSocket receiveSocket;
    private DatagramSocket sendSocket;
    
    public Router(int receivePort, int sendPort) throws SocketException {
        receiveSocket = new DatagramSocket(receivePort);
        sendSocket = new DatagramSocket(sendPort);
    }
    
    public void forwardPacket() throws IOException {
        //The initialized values should be changed according to the PCs and ports used.
        InetAddress serverAddress = InetAddress.getByName("ServerAddress");
        InetAddress clientAddress = InetAddress.getByName("ClientAddress");
        int serverPort = 6666;
        int clientPort = 6669;
        
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
        receiveSocket.receive(packet);

        // simulate 10% packet loss
        if (Math.random() < 0) {
            System.out.println("Packet dropped: " + new String(packet.getData(), 0, packet.getLength()));
            return;
        }
        
        InetAddress destAddress ;
        int destPort ;
        if(packet.getAddress().equals(serverAddress) &&  packet.getPort() == serverPort )
        {
            destAddress = clientAddress;
            destPort = clientPort;
             System.out.println("recieved packet from server");
        }else
        {
            destAddress = serverAddress;
            destPort = serverPort;
            System.out.println("recieved packet from client");
        }
        
        DatagramPacket sendPacket = new DatagramPacket(packet.getData(), packet.getLength(), destAddress, destPort);
        sendSocket.send(sendPacket);
        System.out.println("Packet forwarded: " + new String(packet.getData(), 0, packet.getLength()));
    }
    
    public void close() {
        receiveSocket.close();
        sendSocket.close();
    }
    
    public static void main(String[] args) throws IOException {
        // create a router instance
        Router router = new Router(9000, 9001);
        
        // forward packets until user interrupts the program
        while (true) {
            try {
                router.forwardPacket();
            } catch (IOException e) {
                e.printStackTrace();
                break;
            }
        }
        
        // close the sockets
        router.close();
    }
}

i was given this code to implement but not really edit on. the problem is, the project i am working on includes multiple clients and one server. with that being said, i am asked to initialize the client port and serverport as well as their inetaddresses. how do i do that if each client has a random port. the server has one default port, port no.6666 but when a client connects to the server it gives that client a unique serverPort to communicate messages to and from. so im not sure who do i implement what i have with this router class

olive oreBOT
#

This post has been reserved for your question.

Hey @zenith mirage! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.