aula5TProxy

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

class ClientHanlder extends Thread {
    Socket sockIn;
    Socket sockOut;

    public ClientHanlder(Socket s1, Socket s2) {
        sockIn = s1;
        sockOut = s2;
    }

    @Override
    public void run() {
        int c;

        System.out.println(Thread.currentThread().getName() + " iniciada.");
        try {
            InputStream in = sockIn.getInputStream();
            OutputStream out = sockOut.getOutputStream();

            while ((c = in.read()) != -1) {
                out.write(c);

            }
        } catch (IOException e) {
            System.out.println(Thread.currentThread().getName() + " : " + e);
        } finally {
            try {
                sockIn.close();
            } catch (Exception e) {
            }
            try {
                sockOut.close();
            } catch (Exception e) {
            }
        }

    }
}

class Proxy {
    static final int LISTENING_PORT = 5001;
    static final String POP_ADR = "pop.isec.pt";
    static final int POP_PORT = 110;

    private int listeningPort;
    private int destinationPort;
    private String destinationAddr;
    ServerSocket listeningSocket;

    public Proxy() {
        listeningPort = -1;
        destinationAddr = null;
        destinationPort = -1;
    }

    public void processArguments(String args[]) {

        if (args.length == 3) {
            try {
                listeningPort = Integer.parseInt(args[0]);
                destinationAddr = args[1];
                destinationPort = Integer.parseInt(args[2]);
            } catch (NumberFormatException e) {
                listeningPort = -1;
            }
        }

        if (listeningPort == -1) {
            listeningPort = LISTENING_PORT;
            destinationAddr = POP_ADR;
            destinationPort = POP_PORT;
        }

    }

    public void processaPedidos(String args[]) throws IOException {
        Socket cliScket, socketToPopServer;
        Thread t1, t2;

        processArguments(args);
        System.out.println("porto de escuta: " + listeningPort + " ; destino " + destinationAddr + " ; Porto de destino: " + destinationPort);

        while (true) {
            cliScket = listeningSocket.accept();
            System.out.println("Novo cliente: " + cliScket.getInetAddress().getHostName() + ":" + cliScket.getPort());

            try {
                socketToPopServer = new Socket(destinationAddr, destinationPort);

            } catch (IOException e) {
                System.out.println(e);
                cliScket.close();
                continue;
            }

            t1 = new ClientHanlder(socketToPopServer, cliScket);
            t2 = new ClientHanlder(cliScket, socketToPopServer);

            t1.setName("Thread " + socketToPopServer.getInetAddress().getHostAddress() + ":" + socketToPopServer.getPort() + "->" + cliScket.getInetAddress().getHostAddress() + ":" + cliScket.getPort() + "]");
            t2.setName("Thread " + socketToPopServer.getInetAddress().getHostAddress() + ":" + socketToPopServer.getPort() + "->" + cliScket.getInetAddress().getHostAddress() + ":" + cliScket.getPort() + "]");

            t1.start();
            t2.start();
        }

    }
}


public class aula05TProxy {
    public static void main(String[] args) throws IOException {
        Proxy p;
        p = new Proxy();
        p.processaPedidos(args);
    }
}
Tags : ,

0 thoughts on “aula5TProxy”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.