Assignment 2
Assignment 2
Assignment 2
Version1 : The proxy server will accept a single connection from a client and
forward it using a single connection to the server. During the connection, the
proxy will not accept any other connections.
Version 2: The proxy server will accept connections from multiple clients and
forward them using multiple connections to the server. The proxy should
forward data in both directions: from client to server and from server to client.
No client or server should be able to hang the proxy server by refusing to read
or write data on its connection. For instance, if one client suddenly stops
reading from the socket to the proxy, other clients should not notice
interruptions of service through the proxy.
Example
Here is an example of how to use tcpproxy to redirect all connections to port
4000 on your local machine to google's web server.
import java.io.*;
import java.net.*;
import java.util.*;
try
{
socket1=new Socket(IPAddress1, this.port1);
socket2=new Socket(IPAddress2, this.port2);
sockIn1=new Scanner(socket1.getInputStream());
sockOut1=new PrintWriter(socket2.getOutputStream(),
true);
sockIn2=new Scanner(socket2.getInputStream());
sockOut2=new PrintWriter(socket1.getOutputStream(),
true);
new Thread(this).start();
while(sockIn1.hasNextLine())
{
String line=sockIn1.nextLine();
System.out.println(IPAddress1+":"+port1+">"+line);
sockOut1.println(line);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
if(args.length==2)
new ConnectPorts(args[0], args[1]);
else
new ConnectPorts(args[0], args[1], args[2], args[3]);
}
@Override
public void run()
{
while(sockIn2.hasNextLine())
{
String line=sockIn2.nextLine();
System.out.println(IPAddress2+":"+port2+">"+line);
sockOut2.println(line);
}
}
}
<PortRedirect.java>
import java.io.*;
import java.net.*;
import java.util.*;
<Handler.java>
import java.io.*;
import java.net.*;
import java.util.*;
class Handler
{
public Handler(Socket socket1, Socket socket2) throws Exception
{
this(new Scanner(socket1.getInputStream()), new
PrintWriter(socket2.getOutputStream(), true), new
Scanner(socket2.getInputStream()), new
PrintWriter(socket1.getOutputStream(), true));
}