I am currently implementing a multithreaded proxy server in java which will accept messages from clients and forward them to another server which will then acknowledge the reception of the message. However, i'm having trouble doing so. Could someone point out what i am doing wrong? Thanks.
CLIENT SIDE
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client{
public static void main(String[] args)
{
try
{
Socket client = new Socket(InetAddress.getLocalHost(), 6789);
if(client.isBound())
{
System.out.println("Successfully connected on port 6789");
}
Scanner scanner = new Scanner(System.in);
DataInputStream inFromProxy = new DataInputStream(client.getInputStream());
DataOutputStream outToProxy = new DataOutputStream(client.getOutputStream());
while(true)
{
String message;
System.out.print("Enter your message: ");
message = scanner.next();
outToProxy.writeUTF(message);
System.out.println(inFromProxy.readUTF());
}
}
catch(IOException io)
{
System.err.println("IOException: " + io.getMessage());
System.exit(2);
}
}
}
SERVER SIDE
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args)
{
try
{
ServerSocket server = new ServerSocket(6780);
if(server.isBound())
{
System.out.println("Server successfully connected on port 6780");
}
Socket client = null;
while(true)
{
client = server.accept();
if(client.isConnected())
{
System.out.println("Proxy is connected");
}
DataInputStream inFromProxy = new DataInputStream(client.getInputStream());
DataOutputStream outToProxy = new DataOutputStream(client.getOutputStream());
System.out.println(inFromProxy.readUTF());
outToProxy.writeUTF("Message has been acknowledged!");
}
}
catch(IOException io)
{
System.err.println("IOException: " + io.getMessage());
System.exit(2);
}
}
}
PROXY
import java.io.*;
import java.net.*;
public class Proxy{
public static ServerSocket server = null;
public static Socket client = null;
public static void main(String[] args)
{
try
{
server = new ServerSocket(6789);
Socket clientsocket = null;
while(true)
{
client = server.accept();
if(client.isConnected())
{
System.out.println("Proxy is currently listening to client on port 6789");
}
clientsocket=new Socket(InetAddress.getLocalHost(),6780);
Thread t1 = new ProxyHandler(client, clientsocket);
t1.start();
Thread t2 = new ProxyHandler(clientsocket,client);
t2.start();
if(clientsocket.isBound())
{
System.out.println("Clientsocket successfully connected on port 6780");
}
}
}
catch(IOException io)
{
System.err.println("IOException: " + io.getMessage());
}
}
}
PROXYHANDLER
import java.io.*;
import java.net.*;
public class ProxyHandler extends Thread {
private Socket socket;
private String message;
public ProxyHandler(Socket socket, Socket clientsocket)
{
this.socket = socket;
}
public void run()
{
message = "";
try
{
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
while(true)
{
message = in.readUTF();
out.writeUTF(message);
System.out.println(message);
}
}
catch(IOException io)
{
System.err.println("IOException: " + io.getMessage());
System.exit(2);
}
}
}